简体   繁体   English

Symfony2在绑定上丢失表单数据

[英]Symfony2 losing form data on bind

I came across an issue with Symfony2 forms that I can't figure out, namely, on the form there are 5 checkboxes for user roles. 我遇到了一个我无法弄清楚的Symfony2表单问题,即在表单上有5个用于用户角色的复选框。 These are defined in an entity as constants and added to the form type, like such: 这些在实体中定义为常量,并添加到表单类型中,例如:

public static $GENERAL_ROLES = array(
    "ROLE_ADMIN" => "ROLE_ADMIN",
    "ROLE_USER" => "ROLE_USER",
    "ROLE_WORKER" => "ROLE_WORKER",
    "ROLE_PM" => "ROLE_PM",
    "ROLE_PM_MANAGER" => "ROLE_PM_MANAGER"
    );

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('username', null, array('required' => true, 'label' => "Login"))
        ->add('expiresAt', 'date', array(
            'label' => "End account date"
            'required' => false,
            'widget' => 'single_text',
            'format' => 'dd-MM-yyyy',
            ))
        ->add('plainPassword', 'repeated', array(
            'type' => 'password',
            'options' => array('translation_domain' => 'FOSUserBundle'),
            'first_options' => array('label' => 'Mot de passe'),
            'second_options' => array('label' => 'Confirmation'),
            'invalid_message' => 'fos_user.password.mismatch',
        ))
        ->add('roles', 'choice', array(
            'label' => 'Role',
            'mapped' => true,
            'expanded' => true,
            'multiple' => true,
            'attr' => array('class' => 'control-label'),
            'choices' => \FullSix\ProjectForecastBundle\Entity\Login::$GENERAL_ROLES
          ))
    ;
}

So far so good, the choices appear on the form. 到目前为止,所有选择都显示在表单上。 When I submit with all roles checked, if I check $_POST, all the roles are there, all good. 当我提交所有角色时,如果我检查$ _POST,则所有角色都在那里,一切都很好。 However, when I do $form->bind($request), things start to go south. 但是,当我执行$ form-> bind($ request)时,事情开始往南走。 In the validator, 在验证器中,

class LoginValidator extends ConstraintValidator
{
    public function validate($login, Constraint $constraint)
    {
        if ($login->getPlainPassword() || $login->getUsername() || $login->getRoles() || $login->getExpiresAt()) {
            if (!$login->getPlainPassword()) {
                $this->context->addViolation($constraint->noPasswordMessage);
            }
            if (!$login->getUsername()) {
                $this->context->addViolation($constraint->noUsernameMessage);
            }
            if (!$login->getRoles()) {
                $this->context->addViolation($constraint->noRole);
            }         
        }
    }
}

only 4 of the 5 roles are still here, specifically all except ROLE_USER. 5个角色中只有4个仍然在这里,尤其是ROLE_USER以外的所有角色。 The funny thing is, if I change the value from ROLE_USER into anything else, it works fine. 有趣的是,如果我将值从ROLE_USER更改为其他任何值,它都可以正常工作。 It only breaks with this particular value. 它仅与此特定值一起中断。

I couldn't find any info on weather symfony has something against using this name, or what. 我找不到有关天气symfony的任何信息,因此无法使用此名称或其他名称。

Anyway, I'm really curious why this issue occurs, so if anyone could enlighten me that would be great :). 无论如何,我真的很好奇为什么会发生此问题,所以如果有人能启发我,那将是很棒的:)。 Thank you. 谢谢。

It's as Cerad said, because the entity extended FOSUserBundle:User and this means it will always have the role user, it filtered out the ROLE_USER from the form. 就像塞拉德所说的那样,因为实体扩展了FOSUserBundle:User,这意味着它将始终具有角色user,因此它从表单中滤除了ROLE_USER。 Thank you for your answer. 谢谢您的回答。 In the end my colleague who was working on this went with another approach but I was still curious :) 最后,我为此工作的同事采用了另一种方法,但是我仍然很好奇:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM