dqsot35145 2015-09-09 17:06
浏览 42

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

ok, this is starting to drive me up the wall that I can not do this, so heres hoping someone can help!

I am using Symfony 2.3 and I have built up my user form in an 'UserType' class. I have this in two places, one is for when the app adds a new user, which I want the field to stay as it is. However I build a new 'UserType' to view all users, so the app as a management area.

So I want to be able to remove 'required' from my password input on my users form, when a form is just edited, e.g. the email is updated.

From what I have read, this is not doable in twig (which I don't understand why!) but I have to do it in the builder?

1st, is there a way to do this in twig, best option! 2nd, how do I do this to just one of my 'UserTypes' and not the other?

So my code as follows,

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 ]);
}

Twig File

   {% 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 %}

I have read, Force a field to not be required, but that adds it to the builder, which would change both add new user, and edit all users, which i don't want!

Any help, please?

*If I have forgotten any code or other sections you need to see, please let me know!

  • 写回答

2条回答 默认 最新

  • doutui2883 2015-09-09 19:47
    关注

    It might differ in v2.3, as im not sure if this is already supported, but you could add an event listener on The pre-set-data event. Here you can check if the given data (your entity) in the builder has an id or not. When it has one, you are editting the data. If it has no id, you are adding a new user. So with this info, you could easily remove the required option.

    There is lots of info on this at http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题