-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] required
attribute is set on all form fields regardless of whether they are required or not
#3661
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
Comments
There is no inconsistency in the doc: the first place tells that the HTML5 |
OK, then let’s focus on fixing the behaviour, and then update the documentation to reflect it.
|
setting required to false so late would be a huge and by huge I mean giant BC break. |
@mvrhov The change is not that giant. @oscherler only talks about cases were a class was annotated with validation constraints and fields that did not have a Not[Null|Blank] constraints were still set to required by default, leaving the "required" attribute in the browser. Server-side validation did not happen, so on browsers < HTML5 you don't even notice the difference after fixing this. |
Commits ------- e0ce6b4 [Form] Fixed required value guessed by ValidatorTypeGuesser Discussion ---------- [Form] Fixed required value guessed by ValidatorTypeGuesser Bug fix: yes Feature addition: no Backwards compatibility break: no* Symfony2 tests pass: yes Fixes the following tickets: #3661 Todo: -  The documentation states that a field is set to required if constrained with NotBlank or NotNull and to not required otherwise. Due to a bug this was not the case and fields were always required. This is now fixed. The consequence is that some fields that were required before by default are not required anymore. The only difference is that the HTML5 "required" attribute disappears, because server-side validation did not occur before anyway.
I fear this behaviour is back... I'm having required field by default EVEN with required => false set in some case (ie repeated password). Again, I have all my fields set to required even if no assert annotation exists. See my e-mail this morning on SF2 ML called "Forms&validators / Capricious required option ?". thanks for your reply. |
I am using symfony 2.2 and it looks like bschussek solution has been implemented in this version but the problem remains !! |
@mlwacosmos I'm having the same issue as well on 2.2 using Propel (and therefore not using annotations). All fields are required by default. |
Actually, the problem still exist. Symfony does not try to guess the form options when does not need to guess the form type, check You can easily reproduce the bug, just create an entity with this field like this:
Now let's create a form for this entity, if you create the field without specifying the type:
Symfony guess that the
Symfony doesn't guess the This also apply if we change the constraints for the field since |
@eagleoneraptor This is on purpose, so guessing can be deactivated. Otherwise guessing might add things suddenly you don't want. |
See also #5365 |
Hi there,
The executed code is well the "else" case, but the fields are still required. Is something which can be done for fix it please ? Thanks. |
Your code is wrong. It must be
|
Hi Tobion, Thanks a lot for your quick answer, that was the trick ! See you. |
You are welcome. |
This problem still occurs when using the form builder (without any data class). I think this is the minimal setup: <?php
// Controller
public function indexAction()
{
$form = $this->createFormBuilder()
->add('q', 'text')
->getForm()
;
return [
'form' => $form->createView(),
];
} {# Template #}
{{ form_widget(form.q) }} <!-- Output -->
<input type="text" id="form_q" name="form[q]" required="required" class=" form-control" /> |
<?php
// Controller
public function indexAction()
{
$form = $this->createFormBuilder()
->add('q', 'text', ['required' => false])
->getForm()
;
return [
'form' => $form->createView(),
];
} The |
@stof this still holds true (although this is maybe a DX matter):
|
@stof complementing my last comment: I would probably do something like this: <?php
// Controller
public function indexAction()
{
$form = $this->createFormBuilder()
->add('q', 'text', [
'constraints' => [ new NotBlank ],
])
->getForm()
;
return [
'form' => $form->createView(),
];
} |
i faced the same problem, and after digging around i made a Extension which solves the UI problem for me. |
So, the validation_groups can't control the html5 required? |
@waltercruz My point exactly. Some validation groups require the field, others do not. Hardcoding a |
I'm also facing this problem, with validation groups and required in the interface. I solved this, making a small change to how things work in the system flow. So I ended up with 2 forms, this way I've avoided the validation groups. But, it's annoying. |
Still a 'problem' in 2.7.3. I can't let Symfony guess the field type because I need textarea instead of normal text. So I have to hardcode the required -> false, instead of using nullable=true. |
@fabpot @webmozart Can we get a little love in here? I think we have a pretty compelling case. Can we, at least, know why this won't be fixed? Or even better, is there a way so we can implement it ourselves in a per-project basis? (something like changing the default guesser) |
@felds the guesser is fine. when guessing is used, it will guess it properly. The issue is with the default value when guessing is not used. and this default value cannot be changed in 2.x as it is a BC break. However, the form component has tons of extension points, so it is totally possible to change the default (be aware that you may miss the HTML5 validation in forms provided in third-party bundles if they rely on the existing default): namespace AppBundle\Form\TypeExtension;
use Symfony\Component\Form\AbstractTypeExtension;
class ChangeRequiredDefaultTypeExtension extends AbstractTypeExtension
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array('required' => false));
}
public function getExtendedType()
{
return 'form';
}
} # app/config/services.yml
services:
# ...
app.form.type_extension.change_required_default:
class: AppBundle\Form\TypeExtension\ChangeRequiredDefaultTypeExtension
tags:
- { name: form.type_extension, alias: form } |
@stof It still doesn't fix this #3661 (comment), but it is surely a better solution than putting a To condense the current state of this issue (as a tl;dr for us in the future): The current form... $form = $this->createFormBuilder()
->add('x', 'text')
->add('y', 'text', [
'constraints' => [
new Assert\NotBlank,
],
])
->getForm()
; ... returns without the extension: <input type="text" id="form_x" name="form[x]" required="required" />
<input type="text" id="form_y" name="form[y]" required="required" /> ... and with the extension: <input type="text" id="form_x" name="form[x]" />
<input type="text" id="form_y" name="form[y]" /> What me and others in this issue would like it to return: <input type="text" id="form_x" name="form[x]" />
<input type="text" id="form_y" name="form[y]" required="required" /> (without hardcoding the |
@felds were you able to accomplish what you were trying to do
by using validation_groups |
@drmjo Not really. I've been doing all by hand. Sent from my George Foreman Grill
|
@felds Same Here... the logic involved gets really complicated and involves hard coded group names... |
Have the same issue. with checkboxes.
The problem with checkboxes is that you can't submit a FALSE value x'd Is it possible to set required = false for all the checkboxes only by default? |
In your class of form you must implement method configureOptions, like this:
|
8 years and still not fixed. Amazing. |
for those who may be interested, this public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'required' => false,
]);
} WILL NOT WORK (smf v 5.1); even more - none of your fields will have example class CompanyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'required' => true,
])
->add('description', TextareaType::class, [
'required' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'required' => false,
]);
}
} you expect name to have 'required' html, right? nope. only when you remove default to false in configureOptions(), the name becomes required. so the only option you are left is to do is |
As it seems that there is still the need by developers to only guess the options for a form I have decided to start to work on a new |
@yyaremenko I am not sure if I completely understand the issue you describe in #3661 (comment). It may be worth to open a new issue with it and some example application that allows to reproduce it. |
This should be a simple fix, what's going on? |
2024, using 7.3, I am encountering exactly the situation @yyaremenko described. While there may be technical reasons for this, as a new Symfony user, the behaviour of |
Build a form from a form type class:
Add basic validation rules where none of the fields are required:
Render the form:
All the fields get a
required
attribute:This is incorrect, since the validation rules state that they are not required.
Under Forms (2.0) – Form Validation, the documentation states:
This is clearly not the case: all the fields get the
required
attribute.Also, under Built-in Field Types – Field Type Options (i.e. not under Form Validation), one can read:
In other words, Symfony is artificially introducing a discrepancy between the client- and the server-side validation criteria, and this by default.
Moreover, there is a conflict between the two cited paragraphs of the documentation. The first one states that the
required
attribute is rendered on fields that are required, which is confirmed by the following paragraph under Field Type Guessing – Field Type Options Guessing:However, the second paragraph states that the
required
option is set totrue
by default.Resolution
The documentation should be corrected for consistency: under Built-in Field Types – Field Type Options:
Symfony’s behaviour should be changed to match the documentation: the
required
option should be set based on the guessed value and not totrue
by default.The text was updated successfully, but these errors were encountered: