0
votes

I'm trying to figure out how to build a validation rule, and the simplest example I can give would be a class signup with students emails. I want each class to be able to have unique student emails signup, but I also want students to be able to sign up with the same email for multiple classes.

Class hasMany Students (id, name, limit)

Student belongsTo Class (id, email, class_id)

Example Scenario:

Class A, has student signup: [email protected] (If [email protected] signed up again for this class, it would not allow.)

Class B, could have student signup: [email protected] again since it's a different class.

So is there a way to use isUnique in combination with another rule, or is this a custom validator situation? Thanks!

1

1 Answers

0
votes

I think I have to answer a different question here, because your model association doesn't make sense.

If any one Student can take multiple classes and a Class can have multiple students, then your association needs to be hasAndBelongsToMany. Your students table, then, would not have a class_id. You would instead set up a join table, classes_students, which would have a class_id and student_id.

Then you could add a validation rule on the join table to make sure no student is taking the same class twice. See this Making HABTM relationships unique in CakePHP question.