-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
[Form] Validate missing fields when $clearMissing is false #25331
Comments
Could you please add a test case failling so we can look at it and find a fix ? |
I am trying to create a test but I am not able to make it working. In I would like to create a form with a single field and submit it a blank value (in order to check if the Then we should have to change the submit by public function testValidateConstraintsIfClearMissingIsFalse()
{
$object = $this->getMockBuilder('\stdClass')->getMock();
$constraint = new NotBlank(array('groups' => 'group2'));
$options = array(
'constraints' => array($constraint),
);
$form = $this->getBuilder('name', '\stdClass')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->setData($object)
->getForm();
$missing = $this->getBuilder('missing', '\stdClass', $options)->getForm();
$form->add($missing);
// Launch transformer
$form->submit(array('missing' => ''));
$this->expectValidateAt(0, 'data', $object, array('Default'));
$this->expectValidateValueAt(1, 'data', $object, $constraint, 'Default');
$this->validator->validate($form, new Form());
$this->assertNoViolation();
} |
@Seb33300 You can just create a small example project that allows to reproduce your issue if you struggle to come up with a real test case. We can then look into it and see if there's actually any work to do. |
@Seb33300 I am closing for now, but please leave a comment when you have a reproducer and we can reopen. |
The project I created on my original issue is still valid to reproduce the issue: #9998 (comment) |
This is a really annoying issue that is most likely the reason of a lot of posts around the net if you search for this problem topic. |
Ok after digging a bit into the form->submit logic I guess I found the problem root. There does not seem to be a perfectly easy change possible. At least not for my knowledge. Either it needs an "else-handling" for not calling form->submit or call form->submit for required fields too and change some insides |
@HeahDude Could you please look at this one ? it seems to be a really annoying bug. |
This was reported many times in the past, and is not a bug. Changing this behavior is a big BC break. |
I understand the technical difficulties and also to break a previous (bad) behavior. But putting this to side and focus on the expectations of a user on this functionality I can't share the position to call this not a bug. Also from a security point this is sensitive. A HTML-Form that per default sends an empty input field going through all tests. A simple "hack" by removing this empty field from the post would break the validation of this application with unknown consequences. |
Hello @simonberger, sorry I don't follow you, especially on the security part. Let's put things in context :).
From here, I ask you guys. What concrete use case does not fit with that? Then let's try to fix it. |
Thank you for your answer @HeahDude What the different impact of the parameter values is:
In my project we wanted to archive the following:
We can just have one of those above requirements. Note: I know we can't do much here without breaking functionality. But for example the parameter name can be changed which would add clarity. I favor long parameter names for such a complex impact ;) |
Thanks @simonberger, now that I understand the use cases, I can tell you, indeed, this |
Hi,
it behaves the same as: which means that missing entity fields that are not submitted in the request, their values are set to null, which is not what I want when I am using an Edit Form, because normally I have many optional fields and some and not even rendered in the form because they are no intended to be modified. I'm I right? The validation part of the entity is other related thing, that can be achieved manually by calling the validator on the entity, or using Thanks. |
true
In such case (clear missing is true), when editing, optional fields can mean two things:
|
I may need to precise about:
the request handler also prevents submitting if the post max size is reached, and merges |
Re-reading the issue I am going to close here. As @HeahDude explained this is the expected behaviour and we are not going to change that. Though, if someone likes to open a pull request to improve the documentation, we will happily review and merge that. |
…trickbussmann) This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #11905). Discussion ---------- Added example for direct form submit with validation See symfony/symfony#25331. I had the same issue. My registration form is filled via an API call and this call contains only filled fields. But when the user is missing all fields its an empty array and then the username and email will not be validated. After I found out that this is the behaviour I want to share this knowledge ;-) Commits ------- c81d30a Added example for direct form submit with validation
When using the
$clearMissing
option set tofalse
(when submitting a form with$form->submit()
), validation constraints on missing fields are ignored.Since the fields are in the form, I think they should be validated too even if they are missing on submit.
I reported this issue several years ago but it seems to be misunderstand: #9998
The example provided on the original issue is still valid to reproduce the issue.
The text was updated successfully, but these errors were encountered: