Returns the text label for the specified attribute.
public getAttributeLabel ( string $attribute ) : string | ||
$attribute | string | the attribute name |
Résultat | string | the attribute label |
/** * @inheritdoc */ public function run() { Asset::register($this->view); $inputId = Html::getInputId($this->model, $this->attribute); $field = $this->render('_field', ['fieldClass' => 'field-' . $inputId, 'inputId' => $inputId, 'inputName' => Html::getInputName($this->model, $this->attribute) . '[]', 'inputLabel' => $this->model->getAttributeLabel($this->attribute)]); $options = Json::htmlEncode(['field' => $field, 'firstAddTogglerText' => 'добавить свою категорию', 'addTogglerText' => 'добавить ещё', 'values' => $this->model->{$this->attribute}]); $this->view->registerJs('jQuery("#' . $this->id . '").kategoriiSlushatelejInputFields(' . $options . ');'); return Html::tag('div', $this->renderFirstToggler(), ['id' => $this->id, 'class' => 'kategorii-slushatelej-input-fields']); }
/** * Applies the required formatting and params to messages. * @param Model $model * @param string $attribute * @return array Prepared messages */ public function prepareMessages($model, $attribute) { $params = array_merge($this->messageParams(), ['attribute' => $model->getAttributeLabel($attribute)]); $messages = []; foreach ($this->messages() as $name => $message) { $messages[$name] = Yii::$app->getI18n()->format($message, $params, Yii::$app->language); } return $messages; }
public function getAttributeLabel($attribute) { foreach ($this->models as $model) { if ($model->hasAttribute($attribute)) { return $model->getAttributeLabel($attribute); } } return parent::getAttributeLabel($attribute); }
/** * Extracts Model errors from array and puts them into string * @param Model $model * @param boolean $html * @return string */ public static function errorsToString($model, $html = true) { $result = ''; $delimiter = $html ? "<br/>\n" : "\n"; foreach ($model->errors as $attribute => $error) { $errors = implode(', ', $error); $result .= $model->getAttributeLabel($attribute) . ": {$errors}{$delimiter}"; } return $result; }
/** * Валидация атрибутов. * * На вход передается модель для валидации и массив значений для валидации. * Массив значений должен иметь следующий формат: * ```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); } } } }
/** * Returns the client side validation options. * @param \yii\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'] = Yii::$app->getI18n()->format($this->message, ['attribute' => $label], Yii::$app->language); } $options['skipOnEmpty'] = $this->skipOnEmpty; if (!$this->skipOnEmpty) { $options['uploadRequired'] = Yii::$app->getI18n()->format($this->uploadRequired, ['attribute' => $label], Yii::$app->language); } if ($this->mimeTypes !== null) { $options['mimeTypes'] = $this->mimeTypes; $options['wrongMimeType'] = Yii::$app->getI18n()->format($this->wrongMimeType, ['attribute' => $label, 'mimeTypes' => join(', ', $this->mimeTypes)], Yii::$app->language); } if ($this->extensions !== null) { $options['extensions'] = $this->extensions; $options['wrongExtension'] = Yii::$app->getI18n()->format($this->wrongExtension, ['attribute' => $label, 'extensions' => join(', ', $this->extensions)], Yii::$app->language); } if ($this->minSize !== null) { $options['minSize'] = $this->minSize; $options['tooSmall'] = Yii::$app->getI18n()->format($this->tooSmall, ['attribute' => $label, 'limit' => $this->minSize], Yii::$app->language); } if ($this->maxSize !== null) { $options['maxSize'] = $this->maxSize; $options['tooBig'] = Yii::$app->getI18n()->format($this->tooBig, ['attribute' => $label, 'limit' => $this->maxSize], Yii::$app->language); } if ($this->maxFiles !== null) { $options['maxFiles'] = $this->maxFiles; $options['tooMany'] = Yii::$app->getI18n()->format($this->tooMany, ['attribute' => $label, 'limit' => $this->maxFiles], Yii::$app->language); } return $options; }
/** * Builds and adds [[comboNotUnique]] error message to the specified model attribute. * @param \yii\base\Model $model the data model. * @param string $attribute the name of the attribute. */ private function addComboNotUniqueError($model, $attribute) { $attributeCombo = []; $valueCombo = []; foreach ($this->targetAttribute as $key => $value) { if (is_int($key)) { $attributeCombo[] = $model->getAttributeLabel($value); $valueCombo[] = '"' . $model->{$value} . '"'; } else { $attributeCombo[] = $model->getAttributeLabel($key); $valueCombo[] = '"' . $model->{$key} . '"'; } } $this->addError($model, $attribute, $this->message, ['attributes' => Inflector::sentence($attributeCombo), 'values' => implode('-', $valueCombo)]); }
/** * Adds an error about the specified attribute to the model object. * This is a helper method that performs message selection and internationalization. * @param \yii\base\Model $object the data object 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($object, $attribute, $message, $params = []) { $value = $object->{$attribute}; $params['attribute'] = $object->getAttributeLabel($attribute); $params['value'] = is_array($value) ? 'array()' : $value; $object->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language)); }
/** * Gets an attribute label * * @param string $attribute * * @return string */ public function getAttributeLabel($attribute) { return parent::getAttributeLabel($attribute); }
/** * Returns the client side validation options. * @param \yii\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); $format = function ($message, $label) { return Yii::$app->getI18n()->format($message, ['attribute' => $label], Yii::$app->language); }; return ['skipOnEmpty' => $this->skipOnEmpty, 'typeAttribute' => Html::getInputId($model, $this->typeAttribute), 'messageLength' => $format($this->getAdapter($model)->messageLength, $label), 'messageChecksum' => $format($this->getAdapter($model)->messageChecksum, $label), 'messageCharacters' => $format($this->getAdapter($model)->messageCharacters, $label)]; }
/** * Returns the client-side validation options. * @param \yii\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'] = Yii::$app->getI18n()->format($this->message, ['attribute' => $label], Yii::$app->language); } $options['skipOnEmpty'] = $this->skipOnEmpty; if (!$this->skipOnEmpty) { $options['uploadRequired'] = Yii::$app->getI18n()->format($this->uploadRequired, ['attribute' => $label], Yii::$app->language); } if ($this->mimeTypes !== null) { $mimeTypes = []; foreach ($this->mimeTypes as $mimeType) { $mimeTypes[] = new JsExpression(Html::escapeJsRegularExpression($this->buildMimeTypeRegexp($mimeType))); } $options['mimeTypes'] = $mimeTypes; $options['wrongMimeType'] = Yii::$app->getI18n()->format($this->wrongMimeType, ['attribute' => $label, 'mimeTypes' => implode(', ', $this->mimeTypes)], Yii::$app->language); } if ($this->extensions !== null) { $options['extensions'] = $this->extensions; $options['wrongExtension'] = Yii::$app->getI18n()->format($this->wrongExtension, ['attribute' => $label, 'extensions' => implode(', ', $this->extensions)], Yii::$app->language); } if ($this->minSize !== null) { $options['minSize'] = $this->minSize; $options['tooSmall'] = Yii::$app->getI18n()->format($this->tooSmall, ['attribute' => $label, 'limit' => $this->minSize, 'formattedLimit' => Yii::$app->formatter->asShortSize($this->minSize)], Yii::$app->language); } if ($this->maxSize !== null) { $options['maxSize'] = $this->maxSize; $options['tooBig'] = Yii::$app->getI18n()->format($this->tooBig, ['attribute' => $label, 'limit' => $this->getSizeLimit(), 'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit())], Yii::$app->language); } if ($this->maxFiles !== null) { $options['maxFiles'] = $this->maxFiles; $options['tooMany'] = Yii::$app->getI18n()->format($this->tooMany, ['attribute' => $label, 'limit' => $this->maxFiles], Yii::$app->language); } return $options; }
/** * @param Model $model * @return array */ protected function getAuditValues($model) { return [[$model->getAttributeLabel('created_at'), date('d.m.Y', $model->created_at)], [$model->getAttributeLabel('updated_at'), date('d.m.Y', $model->updated_at)]]; }
/** * Adds an error about the specified attribute to the model object. * This is a helper method that performs message selection and internationalization. * @param \yii\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 = []) { $params['attribute'] = $model->getAttributeLabel($attribute); if (!isset($params['value'])) { $value = $model->{$attribute}; $params['value'] = is_array($value) ? 'array()' : $value; } $model->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language)); }
/** * Returns the JavaScript needed for performing client-side validation. * @param \yii\base\Model $model the data model being validated * @param string $attribute the name of the attribute to be validated. * @param \yii\web\View $view the view object that is going to be used to render views or view files * containing a model form with this validator applied. * @return string the client-side validation script. Null if the validator does not support * client-side validation. * @see \yii\widgets\ActiveForm::enableClientValidation */ public function clientValidateAttribute($model, $attribute, $view) { // get the pattern $pattern = $this->elaboratePattern(); // get the error message $message = $this->message ? $this->message : \Yii::t('yii', '{attribute} is invalid.'); $message = str_replace('{attribute}', $model->getAttributeLabel($attribute), $message); $condition = "!value.match({$pattern})"; return "\nif(" . $condition . ") {\n messages.push(" . Json::encode($message) . ");\n}\n"; }
/** * @param \yii\base\Model $model the model to be validated. * @param string $attribute the name of the attribute to be validated. */ private function getErrorParams($model, $attribute) { return ['attribute' => $model->getAttributeLabel($attribute), 'other_attributes' => implode(', ', array_map(function ($e) use($model) { return $model->getAttributeLabel($e); }, $this->otherAttributes))]; }
/** * Adds an error about the specified attribute to the model object. * This is a helper method that performs message selection and internationalization. * @param \yii\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 = []) { $params['attribute'] = $model->getAttributeLabel($attribute); if (!isset($params['value'])) { $value = $model->{$attribute}; if (is_array($value)) { $params['value'] = 'array()'; } elseif (is_object($value) && !method_exists($value, '__toString')) { $params['value'] = '(object)'; } else { $params['value'] = $value; } } $model->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language)); }
/** * @param \yii\base\Model $model * @param string $attribute * @param \yii\web\View $view * @return string */ public function clientValidateAttribute($model, $attribute, $view) { $message = $this->uncheckedMessage ? $this->uncheckedMessage : Yii::t('yii', '{attribute} cannot be blank.', ['attribute' => $model->getAttributeLabel($attribute)]); return "(function(messages){if(!grecaptcha.getResponse()){messages.push('{$message}');}})(messages);"; }
/** * Generate a placeholder from attribute label * * @param Model $model * @param string $attr * @return string */ public static function placeholder(Model $model, $attr) { return $model->getAttributeLabel($attr) . ($model->isAttributeRequired($attr) ? ' *' : ''); }