view/test/create.php
<?php
$this->breadcrumbs=array(
'test',
);?>
<h2> Test Sample </h2>
<br>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'subscription-form',
'enableAjaxValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->textField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton( 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
controller/TestController.php
class TestController extends Controller
{
public function actionIndex()
{
$this->render('index');
}
public function actionCreate()
{
$model=new test;
$this->performAjaxValidation($model);
if(isset($_POST['test']))
{
$model->attributes=$_POST['test'];
if($model->save())
{
//$this->redirect('index.php?r=test/create');
}
}
$this->render('create',array('model'=>$model));
}
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='subscription-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
models/test.php
class test extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'tbl_test';
}
public function rules()
{
return array(
array('username, password', 'required'),
array('username, password', 'length', 'max'=>128),
array('id, username, password', 'safe', 'on'=>'search'),
);
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
);
}
}
this ajax form not working , and how to create nice dialog message for this ajax form
thanks