getFirstErrors() public method

Returns the first error of every attribute in the model.
See also: getErrors()
See also: getFirstError()
public getFirstErrors ( ) : array
return array the first errors. The array keys are the attribute names, and the array values are the corresponding error messages. An empty array will be returned if there is no error.
Beispiel #1
0
 /**
  * @return array
  */
 public function getErrors()
 {
     if (empty($this->_errors)) {
         foreach ($this->_model->getFirstErrors() as $name => $message) {
             $this->_errors[] = ['field' => $name, 'message' => $message];
         }
     }
     return $this->_errors;
 }
Beispiel #2
0
 /**
  * Serializes the validation errors in a model.
  * @param Model $model
  * @return array the array representation of the errors
  */
 protected function serializeModelErrors($model)
 {
     Yii::$app->response->setStatusCode(422, 'Data Validation Failed.');
     $result = [];
     foreach ($model->getFirstErrors() as $name => $message) {
         $result[] = ['field' => $name, 'message' => $message];
     }
     return $result;
 }
Beispiel #3
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;
 }
 /**
  * Helper function
  *
  * @param \yii\base\Model $model
  *
  * @return array
  * @todo breaks DRY. Think of the best way to implement.
  */
 protected function getModelErrors($model)
 {
     $errors = [];
     foreach ($model->getFirstErrors() as $error) {
         $errors[] = $error;
     }
     return $errors;
 }
Beispiel #5
0
 /**
  * Serializes the validation errors in a model.
  * @param Model $model
  * @return array the array representation of the errors
  */
 protected function serializeModelErrors($model)
 {
     $this->response->setStatusCode(422, 'Data Validation Failed.');
     $result = [];
     foreach ($model->getFirstErrors() as $name => $message) {
         $result[] = ['field' => $name, 'message' => $message];
     }
     return $result;
     // return [
     //     'success'=>0,
     //     'status'=>\Yii::$app->response->getStatusCode(),
     //     $this->collectionEnvelope=>$result
     // ];
 }
 /**
  * @param \yii\base\Model $model
  * @param array $attributes
  * @return bool
  */
 private function validateModel(\yii\base\Model $model, array $attributes)
 {
     $model->attributes = $attributes;
     if ($model->validate() !== true) {
         $this->errors[] = $model->getFirstErrors();
         return false;
     }
     return true;
 }
 /**
  * Exports the model validation errors.
  * @param Model $model
  * @return array
  */
 public function exportErrors($model)
 {
     $result = ['code' => $this->validationErrorCode, 'message' => $this->validationErrorMessage, 'errors' => []];
     foreach ($model->getFirstErrors() as $name => $message) {
         $result['errors'][] = ['field' => $name, 'message' => $message];
     }
     return $result;
 }
Beispiel #8
0
 /**
  * Serializes the validation errors in a model.
  * @param Model $model
  * @return array the array representation of the errors
  */
 protected function serializeModelErrors($model)
 {
     $this->response->setStatusCode(422, 'Data Validation Failed.');
     $result = [];
     foreach ($model->getFirstErrors() as $name => $message) {
         $result[] = ['source' => ['pointer' => "/data/attributes/{$name}"], 'detail' => $message];
     }
     return $result;
 }