php – 如何禁用一个UserType而不是另一个?

好吧,这开始让我爬上墙,我无法做到这一点,所以希望有人可以提供帮助!

我使用的是Symfony 2.3,我在’UserType’类中构建了我的用户表单.我有两个地方,一个是应用程序添加新用户时,我希望该字段保持不变.但是我构建了一个新的“UserType”来查看所有用户,因此将app作为管理区域.

因此,我希望能够在我的用户表单上删除我的密码输入中的“必需”,当表单刚刚编辑时,例如电子邮件已更新.

从我所读到的,这在树枝上是不可行的(我不明白为什么!)但我必须在建造者中做到这一点?

1,有没有办法在树枝上做到这一点,最好的选择!
2,我如何只对我的一个’UserTypes’而不是另一个?

所以我的代码如下,

UserType.php

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $RoleOpts = ['ROLE_USER' => 'User','ROLE_ADMIN' => 'Admin'];

        $builder->setMethod('POST');
        $builder->add('username',  'text',    ['label' => 'Username',  'attr' => ['autocomplete' => 'off','class' => 'Admin_Username']]);
        $builder->add('password',  'password',['label' => 'Password',  'attr' => ['autocomplete' => 'off','class' => 'Admin_Pswd']]);
        $builder->add('firstname', 'text',    ['label' => 'First Name','attr' => ['autocomplete' => 'off','class' => 'Admin_Name']]);
        $builder->add('surname',   'text',    ['label' => 'Surname',   'attr' => ['autocomplete' => 'off','class' => 'Admin_Surname']]);
        $builder->add('email',     'email',   ['label' => 'Email',     'attr' => ['autocomplete' => 'off','class' => 'Admin_Email']]);
        $builder->add('userroles', 'choice',  ['label' => 'Role','choices' => $RoleOpts, 'attr' => ['class' => 'Admin_Role']]);
        $builder->add('save',      'submit',  ['label' => 'Save User', 'attr' => ['class' => 'Admin_SaveUser']]);
    }

ViewUsers Action

public function ViewUsersAction() {
    $GetAllUsers = $this->getDoctrine()
                     ->getRepository('Bundle:Users')
                     ->findAll();

    foreach($GetAllUsers as $UserKey => $UserValue) {
        $UserID = $UserValue->getId();

        $ViewAllUsers[$UserKey] = $this->createForm(new UserType(), 
                                                    $GetAllUsers[$UserKey],
                                                    ['action' => $this->generateUrl('Admin_EditUser', ['id' => $UserID])]
                                                    )->createView();
    }

    return $this->render('Bundle:Admin:Users/view.html.twig', ['ViewUsers' => $ViewAllUsers ]);
}

树枝文件

   {% for View in ViewUsers %}

    {{ form_start(View, {'attr': {'class': 'EditUser'}}) }}
        {{ form_errors(View) }}

        {{ form_row(View.username, {'attr': {'disabled': 'disabled'}}) }}
        {{ form_row(View.password, {'attr': {'required': false}}) }}
        {{ form_row(View.firstname) }}
        {{ form_row(View.surname) }}
        {{ form_row(View.email) }}
        {{ form_row(View.userroles) }}

        {{ form_row(View.save, { 'label': 'Update User' }) }}

    {{ form_end(View) }}

{% endfor %}

我已阅读,Force a field to not be required,但是它将它添加到构建器,这将更改添加新用户,并编辑所有用户,这是我不想要的!

有什么帮助吗?

*如果我忘记了您需要查看的任何代码或其他部分,请告诉我们!

最佳答案 第一步是在setDefaultOptions方法中编写默认选项.

第二步是使用buildForm方法中的选项:

class UserType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ...

        $builder->add('password',  'password',['required' => $options['password_required']]);
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\User',
            'password_required' => TRUE
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_user';
    }
}

第三步是从editAction或createEditForm方法(或者在你的情况下是你的ViewUsersAction)调用createForm方法,使用额外的选项’password_required’=>假:

$form = $this->createForm(new UserType(), $entity, array(
    'action' => $this->generateUrl('user_update', array('id' => $entity->getId())),
    'method' => 'PUT',
    'password_required' => FALSE
));
点赞