CForm showErrorSummary=true, but no errors shown

Just tried out the new CForm for my registration form, but I had no luck. No error summary in case of an input error, no insert into the db (if no error was made). Nothing, only the form with the fields populated with the data I entered before submitting.

In my case the registration form is split up into 2 subforms because

  • One Account can have

  • multiple Email addresses

I have an Account model and an Email model.

My form configuration (protected/views/site/subscribeForm):




return array(

    'showErrorSummary' => true,

    'title' => Yii::t('subscribe', 'Get a free account'),

    'method' => 'post',

    'elements' => array(


        'Email' => array(

            'type' => 'form',

            'model' => new Email,

            'elements' => array(

                'email_type_id' => array(

                    'type' => 'dropdownlist',

                    'items' => CHtml::listData(EmailType::model()->active()->findAll(),

                      'id', 'nameLocalized'),

                ),

                'email' => array(

                    'type' => 'text',

                ),

            ),

        ),


        'Account' => array(

            'type' => 'form',

            'model' => new Account,

            'elements' => array(

                'gender_id' => array(

                    'type' => 'dropdownlist',

                    'items' => CHtml::listData(Gender::model()->findAll(), 'id',

                      'nameLocalized'),

                    'label' => '',

                ),

            ),

        ),

    ),


    'buttons' => array(

        'submit' => array(

            'type' => 'submit',

            'label' => Yii::t('form', 'Subscribe'),

        ),

    ),

);



The controller




...

    public function actionSubscribe()

    {

        $form = new CForm('application.views.site.subscribeForm');


        if ($form->submitted() && $form->validate()) {

            $account = $form['Account']->model;

            $email = $form['Email']->model;

            if ($account->save(false)) {

                $email->id = $account->id;

                $email->save(false);

                $this->redirect(array('thankyou'));

            }

        }

        $this->render('subscribe', array('form'=>$form));

    }

...



The view protected/views/site/subscribe.php




$this->pageTitle=Yii::t('headlines', 'Subscribe');

$this->breadcrumbs=array($this->pageTitle);

?>

<h1><?php echo CHtml::encode($this->pageTitle); ?></h1>

<?php echo $form; ?>



Maybe you can point out, what I did wrong?

Thank you!

Nobody else having trouble displaying errors using CForm?

It’s working for me with a simple (non-nested) form. Perhaps you need to specify it for each sub-form?

Thanks @jsoo for the hint. I tried it out but it doesn’t work. Even on every element (which causes an additional HTML attribute showErrorSummary=“1” :) )

Using an SVN copy from last week, it’s working for me (haven’t tested under other versions). showErrorSummary needs to exist on the same level as the ‘title’ parameter.

It only worked for me using ONE model


$form = new CForm('path.to.form', $model)

. Nested forms with multiple models (one model per subform) didn’t work for me.

@intel352: Could you please send me your code so I can compare yours with mine?

Having tested further, I’m able to verify that I’m able to see an error summary, when I specify showErrorSummary=true per nested form.

My form structure is roughly:




array(

	'elements'=>array(

		'ModelForm'=>array(

			'title'=>'blah',

			'showErrorSummary'=>true,

			'elements'=>array(...), // I nest other model forms within here, same structure repeated downward

		),

	),

	'buttons'=>array(...),

);



One thing that got me originally (regarding errors displaying properly) was that you obviously need to validate every model before returning back to the form page, otherwise if you "exit" your logic once the first errors are encountered in a model, you only see the errors for that model.

As someone else mentioned in another thread, I believe you can also force the entire form to validate, but I don’t use (haven’t tried) that method, as my forms have inter-dependencies :slight_smile:

One additional note, when using nested forms, it shows the error summary per nested form, instead of merging the error summaries together. I don’t know if there is an override option for that, as I can see a use for both methods.

I just set up a complete new yii installation, created 3 models, one nested form and voilà - it worked. So thank you for your patience … seems that there’s a mistake in my project (but didn’t find it yet).

I am also having trouble with using one single error summary with CForm with nested forms.

Having separate error summaries for each nested form worked but seems ugly because a summary form breaks the form in the middle

I have tried creating a single error summary if you go through the more classical approach, instead of using CForm, and this code below worked perfectly creating a single




<?php

print $form->errorSummary($models);	//yes this can indeed be an array of models (since in this form we have multiple models) 

?>



Is there a missing element inside Yii or am I using it wrong?

You guys are right, I cannot display error summary as well.

I tried to use CHtml::errorSummary($model) method, and then I got a message that $model don’t have getErrors() method. It’s a bug to me…

Cheers,

BarBQ

I fixed it using:

<?php echo CHtml::errorSummary($form->models); ?>

In my form with two nested models.