我已經創建了以下嵌套表單數組;Yii CForm,嵌套表單Ajax驗證
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'
),
)
);
我有鑑於這樣的
echo $form->renderBegin();
echo $form['lead'];
echo $form['contact'];
echo $form->buttons['save-lead'];
echo $form->renderEnd();
我actionCreate頁是這樣
$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驗證方法是
protected function performAjaxValidation($model)
{
if (isset($_POST['ajax']) && $_POST['ajax'] === 'contact') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
所以我的問題是我如何能在$ form ['c。]上執行ajax驗證ontact']和$ form ['lead']個別元素?
你已經解決了這個了嗎?這裏有同樣的問題。 – MEM 2013-04-24 16:34:40
您可以向提交按鈕添加一個名稱,並檢查它是否存在於您的控制器中。 ();} 然後在你的控制器中if(isset($ _ REQUEST ['submit-contact'])){echo CActiveForm :: validate($ contact); YII ::應用程序() - >端(); }' – dimvic 2013-11-25 00:33:43