yii\base\Model::validateMultiple PHP Method

validateMultiple() public static method

This method will validate every model. The models being validated may be of the same or different types.
public static validateMultiple ( array $models, array $attributeNames = null ) : boolean
$models array the models to be validated
$attributeNames array list of attribute names that should be validated. If this parameter is empty, it means any attribute listed in the applicable validation rules should be validated.
return boolean whether all models are valid. False will be returned if one or multiple models have validation error.
    public static function validateMultiple($models, $attributeNames = null)
    {
        $valid = true;
        /* @var $model Model */
        foreach ($models as $model) {
            $valid = $model->validate($attributeNames) && $valid;
        }
        return $valid;
    }

Usage Example

Beispiel #1
2
 /**
  * Anlegen eines Benutzers
  * 
  * @return \yii\web\View
  * @author KAS <*****@*****.**> 28.07.2015
  */
 public function actionCreate()
 {
     Yii::$app->view->params['headline'] = 'Benutzer anlegen';
     $model = new User();
     //----------------------------------------------------------------------
     $post = \Yii::$app->request->post();
     if ($model->load($post)) {
         $authArr = [];
         foreach ($post['Auth'] as $authData) {
             $authArr[] = new Auth($authData);
         }
         // Daten Validieren und Zuordnen -----------------------------------
         if (Model::loadMultiple($authArr, $post) && Model::validateMultiple($authArr)) {
             // aus den Auth Objekten machen wir arrays,
             // damit wir das in die Mongo speichern können
             $model->auth = array_map(function ($a) {
                 return $a->toArray();
             }, $authArr);
             // Speichern ---------------------------------------------------
             $model->save();
             // Benutzer benachrichtigen ------------------------------------
             \Yii::$app->session->setFlash('success', 'Benutzer wurde erfolgreich angelegt!', TRUE);
             // Neue Daten laden, da wir in den Models Veränderungen vornehmen
             $model->refresh();
         }
     }
     // Defaultwerte festlegen ----------------------------------------------
     $model->created_at = new \MongoDate();
     $model->updated_at = new \MongoDate();
     $model->role = "Normal";
     //----------------------------------------------------------------------
     return $this->render('create', ['model' => $model]);
 }
All Usage Examples Of yii\base\Model::validateMultiple