gpt4 book ai didi

Yii CForm,嵌套表单 Ajax 验证

转载 作者:行者123 更新时间:2023-12-04 14:01:13 25 4
gpt4 key购买 nike

我创建了以下嵌套表单数组;

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['contact'] 和 $form['lead'] 元素执行 ajax 验证?

最佳答案

一个页面中可以有多个表单,但它们不应嵌套。
嵌套表单无效。

关于Yii CForm,嵌套表单 Ajax 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15151872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com