我的Yii代码在数据库中保存电影之前没有显示错误摘要..问题是什么?
表格代码:http://jsfiddle.net/SRMzc/
这是actionCreate函数:
public function actionCreateM()
{
$model=new Movie;
if(isset($_POST['Movie']))
{
$model->attributes=$_POST['Movie'];
$photos = CUploadedFile::getInstancesByName('photo');
if (isset($_POST['Movie']['youtube_id'])){
$model->youtube_id=$_POST['Movie']['youtube_id'];
}
if (isset($_POST['Movie']['poster_uri'])){
$file=CUploadedFile::getInstance($model,'poster_uri');
if(isset($file)){
$model->poster_uri = $model->short_title .'_poster.' . $file->extensionName;
}
}
if($model->save()).......
规则:
来自电影模特
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title, short_title, year, imdb_rate, rate, cast, director, summary, release_date, duration, views, featured', 'required'),
array('views, featured, available_status', 'numerical', 'integerOnly'=>true),
array('title, short_title, genre, director', 'length', 'max'=>64),
array('poster_uri', 'file', 'types'=>'jpg, gif, png', 'allowEmpty' => true),
array('cast', 'length', 'max'=>256),
array('year', 'length', 'max'=>4),
array('lang', 'length', 'max'=>2),
array('imdb_rate', 'length', 'max'=>3),
array('rate', 'length', 'max'=>5),
array('duration', 'length', 'max'=>11),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('title, short_title, views, featured, available_status', 'safe', 'on'=>'search'),
);
}
答案 0 :(得分:3)
错误摘要适用于ajax。您需要在表单定义中将enableAjaxValidation设置为true。那么你需要跟踪该ajax调用的运行并验证你的模型,然后回显错误(在Yii博客演示应用程序中查看创建操作以获取更多详细信息)。
如果您需要在PHP中验证代码,请尝试按照
进行操作if(!$model->save())
{
print_r($model->getErrors());
}
或
if(!$model->validate())
{
print_r($model->getErrors());
}
else
{
$model->save();
}
验证码就像这样
if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
echo CActiveForm::validate($comment);
Yii::app()->end();
}
编辑: - 您实际上不需要启用ajax来获取验证摘要。你可以拥有相同的PHP代码,就像这样。
if($model->validate())
{
$model->save();
//render some other view here
}
$this->render('Your_update_or_create_view');
答案 1 :(得分:1)
我认为你使用时应该 $ model-> errorSummary()
<?php echo $form->errorSummary($model); ?>