php - Symfony2 Form ChoiceType External Datasource -


i have form class several choicetype fields contain array of options key:value pair. when form submitted value saved. when i'm rendering object show key value instead.

example: 'monthly' => '1month'. 1month stored, prefer output monthly.

i'm trying avoid conditionals check value , changing output key value.

i wasn't able find documentation best practices sort of thing. i'm thinking creating service stores choice options arrays , build twig filter changing rendered output based on array service.

am on right track or there easier way?

i tried service solution , got working. i'm not sure if elegant or efficient way did job. form form class type , injected service contained choice arrays.

i created choices.php class file inside form folder next formtype file. acts service returns choices formtype , custom twig extension filter created. formtype had set service in order inject choices service.

/*choices.php*/ public function getchoices($choice) {     $choices = array('paymentfrequency' => array('monthly' => '1month',                                                  'bi-weekly' => '2weeks'),                      'compounding' => array('monthly' => 'monthly',                                             'daily' => 'daily')     );      return $choices[$choice]; }  /*formtype.php*/ ->add('paymentfrequency', choicetype::class, array(             'label' => 'payment frequency:',             'choices' => $this->choicesservice->getchoices('paymentfrequency'),  )) ->add('compounding', choicetype::class, array(             'label' => 'compounding:',             'choices' => $this->choicesservice->getchoices('compounding'),  )) 

i created custom twig filter function choices service injected it.

/*twigextension.php*/ public function renderchoicesfilter($value, $type) {      $choices = $this->choicesservice->getchoices($type);      return array_search($value, $choices); }  /*twig template*/ {{ object.paymentfrequency|renderchoices('paymentfrequency') }} 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -