Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added ValidableQuestion #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Conversation

pechitook
Copy link

With ValidableQuestion you can do things like:

$question = new ValidableQuestion("Give me something: ", ["required"]);

or

$question = new ValidableQuestion("Give me a number: ", ["numeric"]);

I needed to exctract this out and now I share it here. It's just an idea of a functionality that I had to extend into this amazing package. Hope it helps.

Thanks :)

@Danack
Copy link

Danack commented Sep 21, 2014

This seems to be a (limited) duplicate of the functionality available through passing a validation callable:

http://symfony.com/doc/current/components/console/helpers/questionhelper.html#validating-the-answer

@pechitook
Copy link
Author

Yes, it kind of is. The idea is to provide a one-liner, readable solution to do simple validation that otherwise you would have to rewrite for each question. This could simply be extended to allow simple validation templates like required, numeric, email, text, max_characters, regex, etc.

I found this to be very useful when you need to validate 5-10 user's consecutive answers, and all you're interested in is its type, or if it is not null.

I hope I could make myself clear on the distinction now. Anyhow, I do realise that this may be too specific to include in the package, but I found it very useful and wanted to share it.

Thanks

@Danack
Copy link

Danack commented Sep 22, 2014

Not my repository, so totally not up to me whether it gets accepted or not but in general adding duplicate functionality doesn't sound like a great idea.

otherwise you would have to rewrite for each question.

Why would you have to rewrite the callable? You can just define it once and re-use the same callable for multiple questions.

readable solution to do simple validation that

As annoying as the syntax for PHP closures is, once you get used to it, it's totally readable. As callables help you composite functionality and also make writing asynchronous code be much easier, I'd recommend getting used to them.

@pechitook
Copy link
Author

That's what I did, I wrote the callable once (on a sepparate class, extending Question) in order to be able to use it app-wide. I thought it would be a better approach than having similar callables on each class, or one class or traits containing those validation related functions that I would call on each setValidator().

I'm not against PHP closures, I kinda love them (?), but for this solution I prefer something like this:

$question = new ValidableQuestion("Name of the city?: ", ["required", "max:60"]);
$name = $helper->ask($input, $output, $question);

Thanks a lot for the feedback!

@lyrixx
Copy link
Member

lyrixx commented Sep 26, 2015

This is a read only repository.

Pull Request should be send to https://github.com/symfony/symfony.
After the merge in the main repository, a sub-tree of split is done and this repository is updated.

@fabpot
Copy link
Member

fabpot commented Sep 28, 2015

Closing as someone already mentioned, we try to avoid having more than one way to do something.

@fabpot fabpot closed this Sep 28, 2015
symfony-splitter pushed a commit that referenced this pull request Sep 28, 2024
…adFromProcess` (fritzmg)

This PR was merged into the 5.4 branch.

Discussion
----------

[Console] Suppress `proc_open` errors within `Terminal::readFromProcess`

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

When instantiating `SymfonyStyle` in a command it will try to determine the maximum width of the current console interface.

https://github.com/symfony/symfony/blob/6687e4ea35f45ebd73fb0315938103628cfb13a0/src/Symfony/Component/Console/Style/SymfonyStyle.php#L53

This will execute `stty -a | grep columns` down the line. Access to `stty` might be disallowed however, resulting in the following error:

```
ErrorException: Warning: proc_open(): Exec failed: Permission denied
#16 /vendor/symfony/console/Terminal.php(220): Symfony\Component\Console\Terminal::readFromProcess
#15 /vendor/symfony/console/Terminal.php(204): Symfony\Component\Console\Terminal::getSttyColumns
#14 /vendor/symfony/console/Terminal.php(170): Symfony\Component\Console\Terminal::initDimensionsUsingStty
#13 /vendor/symfony/console/Terminal.php(153): Symfony\Component\Console\Terminal::initDimensions
#12 /vendor/symfony/console/Terminal.php(94): Symfony\Component\Console\Terminal::getWidth
#11 /vendor/symfony/console/Style/SymfonyStyle.php(55): Symfony\Component\Console\Style\SymfonyStyle::__construct
#10 /vendor/symfony/messenger/Command/ConsumeMessagesCommand.php(136): Symfony\Component\Messenger\Command\ConsumeMessagesCommand::interact
#9 /vendor/symfony/console/Command/Command.php(311): Symfony\Component\Console\Command\Command::run
```

(Stack Trace actually from Symfony 6)

The phpDoc of `Terminal::getSttyColumns` states

> Runs and parses stty -a if it's available, _suppressing any error output_.

The latter might refer to `['suppress_errors' => true]` (though I am not sure) - which is a Windows only functionality. In any case, since `Terminal::readFromProcess` already checks for

```php
if (!$process = proc_open(…)) {
    return null;
}
```

and

```php
if (!\is_resource($process)) {
    return null;
}
```

upstream in Symfony 6/7, indicating that `proc_open` might fail - this error can additionally be suppressed using `@`. Besides, `Process::start` also uses ``@proc_open`` (added in symfony/symfony@099481f "Prevent warning in proc_open()").

Commits
-------

575249a3431 suppress proc_open errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants