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

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


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


Your limitation is not Symfony, your limitation is HTML and HTTP. Unfortunately, once the HTTP request is fulfilled, once that data is sent to the browser, there is nothing a server can do to change what is rendered (well, almost nothing, there is always Skynet). The only option is JavaScript (and that can do a lot if they're not running Lynx).

I saw your question on Google Groups and based on the combination of both of them, I can tell you that you only have two options.

  • You can make the option appear as a response to the first response you have from the browser.
  • You can use JavaScript and then handle any failures on the server side.

Your best bet? I think the users will appreciate the JavaScript option. It is good policy to validate user information server-side anyway. Obviously let the user know as soon as possible by validating with JavaScript, but you'll need to be checking their input on the server anyway.

By the way, to disable a form field in Symfony, step-by-step instructions are here.