Yii - Ajax Form with validations

社会主义新天地 提交于 2020-01-03 06:22:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!