-
Notifications
You must be signed in to change notification settings - Fork 701
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
[RFC] Added the constraint option on the ParamFetcher #700
[RFC] Added the constraint option on the ParamFetcher #700
Conversation
ok .. let me know when you have some docs to review |
@lsmith77 It's ready for review |
*/ | ||
public function __construct(ParamReader $paramReader, Request $request) | ||
public function __construct(ParamReader $paramReader, Request $request, Validator $validator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The typehint should be the ValidatorInterface, not the implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof Fixed, thx.
Whether you want nested values or validation, I think you should use a form: public function fooAction(Request $request)
{
$form = $this->get('form.factory')->createNamed('', 'acme_foo_filter');
$form->submit($request->query->all(), false);
} I also think QueryParam should always be replaced by using a form. What the FOSRestBundle could do to help is add some kind of ParamConverter to get the bound form as an argument: /**
* @SomeFosRestConverter(name = "form", type = "acme_foo_filter", request = {"query", "_attributes"})
*/
public function fooAction(Form $filterForm)
{
} |
@adrienbrault I disagree. With your proposal we should drop completly the /** @QueryParam(name="foo", constraint=@Regex("\d+")) */ and /** @QueryParam(name="foo", requirements="\d+") */ I'm only extending the functionalities here. The "problem" with your |
@adrienbrault I disagree as well. The main feature of the Form component is data binding. If all you need is validating data, binding a form is useless overhead (and not negligeable overhead). It is better to run only the validator in such case |
Right, I guess this is mostly for cases where it is not really necessary to map things to a model/form type and where an array structure is really sufficient. |
I'm not arguing against that change, I'm arguing against the whole |
Soooo ... Could we merge this as it ? We can discuss of droping QueryParam support in an other PR I guess. |
if ('' !== $config->requirements | ||
&& ($param !== $default || null === $default) | ||
if ($param === $default && null !== $default) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can remove this empty line?
I finaly reviewed the code. I hope it will be ok for you. |
needs a rebase according to github |
} | ||
|
||
$constraint = new Regex(array( | ||
'pattern' => '#^'.$config->requirements.'$#xs', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preg_quote()
?
Rebased and added |
wait, the annotation does not work:
|
…ms and RequestParams
I changed |
[RFC] Added the constraint option on the ParamFetcher
thx! |
thx ;) |
It looks like the requirements are not working anymore because of |
ok, i guess we need to document that quoting is the responsibility of the user in that case and remove it. can you work on a PR, ideally with a test case? |
Well, given that the requirement is a regex, applying preg_quote is indeed wrong |
Yup it broke all regexes in my "requirements". |
Ahm, that was more a question than a suggestion.. Sorry :-( |
please test ed28a05 |
Looks good @lsmith77. |
ok .. i have released 1.3.1 |
BTW it is a funny situation because this should have been covert by unit tests but.. tests have been updated as well. :) |
How can I set custom options for the Constraint?
|
Oh well. The upcoming sf 2.5 broke the validator implementation. |
annotations have never been meant to work as Your syntax is passeing an array as param to the constraint, with a |
Hi friends of Symfony :)
For business needs, I want that my API allows some complex
QueryParams
like deep array structures. For example I want to be able to query/api/users?filters[0][key]=name&filters[0][operator]=equals&filters[0][value]=Foobar
.I wanted those parameters validated in order to return a
BadRequestResponse
like its did using therequirements
of theQueryParam
orRequestParam
so I added an option:constraint
and this constraint is validated by the Validator component.requirements
andconstraint
cannot be combined.