Yii not showing errorSummary

11,326

Solution 1

Error summary works with ajax. you need to set enableAjaxValidation to true in your form definition. then you need to track that ajax call in action and validate your model and then echo the errors (check create action in Yii blog demo app for more details).

If you need to validate the code in PHP try following

if(!$model->save())
{
    print_r($model->getErrors());
}

or

if(!$model->validate())
{
    print_r($model->getErrors());
}
else 
{
    $model->save();
}

validation code goes something like this

if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
    echo CActiveForm::validate($comment);
    Yii::app()->end();
}

Edit:- You don't really need to enable ajax to get validation summary. you can have php code for the same, something like this.

if($model->validate())
{
    $model->save();
    //render some other view here
}
$this->render('Your_update_or_create_view');

Solution 2

I think it should be $model->errorSummary() while you are using

<?php echo $form->errorSummary($model); ?>
Share:
11,326
Abudayah
Author by

Abudayah

Front End Software Engineer

Updated on June 04, 2022

Comments

  • Abudayah
    Abudayah almost 2 years

    My Yii code not showing error summery before save movie in database.. what's the problem ?

    Form code: http://jsfiddle.net/SRMzc/

    it's actionCreate function:

      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()).......
    

    Rules:

    from movie model

    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'),
        );
    }
    
    • adamors
      adamors almost 12 years
      You have a die() in the middle of the code, is that on purpose?
    • Abudayah
      Abudayah almost 12 years
      sorry.. removed.. still not showing error summary
    • Pentium10
      Pentium10 almost 12 years
      Where do you call validate and what are your rules? Where do you print errorSummary() ?
    • Ivo Renkema
      Ivo Renkema almost 12 years
      You would print your error summary form your view. Please show that. Your rules would be in your Movie model. Please show that too.
    • Abudayah
      Abudayah almost 12 years
      hi all, I have add rules form movie model.. and form code: jsfiddle.net/SRMzc
  • bool.dev
    bool.dev almost 12 years
    $form->errorSummary($model); is correct check the errorSummary documentation, the method doesn't belong to any model object but belongs to CHtml class, and CActiveForm class also has a separate implementation
  • bool.dev
    bool.dev almost 12 years
    @Uday errorSummary doesn't absolutely need ajaxvalidation(so please correct that). however i agree with your method of showing errorSummary for ajax. @AnasA i think you might have missed something in your if statements which prevents the model with error being passed to the view. so if you add your entire actionCreateM , we could help you correct that.
  • Uday Sawant
    Uday Sawant almost 12 years
    @bool.dev Yes I agree with you, the action does not need to be an ajax call to return validation errors. but the code shown above makes active validation using ajax calls, like for onFocusLost event of a textbox field.
  • bool.dev
    bool.dev almost 12 years
    well, the line(s) "Error summary works with ajax. you need to set enableAjaxValidation to true in your form definition." is misleading, when the question reads "errorSummary not showing". if you intended to give an alternate solution, its ok, but wouldn't it be better to mention that? i'm just saying that he never asked for active validation, but if you are offering that, you could have mentioned it, which will (IMHO) make your answer unambiguous. [ps: ignore my previous comment, which i'll be deleting shortly]