/** * Find the maxlength parameter for an attribute's model. * * This method searches for a yii\validators\StringValidator among all the active validators (based on the current * model scenario). If it founds one, it returns the max length parameter value. If no such value can be found because it is not * defined or because no StringValidator is active, $defaultValue is returned. * * @param yii\base\Model $model * @param string $attribute the attribute name * @return integer | null the maxlength setting * @see yii\validators\StringValidator */ public static function getMaxLength($model, $attribute, $defaultValue = null) { $maxLength = null; foreach ($model->getActiveValidators($attribute) as $validator) { if ($validator instanceof yii\validators\StringValidator) { $maxLength = $validator->max; break; } } return $maxLength !== null ? $maxLength : $defaultValue; }