I am building a user registration page for my site. I have a FormModel for the registration web form and a ActiveRecord class for the account. After the form is valiated, its attributes are massively assigned to newly created AR instance and second one is saved.
I have 2 questions:
Where is right to validate?
Field like "passowrd", "email" and "username" can be validate in the AR class and also in the FormModel. Where is better?
How to bubble the validation errors?
The uniqueness of username is validated in the AR model. When there is already same username in the database, I need to show error. But I am passing the form model to the view. Is there any kind of "shortcut" to pass the errors from the AR model to the FormModel? Or - is there any undocumented way to check for uniqueness in the FormModel? (the documentation says "no" to this one)…
If I extend described scenario: Let's say I have Account AR class, and a Profile AR class. They are related like this: "Profile has one account".
At the registration page, for simplicity, I let the user enter his first and family name and username, password and email address. First two - of the Account class, and the last three belongs to the Profile class.