php - Adding a 'other, please specify' option to my ChoiceType form field in Symfony -
i'm trying create form field set of choices text input needs filled out if choose 'other':
how exercise? (*) not exercise @ moment ( ) once month ( ) once week ( ) once day ( ) other, please specify: [ ]
currently, i'm using choicetype
have set choices
this:
$form->add('exercise', type\choicetype::class, array( 'label' => 'how exercise?', 'choices' => [ 'i not excerise @ moment' => 'not', ... ], 'expanded' => true, 'multiple' => false, 'required' => true, 'constraints' => [ new assert\notblank() ], ));
how 'other, please specify' option work expected?
in case need create custom form type combination of choicetype
, texttype
. nice intro custom form types can find id doc: http://symfony.com/doc/master/form/create_custom_field_type.html
this should similar to:
class choicewithothertype extends abstracttype { /** * {@inheritdoc} */ public function buildform(formbuilderinterface $builder, array $options) { // prepare passed $options $builder ->add('choice', type\choicetype::class, $options) ->add('other', type\texttype::class, $options) ; // requires custom modeltransformer $builder->addmodeltransformer($transformer) // constraints can added in listener $builder->addeventlistener(formevents::pre_set_data, function (formevent $event) { // ... adding constraint if needed }); } /** * {@inheritdoc} */ public function buildview(formview $view, forminterface $form, array $options) { // if needed } /** * {@inheritdoc} */ public function configureoptions(optionsresolver $resolver) { // } ));
please take at:
- chapter data transformers: http://symfony.com/doc/current/form/data_transformers.html
- dynamic form modifications: http://symfony.com/doc/current/form/dynamic_form_modification.html
i think best way achieve take @ source code of datetimetype.
Comments
Post a Comment