This is determined by checking if the attribute is associated with a
[[\yii\validators\RequiredValidator|required]] validation rule in the
current [[scenario]].
Note that when the validator has a conditional validation applied using
[[\yii\validators\RequiredValidator::$when|$when]] this method will return
false regardless of the when condition because it may be called be
before the model is loaded with data.
public isAttributeRequired ( string $attribute ) : boolean | ||
$attribute | string | attribute name |
Результат | boolean | whether the attribute is required |
/** * @param \yii\base\Model $model * @param array[] $fields * * @return bool */ public static function hasRequiredFields($model, $fields) { foreach ($fields as $column) { if (!is_array($column)) { if ($column instanceof ActiveField && $model->isAttributeRequired($column->attribute)) { return true; } continue; } foreach ($column as $row) { if (!is_array($row)) { if ($row instanceof ActiveField && $model->isAttributeRequired($row->attribute)) { return true; } continue; } if (static::hasRequiredFields($model, $row)) { return true; } } } return false; }
/** * 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) ? ' *' : ''); }