本文整理汇总了PHP中yii\base\Model::hasErrors方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::hasErrors方法的具体用法?PHP Model::hasErrors怎么用?PHP Model::hasErrors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\base\Model
的用法示例。
在下文中一共展示了Model::hasErrors方法的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: modelErrors
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);
}
}
示例3: afterValidate
/**
* @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();
}
}
示例4: 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;
}
示例5: validateAttributes
/**
* 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);
}
}
}
}
示例6: validateAttributes
/**
* 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);
}
}
}
}
示例7: alertValidation
/**
* 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)]);
}
示例8: hasErrors
/**
* @param null $attribute
* @return bool
*/
public function hasErrors($attribute = null)
{
if (!empty($this->exceptions)) {
return true;
} else {
return parent::hasErrors($attribute);
}
}
示例9: validateAttributes
/**
* 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);
}
}
}
}