[OptionsResolver] Allow giving a callback as an allowedValue to OptionsResolver#8375
[OptionsResolver] Allow giving a callback as an allowedValue to OptionsResolver#8375fabpot merged 1 commit intosymfony:masterfrom marekkalnik:allow-callback-to-validate-option
Conversation
There was a problem hiding this comment.
you are not checking if $options[$option] is set
There was a problem hiding this comment.
and you can use if instead of elseif a the previous if throws an exception
There was a problem hiding this comment.
Thanks @stof, it's fixed. I should stop making PR late at night... :/
|
good idea, 👍 |
|
need ! 👍 |
|
👍 nice. :) |
|
👍 |
1 similar comment
|
👍 |
|
Perhaps it would be useful to also support regular expressions. Your opinion? |
|
@shouze - actually I start to think that it would be nice to use validation constraints with it. This would couple the two components though. As for regexp - you can use a callback, I don't see much of a gain in direct support. |
|
👍 |
1 similar comment
|
👍 |
|
@marekkalnik you're right. In fact we've seen your PR few days ago while we were looking for an elegant solution to validate query string params for a REST api (and avoid some cache poisoning). We know that we could use Form validation but it looked a bit over bloated for a such simple thing. BTW OptionsResolver must remain simple. |
|
Great feature ! 👍 |
|
💯 👍 Regex and constraints should be possible with the closure, so adding that would over-complicate things. |
|
👍 |
|
the feature has gotten some feedback so i think the docs would be nice 👍 👶 |
|
Ok, I'll write some by the end of the week. |
|
I added the doc, sory that it's late, it wasn't easy with all the family holidays :( But it made me think - it won't work with the addAllowedValue, so maybe I should write a separate method for specyfying a callback? What do you think? |
|
@marekkalnik Can you fix the CS issues (http://fabbot.io/report/symfony/symfony/8375/8d05642f9a57150f51da56a3a3c7a122375b71ba)? |
There was a problem hiding this comment.
I would have said: The option "%s" has the value "%s", which it is not valid
|
@fabpot I fixed the CS error and changed the message |
There was a problem hiding this comment.
This is wrong as you have 2 %s but only one var
|
@fabpot it's better now |
|
Thank you @marekkalnik. |
…Value to OptionsResolver (marekkalnik) This PR was merged into the 2.5-dev branch. Discussion ---------- [OptionsResolver] Allow giving a callback as an allowedValue to OptionsResolver I recently had to use an option which was an array and could contain some one or multiple values from a list. As it could contain all possible combinations, it was not possible to validate it with a list of allowed values. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | -- | License | MIT | Doc PR | symfony/symfony-docs#3437 Commits ------- 07d1d30 Allow giving a callback as an allowedValue to OptionsResolver
This PR was merged into the master branch. Discussion ---------- Add info about callback in options resolver | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#8375) | Applies to | 2.5+ | Fixed tickets | The documentation for the PR (pending) about validation callbacks in OptionsResolver. Commits ------- 8231230 Fix according to PR comments 94fe8dc Add info about callback in options resolver
I recently had to use an option which was an array and could contain some one or multiple values from a list. As it could contain all possible combinations, it was not possible to validate it with a list of allowed values.