Conditional validation of fields based on other field value in Symfony2

前端 未结 2 1884
花落未央
花落未央 2021-01-04 14:02

So here is the scenario: I have a radio button group. Based on their value, I should or shouldn\'t validate other three fields (are they blank, do they contain numbers, etc)

2条回答
  •  花落未央
    2021-01-04 14:20

    I suggest you to use a callback validator.

    For example, in your entity class:

    getRadioSelection() == '1' // RADIO SELECT EXAMPLE
                    &&
                    ( // CHECK OTHER PARAMS
                     $this->getFiled1() == null
                    )
                )
            {
                $context->addViolation('mandatory params');
            }
           // put some other validation rule here
        }
    }
    

    Otherwise you can build your own custom validator as described here.

    Let me know you need more info.

    Hope this helps.

提交回复
热议问题