In Symfony2 form component is it possible to create custom attributes?
The reason why I ask is because I'm working on a certain edge case where read_only will not be sufficient.
Here is the scenario: I need to bind data based on the outcome of some external logic parsing. This is crucial because I may have fields that are disabled by default but based on the external logic the fields may be activated. I cannot use client scripting to produce this outcome, it has to be disabled in the form attribute.
If I start with the field as read_only, it will be disabled, but I will never be able to bind data to it. So given the outcome of my aforementioned external logic, I will not be able to use read_only. So this leaves me with no other option but using a different attribute which will make the field disabled.
Is it possible to create a custom attribute to produced this disabled effect?
I'm not sure I've understood your question correctly; do you want to add arbitrary attributes to your form input tags? For example:
<input type="text" name="myInput" myAttr="myValue" />
If this is what you want to do, then this is possible, like so:
$form = $this->createFormBuilder($someObj)
->add('myInput', 'text', array(
'attr' => array('myAttr' => 'myValue')
)
->getForm();
The documentation is here:
http://symfony.com/doc/2.0/reference/forms/types/field.html
Hard to tell exactly what you're looking to do, but sounds like you want dynamically generated forms based on some event, which is described here:
http://symfony.com/doc/2.0/cookbook/form/dynamic_form_generation.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With