问题
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?
[ 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.]
回答1:
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);
in your view
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'test-form',
'enableAjaxValidation' => true,
));
?>
来源:https://stackoverflow.com/questions/8831476/yii-ajax-form-with-validations