public function validate(array $attributes = null, $clearErrors = true) { if (!$this->beforeLogin() || !parent::validate($attributes, $clearErrors)) { return false; } $this->afterLogin(); return true; }
public function validate(array $attributes = NULL, $clearErrors = true) { if (!$this->beforeSignup() || !parent::validate()) { return false; } $this->afterSignup(); return true; }
protected function validateInternal(Validate $validate, $ruleName, $attributeName, $args, $placeholders, $messages) { if ($validate instanceof ModelValidate) { $validate->model = $this->model; $validate->attribute = $attributeName; } // rule if ($placeholders) { $validate->setPlaceholders($placeholders); } if ($messages) { $validate->setMessages($messages); } $validate = call_user_func_array([$validate, $ruleName], $args); if (!$validate->validate($this->model->{$attributeName})) { $this->model->addError($attributeName, $validate->getFirstError()); } }
/** * 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 @see Model::formName * 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 {@see \rock\widgets\ActiveHtml::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 \rock\template\HtmlException 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 HtmlException('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 HtmlException(get_class($model) . '::formName() cannot be empty for tabular inputs.'); } }
protected function errorsToPlaceholders(Model $model) { $errors = ArrayHelper::only($model->getErrors(), [], $model->safeAttributes()); $this->template->addMultiPlaceholders($errors); }
/** * 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.'); return $this->firstErrors ? $model->getFirstErrors() : $model->getErrors(); }
/** * Initializes the object. * * This method is called at the end of the constructor. * The default implementation will trigger an {@see \rock\db\common\BaseActiveRecord::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); }
/** * 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 (Rock::$app->request->isAjax) { * Rock::$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 \rock\components\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 \rock\components\Model $model */ foreach ($models as $model) { $model->validate($attributes); foreach ($model->getErrors() as $attribute => $errors) { $result[ActiveHtml::getInputId($model, $attribute)] = $errors; } } return $result; }