symfony - Operation of whether there is form field or there is no form field in Symfony2 -


formtype

class articletype extends abstracttype {     public function buildform(formbuilderinterface $builder, array $options)     {         $builder->add('name', 'text');         $builder->add('status', 'choice', array(             'choices' => array(                 'a' => 'a',                 'b' => 'b',                 'c' => 'c',             ),             'required' => true         ));          // ...     }      // ... } 

view : no problem.

{{ form_widget(form.status) }} {{ form_widget(form.name) }} <input type="submit" value="submit" /> 

view : problem case.

{% if is_granted("is_authenticated_remembered") %}     {{ form_widget(form.status) }} {% endif %} {{ form_widget(form.name) }} <input type="submit" value="submit" /> 

it has register blank status value if not login.
intention no change status value if there no status field.

do have switch "formtype" in case?

you have check role in form builder too, , have 2 solutions that.

solution 1

the elegant create custom form type service, depending on security context:

class articletype extends abstracttype {     private $securitycontext;      public function __construct(securitycontext $securitycontext)     {         $this->securitycontext = $securitycontext;     }      public function buildform(formbuilderinterface $builder, array $options)     {         $builder->add('name', 'text');          // if user granted         if($this->securitycontext->isgranted('is_authenticated_remembered'))         {             $builder->add('status', 'choice', array(                 'choices' => array(                     'a' => 'a',                     'b' => 'b',                     'c' => 'c',                 ),                 'required' => true             ));         }          // ...     }      // ... } 

mark form type service :

services:     form.type.article:         class: foo\barbundle\form\type\articletype         arguments: ["@security.context"]         tags:             - { name: form.type, alias: article_type } 

now, instead of calling new articletype() in controller, call new service :

$form = $this->createform($this->get('form.type.article'), $data); 

solution 2

the second solution pass security context articletype, no need create service. in controller :

$form = $this->createform(new articletype($this->get('security.context')->isgranted('is_authenticated_remembered')), $article); 

and in form type :

class articletype extends abstracttype {     private $isgranted;      public function __construct($isgranted)     {         $this->isgranted = $isgranted;     }      public function buildform(formbuilderinterface $builder, array $options)     {         $builder->add('name', 'text');          // if user granted         if($this->isgranted)         {             $builder->add('status', 'choice', array(                 'choices' => array(                     'a' => 'a',                     'b' => 'b',                     'c' => 'c',                 ),                 'required' => true             ));         }          // ...     }      // ... } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -