Gerrit
Gerrit

Reputation: 2677

How to get generated HTML-ID-Attribute from Symfony2-Form-Elements

I want to receive Error-Messages from a Symfony2-Form via ajax (JSON). Now I get the problem to join the $form_element->getName() with my form-element ID in HTML. Is there a possibility to get this ID from the $form_element-Object?

Upvotes: 1

Views: 1052

Answers (1)

Vadim Ashikhman
Vadim Ashikhman

Reputation: 10136

Yes, simply use

$form->get('field_name')->createView()->vars['id'];

In order to get view related data (like id and class options) from a form, first you have to create its view.

You can also do it the other way:

$formView = $form->createView(); // Creates view for every element of the form
$id = $formView->children['field_name']->var['id'];

Upvotes: 4

Related Questions