- 修改models下的companies.php
/**
* @inheritdoc
*/public function rules()
{
return [
[[\'company_name\' , \'company_email\', \'company_address\', \'company_start_date\' , \'company_created_date\', \'company_status\'], \'required\'],
[[\'company_start_date\' , \'company_created_date\'], \'safe\'],
[[\'company_start_date\' ],\'checkDate\'],
[[\'company_status\' ], \'string\'],
[[\'company_name\' , \'company_email\', \'company_address\'], \'string\', \'max\' => 255]
];
}
public function checkDate ($attribute, $params){
$today =date(\'Y-m-d\' );
$selectedDate =date($this ->company_start_date);
if ($selectedDate> $today){
$this ->addError( $attribute,\'company start Date must be smaller !\');
}
}
2.修改_form.php文件
# code ...
<?php $form = ActiveForm:: begin([\'enableAjaxValidation\' =>true]); ?>
# code ...
3.修改控制器文件
/**
* Creates a new Companies model.
* If creation is successful, the browser will be redirected to the \'view\' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Companies ();
if (Yii:: $app->request ->isAjax && $model->load ($_POST)){
Yii ::$app-> response->format =\'json\';
return ActiveForm ::validate( $model);
}
if ($model-> load(Yii ::$app-> request->post ()) && $model->save ()) {
return $this ->redirect([ \'view\', \'id\' => $model->company_id ]);
} else {
return $this ->render( \'create\', [
\'model\' => $model,
]);
}
}
来自于datou:https://github.com/datou-leo/ci