douying0108 2014-02-20 13:34
浏览 24
已采纳

Symfony 2验证

i have a question regarding symfony 2 validation via an Entity and a validation configuration file. Here is the explanation of the problem.

I have an entity for the users account settings which has 5 properties - subdomain, email, oldPassword, password and new_password_confirmed.

My configuration file is as follows:

Backend\Builders\PageBundle\Entity\AccountSettings:
properties:
    email:
        - Email: ~
    oldPassword:
        - Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
            message: "Wrong value for your current password"
constraints:
        - Expression:
            expression: "this.passwordMatch()"
            message: "Passwords don`t match"  

My question is - How can i use this entity for validation and choose to perfom the validation only on a certain item, for example the email . Here is my code in the moment for this validation, but it requires all the validation rules to be met in order the validation to be successfull:

    $accountSettings = new AccountSettings();
    $accountSettings->email = $_POST['email'];
    $validator = $this->get('validator');
    $errors = $validator->validate($accountSettings);
  • 写回答

1条回答 默认 最新

  • dongsui8162 2014-02-20 13:41
    关注

    You should then use validation groups. It allows you to do,

    Backend\Builders\PageBundle\Entity\AccountSettings:
    properties:
        email:
            - Email: { groups: [xxx_group] }
        oldPassword:
           # ...
    

    $errors = $validator->validate($accountSettings, array('xxx_group'));
    

    Check the example provided in the documentation.

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

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭