Custom Attribute Validation

Hi, let me explain the scenario in few lines:

I have two tables 1- Users, 2- Groups. Each group has a code that a student can enter in, to join the Group.

On the signup form, I have Group Code field along with other User model fields like username, password etc.

This Group Code field is not a part of Users model. Instead, it is an attribute of Groups model.

Now, my question is when student submits the SignUp form, how it is possible to validate that "Group Code" field??

We can use "onAfterValidate()" method to put logic to validate the Group Code entered by user but in this case, how can I return error to Group Code field on the signup form??

Thanks

Well its not a big problem to validate a field according to your own requirement.

Create a custom validation rule for it. you can get more information about custom validation rule on this link :)

I have already gone through this link, but take a look at how we add error:


$this->addError($attribute, 'error description');

This will work only when Group Code is an attribute of Users model!

BTW, I tried this, it returns just a blank screen!

Thanks

Ahh then why you dont define a custom attribute in your model class like public $group_code; and overwrite this value according to your requirements when you save the form.In this way you can easily ass a custom validation rule and also save the data.

I hope you got my point.