/** * Initializes the object. * This method is called at the end of the constructor. * The default implementation will trigger an [[EVENT_INIT]] event. * If you override this method, make sure you call the parent implementation at the end * to ensure triggering of the event. */ public function init() { parent::init(); $this->trigger(self::EVENT_INIT); }
/** * 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; }
/** * @inheritdoc */ public function __unset($name) { if (array_key_exists($name, $this->_attributes)) { unset($this->_attributes[$name]); } else { parent::__unset($name); } }
/** * Adds an error about the specified attribute to the model object. * This is a helper method that performs message selection and internationalization. * @param \Leaps\Base\Model $model the data model being validated * @param string $attribute the attribute being validated * @param string $message the error message * @param array $params values for the placeholders in the error message */ public function addError($model, $attribute, $message, $params = []) { $value = $model->{$attribute}; $params['attribute'] = $model->getAttributeLabel($attribute); $params['value'] = is_array($value) ? 'array()' : $value; $model->addError($attribute, Leaps::$app->getI18n()->format($message, $params, Leaps::$app->language)); }
/** * 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: * * ~~~ * $model = new Post; * $model->load($_POST); * if (Leaps::$app->request->isAjax) { * Leaps::$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: * * ~~~ * 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; }
/** * Returns the client side validation options. * @param \Leaps\Base\Model $model the model being validated * @param string $attribute the attribute name being validated * @return array the client side validation options */ protected function getClientOptions($model, $attribute) { $label = $model->getAttributeLabel($attribute); $options = []; if ($this->message !== null) { $options['message'] = Leaps::$app->getI18n()->format($this->message, ['attribute' => $label], Leaps::$app->language); } $options['skipOnEmpty'] = $this->skipOnEmpty; if (!$this->skipOnEmpty) { $options['uploadRequired'] = Leaps::$app->getI18n()->format($this->uploadRequired, ['attribute' => $label], Leaps::$app->language); } if ($this->mimeTypes !== null) { $options['mimeTypes'] = $this->mimeTypes; $options['wrongMimeType'] = Leaps::$app->getI18n()->format($this->wrongMimeType, ['attribute' => $label, 'mimeTypes' => implode(', ', $this->mimeTypes)], Leaps::$app->language); } if ($this->extensions !== null) { $options['extensions'] = $this->extensions; $options['wrongExtension'] = Leaps::$app->getI18n()->format($this->wrongExtension, ['attribute' => $label, 'extensions' => implode(', ', $this->extensions)], Leaps::$app->language); } if ($this->minSize !== null) { $options['minSize'] = $this->minSize; $options['tooSmall'] = Leaps::$app->getI18n()->format($this->tooSmall, ['attribute' => $label, 'limit' => $this->minSize], Leaps::$app->language); } if ($this->maxSize !== null) { $options['maxSize'] = $this->maxSize; $options['tooBig'] = Leaps::$app->getI18n()->format($this->tooBig, ['attribute' => $label, 'limit' => $this->maxSize], Leaps::$app->language); } if ($this->maxFiles !== null) { $options['maxFiles'] = $this->maxFiles; $options['tooMany'] = Leaps::$app->getI18n()->format($this->tooMany, ['attribute' => $label, 'limit' => $this->maxFiles], Leaps::$app->language); } return $options; }
/** * Generates an appropriate input name for the specified attribute name or expression. * * This method generates a name that can be used as the input name to collect user input * for the specified attribute. The name is generated according to the [[Model::formName|form name]] * of the model and the given attribute name. For example, if the form name of the `Post` model * is `Post`, then the input name generated for the `content` attribute would be `Post[content]`. * * See [[getAttributeName()]] for explanation of attribute expression. * * @param Model $model the model object * @param string $attribute the attribute name or expression * @return string the generated input name * @throws InvalidParamException if the attribute name contains non-word characters. */ public static function getInputName($model, $attribute) { $formName = $model->formName(); if (!preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) { throw new InvalidParamException('Attribute name must contain word characters only.'); } $prefix = $matches[1]; $attribute = $matches[2]; $suffix = $matches[3]; if ($formName === '' && $prefix === '') { return $attribute . $suffix; } elseif ($formName !== '') { return $formName . $prefix . "[{$attribute}]" . $suffix; } else { throw new InvalidParamException(get_class($model) . '::formName() cannot be empty for tabular inputs.'); } }