-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
form field type option disabled should not ignore submitted value #12946
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
Actually, I think what you're looking for is IF |
You need the readonly attribute on the field. use Symfony\Component\Form\AbstractType;
class FooBarType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('addFoo', 'choice', array(
'choices' => array(
'0' => 'No',
'1' => 'Yes'
),
'expanded' => true,
'required' => true
))
->add('selectFoo', 'entity', array(
'class' => 'Acme\AppBundle\Entity\Foo',
'attr' => array('readonly' => true)
))
;
$builder->addEventListener(
FormEvents::SUBMIT, // `selectFoo` does not get populated
function(FormEvent $event) use ($options) {
$data = $event->getData();
$form = $event->getForm();
$disabled = $data->getFoo() == '1' ? false : true;
$form->add('selectFoo', 'entity', array(
'class' => 'Acme\AppBundle\Entity\Foo',
'disabled' => $disabled
));
}
);
}
} Disabled means by specification that there is no data submitted and considered in the form, readonly means that the data can be sent and will be bound to the entity. Please use the readonly attribute hence the readonly option might get deprecated. (#10658) |
And as you found #12799 this explains a bit more about the issue. |
Thanks guys, it really looks like I was totally mixing up the behaviours of those options. Looking at: Maybe these lines would make sense in the docs too? |
BTW: |
Then add ->add('selectFoo', 'choice', array(
'attr' => array('disabled' => 'disabled')
) It should add the "disabled" attribute to the select tag, without useing the "disabled" option (which disables the server-side too). |
But you need to be careful. If you do not remove the disabled attribute it will send no data and on binding the form data the value gets lost. |
Sure abd thanks @loostro & @peterrehm ! My use case now works fine setting the Maybe we can improve the docs by adding an example like this to the cookbook? |
But usually for your case the disabled attribute is not really recommended, only needed for the select field I guess. You can draft a PR to the docs repository. |
Agree @peterrehm . Maybe I will add it to my blog first and have a look at the reactions if it is worth a cookbook entry. |
As of 11 December 2014 adding form elements on I have form fields whose validity depends on other fields value. As I can only access submitted form data object on P.S: I can not use form validation group in my case. |
Hello, I know it's long time, but could you check this out? Let me know. |
The description added by @Sgoettschkes in symfony/symfony-docs#2114 states:
I only found this short discussion about
readonly
anddisabled
:symfony/symfony-docs#2096
Personally I always regarded
readonly
as being what is now described in the docs asdisabled
.The
disabled
option in HTML was designed to beenabled
again.In my simple use case I have a radio button
addFoo
that was checked with the value1
resp.true
.The Client then enables a dependent choice via JS.
The form now expects the user to select a value from a choice widget
selectFoo
that wasdisabled
by default.After submitting the form should be invalid because a constraint is used to check if a selection has been made since
addFoo
was checked. Of course nowselectFoo
should no longer be disabled.This can be handled by the
SUBMIT
event where I re-add the element beingenabled.
So far so good!
But if I check
addFoo
and DO select a choice inselectFoo
and submitting the form then fails because of a different error theselectFoo
is not being populated because the original choice field type - before enabling it in theSUBMIT
event - had thedisabled
option.The code:
https://gist.github.com/webdevilopers/5306ee3417791227abf3
Should we re-consider the functionality of the
disabled
option when setting data on the form?Or how should this use case (Disabled field gets enabled on client side and needs to be populated afterwards) be handled?
Will
dependent form fields
solve this "issue"?#5807
Possibly related issues:
The text was updated successfully, but these errors were encountered: