Symfony2 form validation based on two fields

前端 未结 5 2053
醉梦人生
醉梦人生 2020-12-05 05:04

I am currently developing a Website in which user may buy gift cards. I am using a three step form using the CraueFormFlow bundle and everything is concerning the steps. I a

5条回答
  •  既然无缘
    2020-12-05 05:44

    You have many solutions for this.

    The easiest one is to add a Callback constraint to your model class.

    Another way to do it would be to create your custom constraint and its associated validator. You have a cookbook explaining how to create a custom validation constrain. This is the best approach to do it.

    As your constraint does not apply to a property but to a class, you must specify it overriding the the ->getTargets() method of your constraint class:

    class MyConstraint extends Constraint
    {
        // ...
    
        public function getTargets()
        {
            return Constraint::CLASS_CONSTRAINT;
        }
    }
    

    So the value passed as $value argument of the ->isValid() method will contain values of the whole class and not only of a single property.

提交回复
热议问题