本文整理汇总了PHP中yii\base\Model::getErrors方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::getErrors方法的具体用法?PHP Model::getErrors怎么用?PHP Model::getErrors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\base\Model
的用法示例。
在下文中一共展示了Model::getErrors方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* @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);
}
示例2: readValue
/**
* @param Model $model
* @param string $attribute
*/
private function readValue($model, $attribute)
{
$model->{$attribute} = $this->prompt(mb_convert_case($attribute, MB_CASE_TITLE, 'utf-8') . ':', ['validator' => function ($input, &$error) use($model, $attribute) {
$model->{$attribute} = $input;
if ($model->validate([$attribute])) {
return true;
} else {
$error = implode(',', $model->getErrors($attribute));
return false;
}
}]);
}
示例3: populateErrors
/**
* @param \yii\base\Model
* @param string $default_attribute
* @param array $attributes_map
*/
public function populateErrors(\yii\base\Model $Model, $default_attribute, $attributes_map = [])
{
$errors = $Model->getErrors();
foreach ($errors as $attribute => $messages) {
$attribute = isset($attributes_map[$attribute]) ? $attributes_map[$attribute] : $attribute;
if (false === $this->hasProperty($attribute)) {
if (!method_exists($this, 'hasAttribute')) {
$attribute = $default_attribute;
} elseif (false === $this->hasAttribute($attribute)) {
$attribute = $default_attribute;
}
}
foreach ($messages as $mes) {
$this->addError($attribute, $mes);
}
}
}
示例4: throwModel
/**
* @param Model $model
* @throws JqException
*/
public static function throwModel($model)
{
$errors = $model->getErrors();
if (empty($errors)) {
return;
}
$message = '';
foreach ($errors as $name => $error) {
if (!is_array($error)) {
continue;
}
$message .= $name . ': ';
foreach ($error as $e) {
$message .= $e . '; ';
}
}
throw new self($message);
}
示例5: validateAttributes
/**
* Валидация атрибутов.
*
* На вход передается модель для валидации и массив значений для валидации.
* Массив значений должен иметь следующий формат:
* ```php
* array(
* '<attribute1>' => array(
* array(
* 'value' => <mixed>, // значение для валидации
* 'isValid' => <boolean>, // true, если значение должно проходить валидацию
* ),
* ),
* )
* ```
*
* Проверяет, что атрибут либо должен проходить проверку валидации, либо не должен.
*
* @param Model $model проверяемая модель
* @param array $attributes массив значений атрибутов для валидации
*/
protected function validateAttributes(Model $model, $attributes)
{
foreach ($attributes as $attribute => $values) {
$attributeTitle = $model->getAttributeLabel($attribute);
foreach ($values as $v) {
$value = $v['value'];
$isValid = $v['isValid'];
$model->{$attribute} = $value;
if ($isValid) {
$message = $attributeTitle . ' validation error: ' . implode("\n", $model->getErrors($attribute));
$message .= "\nAsserting value: " . print_r($value, true);
$this->assertTrue($model->validate([$attribute]), $message);
} else {
$message = $attributeTitle . ' must be invalid' . "\n";
$message .= 'Asserting value: ' . print_r($value, true);
$this->assertFalse($model->validate([$attribute]), $message);
}
}
}
}
示例6: validate
/**
* Validates one or several models and returns an error message array indexed by the attribute IDs.
* This is a helper method that simplifies the way of writing AJAX validation code.
*
* For example, you may use the following code in a controller action to respond
* to an AJAX validation request:
*
* ```php
* $model = new Post;
* $model->load($_POST);
* if (Yii::$app->request->isAjax) {
* Yii::$app->response->format = Response::FORMAT_JSON;
* return ActiveForm::validate($model);
* }
* // ... respond to non-AJAX request ...
* ```
*
* To validate multiple models, simply pass each model as a parameter to this method, like
* the following:
*
* ```php
* ActiveForm::validate($model1, $model2, ...);
* ```
*
* @param Model $model the model to be validated
* @param mixed $attributes list of attributes that should be validated.
* If this parameter is empty, it means any attribute listed in the applicable
* validation rules should be validated.
*
* When this method is used to validate multiple models, this parameter will be interpreted
* as a model.
*
* @return array the error message array indexed by the attribute IDs.
*/
public static function validate($model, $attributes = null)
{
$result = [];
if ($attributes instanceof Model) {
// validating multiple models
$models = func_get_args();
$attributes = null;
} else {
$models = [$model];
}
/* @var $model Model */
foreach ($models as $model) {
$model->validate($attributes);
foreach ($model->getErrors() as $attribute => $errors) {
$result[Html::getInputId($model, $attribute)] = $errors;
}
}
return $result;
}
示例7: addModelErrors
/**
* @param \yii\base\Model $model
*/
public static function addModelErrors($model)
{
$errors = $model->getErrors();
self::addError(self::flatten($errors));
}
示例8: formatModelErrors
/**
* @param Model $model
* @param string $glue
* @return string
*/
public static function formatModelErrors(Model $model, $glue = PHP_EOL)
{
return implode($glue, array_map(function ($item) {
return is_array($item) ? array_pop($item) : $item;
}, $model->getErrors()));
}
示例9:
function __construct(\yii\base\Model $model)
{
parent::__construct('model.validation.error', null, $model->getErrors());
}