Symfony 2:首次保存后将字段设置为只读

时间:2014-06-25 06:04:58

标签: symfony doctrine-orm

我有一个Symfony 2实体。当我创建一个新记录时,我必须使用表单填充所有值,但在保存之后,当我更新其他成员时,其中一个值$ amount不应该是可更新的。

我怎样才能做到这一点?可以在运行时将表单成员标记为只读吗?

1 个答案:

答案 0 :(得分:1)

在创建表单时使用validation_groupsname选项,您可以更改表单。
name属性设置表单创建,validation_groups负责验证 例如,在控制器的create / new方法中;

public function createAction(Request $request)
{
    // Instantiate new Foo object
    $client = new Foo();

    // create the form (setting validation group)
    $form = $this->formFactory->create('foo', $foo, array(
        'name' => 'create',
        'validation_groups' => array('create')
            )
    );

    // form has been submitted...
    if ('POST' === $request->getMethod()) {
        // submits the form
        $form->handleRequest($request);

        // do validation
        if ($form->isValid()) {
            // do whatever
        }
    }

    // either GET or validation failed, so show the form
    return $this->template->renderResponse('FooBundle:foo:add.html.twig', array(
                'form' => $form->createView(),
                'foo' => $foo
    ));
}

在控制器的编辑/更新功能中;

public function updateAction($id, Request $request)
{
    // Instantiate Client object
    $client = new Foo($id);

    // create the form (setting validation group)
    $form = $this->formFactory->create('foo', $foo, array(
        'name' => 'update',
        'validation_groups' => array('update')
    ));

    // form has been submitted...
    if ('POST' === $request->getMethod()) {
        // submits the form
        $form->handleRequest($request);

        // do validation
        if ($form->isValid()) {
            // do whatever
        }
    }

    // either GET or validation failed, so show the form
    return $this->template->renderResponse('FooBundle:foo/edit:index.html.twig', array(
                'form' => $form->createView(),
                'foo' => $foo
    ));
}

你的表格类型看起来像;

class FooType extends BaseAbstractType
{
    protected $options = array(
        'data_class' => 'FooBundle\Model\Foo',
        'name'       => 'foo',
    );

    private $roleManager;

    public function __construct($mergeOptions = null)
    {
        parent::__construct($mergeOptions);
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->$options['name']($builder, $options);
    }

    private function create(FormBuilderInterface $builder, array $options)
    {
        // ID
        $builder->add('Id', 'text', array(
            'required' => true,
            'label' => 'ID',
            'attr' => array(
                'placeholder' => 'Format: 2 alphanumeric (e.g. A1)'
            )
        ));

        // Name - only show on create
        $builder->add('Name', 'text', array(
            'required' => true,
            'label' => 'Name',
            'attr' => array(
                'placeholder' => 'Your name'
            )
        ));

        // add the submit form button
        $builder->add('save', 'submit', array(
            'label' => 'Save'
        ));
    }

    private function update(FormBuilderInterface $builder, array $options)
    {
        // ID
        $builder->add('Id', 'text', array(
            'required' => true,
            'label' => 'ID',
            'attr' => array(
                'placeholder' => 'Format: 2 alphanumeric (e.g. A1)',
            )
        ));

        // Name - just for show
        $builder->add('Name', 'text', array(
            'required' => true,
            'label' => 'Name',
            'attr' => array(
                'readonly' => 'true' // stops it being editable
            )
        ));

        // add the submit form button
        $builder->add('save', 'submit', array(
            'label' => 'Save'
        ));
    }
}

P.S。我的所有类都被声明为服务,所以你如何调用create forms / views / etc可能会有所不同。