hasErrors() public method

Returns a value indicating whether there is any validation error.
public hasErrors ( string | null $attribute = null ) : boolean
$attribute string | null attribute name. Use null to check all attributes.
return boolean whether there is any error.
 /**
  * @param Model $model
  * @param string $message
  * @param int $code
  * @param Exception $previous
  */
 public function __construct(Model $model, $message = null, $code = 0, Exception $previous = null)
 {
     $this->model = $model;
     if (is_null($message) && $model->hasErrors()) {
         $message = implode(' ', array_map(function ($errors) {
             return implode(' ', $errors);
         }, $model->getErrors()));
     }
     parent::__construct($message, $code, $previous);
 }
Beispiel #2
1
 public static function modelErrors(Model $model, $message = null, $category = 'application')
 {
     if (!is_null($message)) {
         static::error($message, $category);
     }
     if ($model->hasErrors()) {
         static::error(['class' => get_class($model), 'attributes' => $model->getAttributes(), 'errors' => $model->getErrors()], $category);
     }
 }
 /**
  * @param Event $event event parameter.
  */
 public function afterValidate($event)
 {
     if (!$this->getIsNeedValidate()) {
         $this->owner->clearErrors($this->attribute);
     }
     if ($this->owner->hasErrors()) {
         $this->addAttempt();
     } else {
         $this->removeAttempts();
     }
 }
Beispiel #4
0
 /**
  * Helper method to correctly send model erros and add correct response headers.
  *
  * @param ActiveRecordInterface $model
  * @throws ServerErrorHttpException
  * @return array
  */
 public function sendModelError(Model $model)
 {
     if (!$model->hasErrors()) {
         throw new ServerErrorHttpException('Object error for unknown reason.');
     }
     Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
     $result = [];
     foreach ($model->getFirstErrors() as $name => $message) {
         $result[] = ['field' => $name, 'message' => $message];
     }
     return $result;
 }
Beispiel #5
0
 /**
  * Validates the specified object.
  * @param \yii\base\Model $model the data model being validated
  * @param array|null $attributes the list of attributes to be validated.
  * Note that if an attribute is not associated with the validator, or is is prefixed with `!` char - it will be
  * ignored. If this parameter is null, every attribute listed in [[attributes]] will be validated.
  */
 public function validateAttributes($model, $attributes = null)
 {
     if (is_array($attributes)) {
         $newAttributes = [];
         foreach ($attributes as $attribute) {
             if (in_array($attribute, $this->attributes) || in_array('!' . $attribute, $this->attributes)) {
                 $newAttributes[] = $attribute;
             }
         }
         $attributes = $newAttributes;
     } else {
         $attributes = [];
         foreach ($this->attributes as $attribute) {
             $attributes[] = $attribute[0] === '!' ? substr($attribute, 1) : $attribute;
         }
     }
     foreach ($attributes as $attribute) {
         $skip = $this->skipOnError && $model->hasErrors($attribute) || $this->skipOnEmpty && $this->isEmpty($model->{$attribute});
         if (!$skip) {
             if ($this->when === null || call_user_func($this->when, $model, $attribute)) {
                 $this->validateAttribute($model, $attribute);
             }
         }
     }
 }
Beispiel #6
0
 /**
  * Validates the specified object.
  * @param \yii\base\Model $object the data object being validated
  * @param array|null $attributes the list of attributes to be validated.
  * Note that if an attribute is not associated with the validator,
  * it will be ignored.
  * If this parameter is null, every attribute listed in [[attributes]] will be validated.
  */
 public function validateAttributes($object, $attributes = null)
 {
     if (is_array($attributes)) {
         $attributes = array_intersect($this->attributes, $attributes);
     } else {
         $attributes = $this->attributes;
     }
     foreach ($attributes as $attribute) {
         $skip = $this->skipOnError && $object->hasErrors($attribute) || $this->skipOnEmpty && $this->isEmpty($object->{$attribute});
         if (!$skip) {
             if ($this->when === null || call_user_func($this->when, $object, $attribute)) {
                 $this->validateAttribute($object, $attribute);
             }
         }
     }
 }
Beispiel #7
0
 /**
  * Prints alert widget containing error summary of validated model
  * @param \yii\base\Model $model
  * @return string
  */
 public static function alertValidation(\yii\base\Model $model)
 {
     return static::alert($model->hasErrors(), ['type' => static::error(), 'body' => Html::errorSummary($model)]);
 }
Beispiel #8
0
 /**
  * @param null $attribute
  * @return bool
  */
 public function hasErrors($attribute = null)
 {
     if (!empty($this->exceptions)) {
         return true;
     } else {
         return parent::hasErrors($attribute);
     }
 }
Beispiel #9
0
 /**
  * Validates the specified object.
  * @param \yii\base\Model $model the data model being validated
  * @param array|null $attributes the list of attributes to be validated.
  * Note that if an attribute is not associated with the validator,
  * it will be ignored.
  * If this parameter is null, every attribute listed in [[attributes]] will be validated.
  */
 public function validateAttributes($model, $attributes = null)
 {
     if (is_array($attributes)) {
         $attributes = array_intersect($this->attributes, $attributes);
         //找出需要被验证的属性
     } else {
         $attributes = $this->attributes;
     }
     foreach ($attributes as $attribute) {
         $skip = $this->skipOnError && $model->hasErrors($attribute) || $this->skipOnEmpty && $this->isEmpty($model->{$attribute});
         //判断如果此属性有一个验证错误或者属性值为空时候是否跳过验证
         if (!$skip) {
             if ($this->when === null || call_user_func($this->when, $model, $attribute)) {
                 //验证一个具体属性之前的回掉函数
                 $this->validateAttribute($model, $attribute);
             }
         }
     }
 }