1111111111111


เว็บใหม่ที่ผมขอใช้ฟรีครับ ภาวนาอยู่ว่าวันดีคืนดีข้อมูลคงไม่หายเหมือนเว็บอื่น

วันเสาร์ที่ 19 กุมภาพันธ์ พ.ศ. 2554

AjaxValidation with multiple models and one form

ประมาณ มีหลาย Model ที่จะทำการ ตรวจสอบข้อมูลของ Form


beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
)); ?>

Fields with * are required.



errorSummary($model); ?>



labelEx($model,'region_id');?>
textField($model,'region_id'); ?>
error($model,'region_id'); ?>




labelEx($acl,'userName'); ?>
textField($acl,'userName',array('size'=>20,'maxlength'=>25)); ?>
error($acl,'userName'); ?>


......


ในส่วน Action


class AbcController extends Controller{
........
public function actionCreate()
{
$model=new user;
$acl = new acl;


// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation1($acl); //ตรวจสอบ $acl
$this->performAjaxValidation($model); // ตรวจสอบ $model



แก้ไขในส่วนของ performAjaxValidation ครับ


class AbcController extends Controller{
........
public function actionCreate(){
.......
}

protected function performAjaxValidation($model, $acl)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
{
echo CActiveForm::validate(array($model, $acl)); // แก้ไขเป็นการตรวจสอบทั้ง $model และ $acl
Yii::app()->end();
}
}



แก้ไขในส่วน errorSummary ของ Views ครับ เป็น


$form->errorSummary(array($model,$acl)); //หมายถึงแสดง error ทั้ง $model,$acl (เอา array() ครอบนะครับ)








อ้างอิง :http://www.yiiframework.com/forum/index.php?/topic/9476-ajaxvalidation-with-multiple-models-and-one-form/



0 ความคิดเห็น: