When using the buildForm-> add () function in Symfony2, What are the acceptable options?

I looked through the documentation and, if I didn’t miss it, I can’t find anything explaining that the official $ options for the buildForm-> add () function are in Symfony2.

public function buildForm(FormBuilder $builder, array $options) { $builder->add('fieldname1'); $builder->add('fieldname2', new formObjectType(), $arrayOptions); } 

Taking the code above, which parameters will be passed as an array for the second field.

thanks

+4
source share
2 answers

The same question bothered me. The default parameters are written inside class classes. Take DateType as an example.

DateType::getDefaultOptions() lists all the default parameters if you yourself do not define them. In addition, we have DateType::getAllowedOptionValues() - it determines which values ​​are valid for certain parameters.

Note that all classes are extended by AbstractType , and in addition to this inheritance, each "type" implements FormTypeInterface::getParent() . For DateType parent of the FieldType . FieldType is obviously the parent class for most fields, and it also has several default options. I assume that all of these options are combined together, invoking a particular type of form.

+3
source

These parameters are passed to the field type, in your case formObjectType . Thus, it really depends on what options are used in this area. For example, say you want to pass an option to show the formObjectType display or an formObjectType field. You can do something like this:

 // Application/AcmeBundle/Form/Type/FormObjectType.php class FormObjectType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $this->add('name', 'text'); if ($options['display_custom_field'] === true) { $this->add('name_custom', 'text'); } } public function getDefaultOptions(array $options) { return array( 'display_custom_field' => false, ); } } // Application/AcmeBundle/Controller/FormController.php class FormController extends Controller { public function createForm($object) { return $this->getFormFactory()->create(new FormObjectType(), $object, array( 'display_custom_field' => true, )); } public function customAction() { $form = $this->createForm(); // Code here ... } } 

If the parameter is an array, passed or missing array is passed at all, the default value is set to formObjectType . Thus, this array is used to configure the parameters expected from this type. This also works with a built-in type like text , date , etc.

Hope this helps.

Yours faithfully,
Matt

PS You must start your class name with an uppercase letter: formObjectType instead of formObjectType to distinguish variables and methods from class names. This is just a suggestion :)

+4
source

All Articles