Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create custom form field attributes in Symfony 2?

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?

like image 680
Jesse Greathouse Avatar asked Jun 14 '11 17:06

Jesse Greathouse


2 Answers

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

like image 183
Ian Phillips Avatar answered Nov 14 '22 01:11

Ian Phillips


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

like image 20
robotron2000 Avatar answered Nov 13 '22 23:11

robotron2000