Conditional Validation Server Side

Conditional Validation Server Side

BoSmithBoSmith Posts: 23Questions: 6Answers: 0

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

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin
    Answer ✓

    The way to do this is with a server-side event (specifically the preEdit or preCreate 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

  • BoSmithBoSmith Posts: 23Questions: 6Answers: 0

    Thank Allan, this makes perfect sense. I will do it that way,
    Thanks.

This discussion has been closed.