Use custom validation.
Declare a special validator in your rules and define the required validation in the verification method.
public function rules() { return array( array('contacts', validateContacts), ); } public function validateContacts($attribute,$params) { if (length($this->contacts) == 0) { $this->addError($attribute, 'You must add at least one contact!'); } foreach($this->contacts as $contact) {
In the controller, assign an array of contacts in the Model field and call the model validation method. If there are any errors, they will be displayed line by line.
<?php echo $form->error($model,'contacts'); ?>
in view.
The controller contains code for invoking verification.
$contactModel = new Contact; // assign the array of contacts to the model $contactModel->contacts = $POST['myForm]['contacts'] $contactsModel->validate(); $this->render('myform', contactModel);
If you want validation to be done through Ajax, you need to specify this when creating your form:
$form=$this->beginWidget('CActiveForm', array( 'id'=>'top-websites-cr-form', 'enableAjaxValidation'=>true, 'clientOptions' => array( 'validateOnSubmit'=>true, 'validateOnChange'=>true), ));
In this case, your controller can check the ajax forms.
if(isset($_POST['ajax']) && $_POST['ajax']==='branch-form') { echo CActiveForm::validate($model); Yii::app()->end(); }
links: http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/