Symfony2 Form Type - how to set form CSS class as attribute? -


when set css class in form controller , pass $options array element works.

private function createeditform(ordercard $entity) {     $form = $this->createform(new ordercardtype($this->get('security.context')                                        ->isgranted('role_super_admin')), $entity, array(         'action' => $this->generateurl('ordercard_update', array('id' => $entity->getid())),         'attr'=>array(                  'class'=>'form-horizontal'                 )     ));      return $form; } 

but when want have same effect using formtype not added form:

public function buildform(formbuilderinterface $builder, array $options) {       $builder           // not works           ->setattribute('attr', array('class' => 'form-horizontal'))           // not works either           ->setattribute('class', 'form-horizontal') //... 

what wrong? how make work?

this can done several places:

  1. define in setdefaultoptions method of form type class
  2. as parameter of $this->createform function called controller.
  3. define in buildview method of form type class
  4. in template while rendering form

you know option has precedence on another, , how work.

option 1: here place set default options. if no else set value default value used.

example implementation (as pivot provided):

/**  * @param optionsresolverinterface $resolver  */ public function setdefaultoptions(optionsresolverinterface $resolver) {     $resolver->setdefaults(array(         'attr' => array(             'class' => 'form-horizontal-from-default'         ),     )); } 

option 2: know this, can provide attr value while calling $this->createform controller(which shortcut $this->container->get('form.factory')->create function). when provide value, value set previous option overridden.

example implementation (as you provided):

$this->createform($formtypeobject, $entity, array(     'action' => 'url,     'attr'=>array('class'=>'form-horizontal') )); 

option 3: can set or override value set previous 2 option here. setting value in buildview method. declare following method in form type class:

/**  * {@inheritdoc}  */ public function buildview(formview $view, forminterface $form, array $options) {                   $options['attr']['class'] = 'form-horizontal-from-build-view';     //if keep value set previous methods can define     //$options['attr']['class'] = isset($options['attr']['class'])? $options['attr']['class'] :'';     //$options['attr']['class'] .= ' form-horizontal-from-build-view';      $view->vars = array_replace($view->vars, array(         'attr'  => $options['attr'],     )); } 

option 4:: final , ultimate way set attributes. has height priority on other options, done in final stage while rendering form.

you can define follow in template:

{{ form_start(form, {'method': 'post', 'attr': {'class': 'form-horizontal-ultimate' }}) }} 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -