Yii Framework Forum: validate() return values for model - array - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

validate() return values for model - array Chapter 3 - Page 96

#1 User is offline   yii 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 440
  • Joined: 25-July 09

Posted 17 January 2012 - 05:42 PM

Hi,

I like your book, but I have a question about returning error messages from an model[]

On site 96 you show following example
public function actionIndex()
{
      $models = array();
      if(!empty($_POST['Task']))
      {
            foreach($_POST['Task'] as $taskData)
            {
                  $model = new Task();
                  $model->setAttributes($taskData);

                  if($model->validate())
                      $models[] = $model;
            }
       }
}


What is happening here?
                  if($model->validate())
                      $models[] = $model;


Example:
I've added 4 Tasks on my view file and the third input field fails, because the user don't input a title.

How can I show the error-Message for field 3?
How does I now that field 3 returns an error?

thx
Meine Fragen stelle ich öffentlich und baue somit eine Wissensdatenbank für andere auf!
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,782
  • Joined: 17-January 09
  • Location:Russia

Posted 21 January 2012 - 02:44 PM

At that line we're just adding a model object to an array. Nothing more.

So overall we're iterating over all data from POST. For each data set we're creating model and filling it with the data. Then trying to validate it. If it fails, model will not be added to models array. As for the error itself, you can get these after validating like $model->getErrors().
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users