Conditional Validation Server Side
Conditional Validation Server Side
Hi Allan,
I have a table in which I want to perform a conditional validation based on another field value in the same row. In this table are two fields users.student_number and users.role.
Field::inst('users.student_number')
->validator('Validate::unique')
->validator('Validate::notEmpty'),
This is the validation I want if users.role is student, but if users.role is teacher, etc., then I do not want to validate this field.
I have been attempting custom validators and the use of functions but I cannot seem to figure out how to use your built in validators at the same time. Is this possible?
Thanks for all of your help in the past,
Bo
This question has an accepted answers - jump to answer
Answers
The way to do this is with a server-side event (specifically the
preEdit
orpreCreate
methods). You could perhaps whatever logic check you need on the inbound data and decide if you need to add the validator or not.Allan
Thank Allan, this makes perfect sense. I will do it that way,
Thanks.