8000 validation issue after removeElement() · Issue #5609 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

validation issue after removeElement() #5609

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
arnaugm opened this issue Sep 26, 2012 · 4 comments
Closed

validation issue after removeElement() #5609

arnaugm opened this issue Sep 26, 2012 · 4 comments

Comments

@arnaugm
Copy link
arnaugm commented Sep 26, 2012

I have a form with a collection of elements. For usability I create a first blank element of the collection for the user. I want to validate that the collection has at least one element, so in case the user leaves this first element blank, I remove it with removeElement() before calling $form->isValid().

My surprise is that even though the element is not any more inside the collection, the validation doesn't take it into account and keeps thinking that there is still one element in the collection, so the validation pass when it should fail.

Am I missusing the remove feature?

I verified that if I remove the element using the application and submit the form with a real empty collection, then the validator fails as expected.

I'm in S2.1.1

@stof
Copy link
Member
stof commented Sep 26, 2012

The validation is applied during the binding of the form (it is done during the POST_BIND event. $form->isValid() does not trigger the validation. It only gives you access to the status of the validation.

@arnaugm
Copy link
Author
arnaugm commented Sep 26, 2012

Ok I was confused on that.
But then how do I remove the field? Before binding the form I don't have access to the entity, right?
Should I use an event?

@stof
Copy link
Member
stof commented Sep 26, 2012

@arnaugm you should use a listener on one of the form events. It could be either on PRE_BIND if you want to remove the empty child in the client data (the format submitted by the user) or on POST_BIND (with a higher priority than the ValidatorListener so that it runs before the validation) if you prefer removing the element in the app data (your entities)

@arnaugm
Copy link
Author
arnaugm commented Sep 27, 2012

That's what I'll do then. Thanks!

@arnaugm arnaugm closed this as completed Sep 27, 2012
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

No branches or pull requests

2 participants
0