Yii - How to use AJAX form validation
Yii supports AJAX form validation, which essentially posts the form values to the server, validates them, and sends back the validation errors, all without leaving the page. It does this every time you tab out of a (changed) field.
As of 1.1.7, Yii supports regular Javascript validation in addition to AJAX validation, but I'll talk about that in another post.
Here's how Yii's AJAX validation works:
<php $form = $this->beginWidget('CActiveForm', array(
'id'=>'lowercasemodelname-form', //not technically required but works w gii generated controllers
'enableAjaxValidation'=>true //turn on ajax validation on the client side ));
And have at least one form element with a matching error function: <?php echo $form->textField($model, 'my_attribute'); ?>
<?php echo $form->error($model, 'my_attribute'); ?>
This makes Yii include the JQuery javascript library, as well as a Yii javascript file called jquery.yiiactiveform.js if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
Which is sligtly different than how Gii generates it, but no big diff. CActiveForm::validate() can take an array of models, which is not clear the way Gii does it. {"Field_id":["Validation error a"],"Another_field_id":["Validation error B"]}
which yii then plugs into the error field below your field. <div id="Model_attributename_em_" class="errorMessage" style="display:none"></div>
If that field has a validation error, then Yii sets the display to block, writes the validation error message to its innerHtml, and then you see the error. If it later validates, yii hides it again. . When a form field is valid, it adds "success" class to the div - which makes it green. When it's invalid, it adds "error" class, which makes it red. It also quickly adds a "validating" class, which does nothing, but you can supply it yourself and change the look of a field while it's validating.
다음으로 이동:http://learnyii.blogspot.tw/2010/12/yii.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.