有没有办法在Symfony2中重命名表单中的字段?

时间:2016-02-24 23:07:16

标签: symfony symfony-forms

我正在使用symfony2,我想知道是否可以重命名表单中的字段。

我的意思是......假设我有一个实体

class MyEntity{
    private $name 
    //more code
}

我为这个实体创建了一个类型:

class MyEntityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\MyEntity'
        ));
    }

    public function getName()
    {
        return 'entity';
    }
}

是否可以在表单中重命名字段name,但映射到name属性有效。类似的东西:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('MySuperName', null, array("mapping" => "name"))
            ;
    }

因此,表单参数名称变为entity[MySuperName]而不是entity[name],但填充实体上的name属性?

1 个答案:

答案 0 :(得分:17)

使用property_path

        public Object getItem(int position) {
            int top, bot, left, right;

            top = rgnH * position; // line 1
            bot = top + rgnH - (position == getCount() - 1 ? 4 : 0); // line 2
            left = rgnNbr * rgnW + (rgnNbr != 0 ? BORDER : 0); // line 3
            right = (rgnNbr + 1) * rgnW + (rgnNbr == 0 || rgnNbr == maxRgn ? BORDER : 0); // line 4
            rect = new Rect(left, top, right, bot);

            Bitmap bitmap = bitmapDecoder.decodeRegion(rect, op);

            if (inBitmap == null) {
                inBitmap = bitmap;
                op.inBitmap = inBitmap;
            }

            return Bitmap.createScaledBitmap(bitmap, finalW, finalH, true);
        }

另一种选择是将get / set别名添加到$builder ->add('MySuperName', null, array('property_path' => 'name')) 字段:

name