I already tried the new form builder but had no luck displaying validation errors. So I tried it the "old fanshioned way" - but also had no luck. Maybe you can help me?
For subscription I have a simple form:
[list=1]
[*]Enter email address (Text input field)
[*]Choose gender (2 Radio buttons)
[*]Choose profile type (OptionList with predefined profiles)
[/list]
I want to validate the input and then insert the data into 3 tables (Account, AccountProfile and Email).This is, because one user can have only one account, but muliple profiles and email addresses.
So my 3 models look like this (simplified):
[Account]
gender_id (related to Gender)
[AccountProfile]
account_id (related to Account)
profile_id (related to Profile)
[Email]
email_address
account_id (related to Account)
So how can I create a form (and later validate it)?
Maybe I’m wrong, but this way I have to define the validation rules for every subscription field twice, right?
Once in the form itself, and once more in the affected models (Account, Email, Gender, Profile, AccountEmail and AccountProfile).
If for example the email address has to be unique (which actually is the case), I can’t check this using the CFormModel. So the first time this error appears, would be in the last line of your example, after a new account is already created and I’m failing to save the email address.
I know: This could be solved opening a transaction and executing a rollback. But to display the correct error message for the email field, I’d also have to manually assign the error provided by the “Email”-model to the related field, right?
public function rules()
{
return array(
array('email', 'checkEmail'),
);
}
public function checkEmail($attribute,$params)
{
if(!$this->hasErrors()) {
$address = Email::model()->find('address = ?', array($this->address));
if ($address) {
$this->addError('address', 'Email has already been taken');
}
}
}
$form->attributes = Yii::app()->request->getParam('Subscribe');
if ($form->validate()) ...
It would check if
all fields required for the subscribe scenario are filled out
gender_id is a valid id from the genders table (f or m)
profile_id is a valid id and activated in the profiles table
email_address is not in the emails table and is a valid email address
I know now how to do this manually, but this simple scenario needs me to write 3 custom validation functions doing the same what the rules in the models already do.
Every time I change something in a model I have to remember that there’s a bunch of custom validation functions I also have to adapt.
Thank you anyway. If I found what I was looking for I’ll update this thread … maybe I’ll give the form builder another chance