Symfony2表单类型 - 如何设置表单CSS类作为属性? [英] Symfony2 Form Type - how to set form CSS class as attribute?

查看:114
本文介绍了Symfony2表单类型 - 如何设置表单CSS类作为属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在表单控制器中设置CSS类,并将其传递为$ options数组元素。

 私有函数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-horizo​​ntal'



return $ form;
}

但是当我想使用formType获得相同的效果时, :

  public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
// not works
- > setAttribute('attr',array('class'=>'form-horizo​​ntal'))
//无效
- > setAttribute('class','form-horizo​​ntal')
// ...

我做错了什么?如何使其工作?

解决方案

这可以从几个地方完成:


  1. 在Form Type类的 setDefaultOptions 方法中定义

  2. 作为 $ this- < / strong>方法中的 buildView 方法中定义

  3. < li>在模板中呈现表单

您只需知道哪个选项优先于其他选项,

选项1:此处是设置默认选项的位置。



示例实现(

提供) :

  / ** 
* @param OptionsResolverInterface $ resolver
* /
public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'attr'=> array(
'class'=> -from-default'
),
));
}

选项2:可以提供attr值,同时从控制器调用 $ this-> createForm (这实际上是 $ this-> container-> get('form.factory') - > create function)。当您提供此值时,将覆盖从上一个选项设置的值。



示例实现( 提供):

  $ this-> createForm($ formTypeObject,$ entity,array(
'action'=>'URL,
'attr'=> array ('class'=>'form-horizo​​ntal')
));

选项3:您可以设置或覆盖前两个选项。在 buildView 方法中设置值。在您的表单类型类中声明以下方法:

  / ** 
* {@inheritdoc}
* /
public function buildView(FormView $ view,FormInterface $ form,array $ options)
{
$ options ['attr'] ['class'] ='form-horizo​​ntal-from-构建视图
//如果你想保留从以前的方法设置值,你可以定义像
// $ options ['attr'] ['class'] = isset($ options ['attr'] [类'])? $ options ['attr'] ['class']:'';
// $ options ['attr'] ['class']。='form-horizo​​ntal-from-build-view';

$ view-> vars = array_replace($ view-> vars,array(
'attr'=> $ options ['attr'],
)) ;
}

选项4:方式设置属性。这具有高度优先于其他选项,因为它是在渲染表单的最后阶段。



您可以在模板中定义如下:

  {{ form_start(form,{'method':'POST','attr':{'class':'form-horizo​​ntal-ultimate'}})}} 


When I set CSS class in form controller and pass it as $options array element it 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 I want to have same effect using formType it is not added to 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 I do wrong? How to make it work?

解决方案

This can be done from several places:

  1. define in setDefaultOptions method of Form Type class
  2. As a parameter of $this->createForm function called from controller.
  3. define in buildView method of Form Type class
  4. In template while rendering form

You just to know which option has precedence over another, and how they work.

Option 1: Here is the place to set default options. If no where else set the value this default value will be 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: You already know this, you can provide the attr value while calling $this->createForm from controller(which is actually a shortcut for $this->container->get('form.factory')->create function). When you provide this value, the value set from previous option will overridden.

Example Implementation (As You provided):

$this->createForm($formTypeObject, $entity, array(
    'action' => 'URL,
    'attr'=>array('class'=>'form-horizontal')
));

Option 3: You can set or override the value set by previous two option here. setting the value in buildView method. Declare following method in your Form Type Class:

/**
 * {@inheritdoc}
 */
public function buildView(FormView $view, FormInterface $form, array $options)
{              
    $options['attr']['class'] = 'form-horizontal-from-build-view';
    //If you like to keep value set from previous methods you can define like
    //$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:: Now the final and ultimate way to set attributes. This has the height priority over other options, as it is done in the final stage while rendering the form.

You can define as follow in your template:

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

这篇关于Symfony2表单类型 - 如何设置表单CSS类作为属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆