本文整理汇总了PHP中yii\base\Model::getFirstErrors方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::getFirstErrors方法的具体用法?PHP Model::getFirstErrors怎么用?PHP Model::getFirstErrors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\base\Model
的用法示例。
在下文中一共展示了Model::getFirstErrors方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getErrors
/**
* @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: serializeModelErrors
/**
* 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: sendModelError
/**
* 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: getModelErrors
/**
* 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: serializeModelErrors
/**
* 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: validateModel
/**
* @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: exportErrors
/**
* 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: serializeModelErrors
/**
* 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;
}