I created the following array of nested forms:
return array( 'elements' => array( 'contact' => array( 'type' => 'form', 'elements' => array( 'first_name' => array( 'type' => 'text', ), 'last_name' => array( 'type' => 'text', ) ), ), 'lead' => array( 'type' => 'form', 'elements' => array( 'primary_skills' => array( 'type' => 'textarea', ), ), ), ), 'buttons' => array( 'save-lead' => array( 'type' => 'submit', 'label' => 'Create', 'class' => 'btn' ), ) );
I have a watch page like this
echo $form->renderBegin(); echo $form['lead']; echo $form['contact']; echo $form->buttons['save-lead']; echo $form->renderEnd();
my actionCreate is like this
$form = new CForm('application.views.leads.register'); $form['lead']->model = new Lead; $form['contact']->model = new Contact; // how can i perform ajax validation only for $form['contact'] $this->performAjaxValidation($model); //if contact form save btn is clicked if ($form->submitted('save-lead') && $form['contact']->validate() && $form['lead']->validate() ) { $contact = $form['contact']->model; $lead = $form['lead']->model; if ($contact->save()) { $lead->contact_id = $contact->id; if ($lead->save()) { $this->redirect(array('leads/view', 'id' => $lead->id)); } } }
Ajax validation method
protected function performAjaxValidation($model) { if (isset($_POST['ajax']) && $_POST['ajax'] === 'contact') { echo CActiveForm::validate($model); Yii::app()->end(); } }
so my question is how can I do ajax check on $ form ['contact'] and $ form ['lead'] elements separately?
source share