Yii的 - 阿贾克斯形式与验证形式、Yii、阿贾克斯

2023-09-10 18:59:21 作者:占有欲与你.

我是Yii的新手,我有以下问题。

I am a Yii Newbie, and I have the following problem.

我有一个要像一个管理的一种形式 - 后端的形式。这将有按键分别具有在控制器类自己的行动的负荷。现在我想要做的就是验证表单元素,这取决于一个情景,并显示相应的错误信息,如果需要的所有操作的参数不填写正确。

I have a form that is going to be like an admin - backend form. It will have loads of buttons each having their own "action" in the controller class. Now all I want to do is to validate the form elements, depending upon a "scenario" and to display appropriate error messages if all the parameters needed for the action is not filled in properly.

能否有人告诉我我怎么能做到这一点不用我无需重新加载页面?

Can some one show me how I can do this without me having to reload the page?

[我已经找到了一种方法,但我不知道我在做什么是技术上是正确的。我有提交按钮,所有我想要的形式来执行的操作,并在各自的行动,我进行验证和形式的RenderPartial数据备份。每个按钮的onSuccess由从控制器retured的数据替换整个形式-DIV的数据。它的工作原理,但我想知道这是否是实现这一目标的唯一途径。]

[ I have found out a way, but I dont know if what I am doing is "technically" correct. I have submit buttons for all the actions that I want to perform in the form, and in the respective actions, I perform the validations and renderPartial form data back. OnSuccess of each button replaces the data of the entire "form-div" by the data that was retured from the controller. It works, but I want to know if this is the only way to achieve this.]

推荐答案

您应该启用CActiveFom ajaxformvalidation属性为true请看下面的例子

you should enable CActiveFom ajaxformvalidation property to true see the following example

在你的控制器动作  您应该取消以下行

in your controller action you should uncomment the following lines

$this->performAjaxValidation($model);

在你看来

<?php
    $form = $this->beginWidget('CActiveForm', array(  
                'id' => 'test-form',  
                'enableAjaxValidation' => true,  
            ));
?> 
 
精彩推荐
图片推荐