duannai5858 2015-07-27 23:54
浏览 29
已采纳

$ form-> isValid()之后的Symfony数据转换器

I ran into a trouble of understanding how exactly data is processed in DataTransformers of Symfony.

I have a just a password form. Just one field. This field belongs to the User entity which has to constraints defined in .yml file.

Software\Bundle\Entity\User:
    password:
        - NotBlank: ~
        - Length:
            min: 6
            max: 155

The validation works fine, as supposed to. The problem arise when password must be encoded automatically from the field. So,

    $builder->add('password', 'password', [
        'label' => 'word.password'
    ]);

    $builder->get('password')
        ->addModelTransformer(new EncodePasswordTransformer());

And the transformer itself:

class EncodePasswordTransformer implements DataTransformerInterface
{
    public function transform($value)
    {
        return $value;
    }

    public function reverseTransform($value)
    {
        // encode password
        return PasswordHash::createHash($value);
    }
}

So here's what's happening:

The form should contain 6 to 155 characters but $form->isValid() is always true, because the PasswordHash::createHash($value) encodes the password to 32 characters. What I was expecting is:

Form validates raw password, if more than 6 chars then go to $form->isValid() true and then encode the password after it's validated.

I know I can just encode the password manually while the form is valid without using DataTransformer but I was hoping for a bit more elegant way.

Am I wrong?

  • 写回答

1条回答 默认 最新

  • duan20145 2015-07-28 06:18
    关注

    You can't, according to the documents.

    Symfony's form library uses the validator service internally to validate the underlying object after values have been submitted.

    So you're not actually validating form, but the object underneath which has no "notion" of the plain password.
    A not so elegant solution would be to include a plain password field on your user and not persist it. However you probably won't be able to validate your existing user objects (e.g: in an update form), since their plain password fields will be null. To get around that you could create a custom validator that checks the validity of the $plainPassword field only if the user is not new. You could check that by using doctrine's UnitOfWork or by checking if the id of the user is null.
    I suggest you also take a look at FOSUserBundle it has (or had) a similar approach to the plain password field and might have what you're looking for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么