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.
コード例 #1
0
ファイル: ValidateException.php プロジェクト: mole-chen/yii2
 /**
  * @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;
 }
コード例 #2
0
ファイル: Serializer.php プロジェクト: dextercool/yii2-easyui
 /**
  * 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;
 }
コード例 #3
0
ファイル: Controller.php プロジェクト: luyadev/luya-core
 /**
  * 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;
 }
コード例 #4
0
 /**
  * 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;
 }
コード例 #5
0
ファイル: Serializer.php プロジェクト: pointdnd/yii2-modular
 /**
  * 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
     // ];
 }
コード例 #6
0
 /**
  * @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;
 }
コード例 #7
0
 /**
  * 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;
 }
コード例 #8
0
ファイル: Serializer.php プロジェクト: tuyakhov/yii2-json-api
 /**
  * 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;
 }