getActiveValidators() public method

Returns the validators applicable to the current [[scenario]].
public getActiveValidators ( string $attribute = null ) : Validator[]
$attribute string the name of the attribute whose applicable validators should be returned. If this is null, the validators for ALL attributes in the model will be returned.
return yii\validators\Validator[] the validators applicable to the current [[scenario]].
Ejemplo n.º 1
0
 /**
  * @param UploadedFile $file
  * @return bool
  * @throws InvalidFileUploadException
  */
 protected function validateFile(UploadedFile $file)
 {
     $validators = $this->formModel->getActiveValidators($this->fileAttribute);
     foreach ($validators as $validator) {
         if ($validator->validate($file, $error) === false) {
             throw new InvalidFileUploadException($error);
         }
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Get client side validators attribute
  * @param \yii\base\Model $model Model
  * @param string $attribute Attribute name
  * @param \yii\web\View $view Current view
  * @return array
  */
 public static function getClientValidatorsAttribute(Model $model, $attribute, $view)
 {
     $validators = [];
     foreach ($model->getActiveValidators($attribute) as $validator) {
         /* @var $validator \yii\validators\Validator */
         $js = $validator->clientValidateAttribute($model, $attribute, $view);
         if ($validator->enableClientValidation && $js != '') {
             if ($validator->whenClient !== null) {
                 $js = "if (({$validator->whenClient})(attribute, value)) { {$js} }";
             }
             $validators[] = $js;
         }
     }
     return $validators;
 }
Ejemplo n.º 3
0
 /**
  * If `maxlength` option is set true and the model attribute is validated by a string validator,
  * the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]].
  * @param Model $model the model object
  * @param string $attribute the attribute name or expression.
  * @param array $options the tag options in terms of name-value pairs.
  */
 private static function normalizeMaxLength($model, $attribute, &$options)
 {
     if (isset($options['maxlength']) && $options['maxlength'] === true) {
         unset($options['maxlength']);
         $attrName = static::getAttributeName($attribute);
         foreach ($model->getActiveValidators($attrName) as $validator) {
             if ($validator instanceof StringValidator && $validator->max !== null) {
                 $options['maxlength'] = $validator->max;
                 break;
             }
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Generates a text input tag for the given model attribute.
  * This method will generate the "name" and "value" tag attributes automatically for the model attribute
  * unless they are explicitly specified in `$options`.
  * @param Model $model the model object
  * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
  * about attribute expression.
  * @param array $options the tag options in terms of name-value pairs. These will be rendered as
  * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
  * See [[renderTagAttributes()]] for details on how attributes are being rendered.
  * The following special options are recognized:
  *
  * - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated
  *   by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]].
  *   This is available since version 2.0.3.
  *
  * @return string the generated input tag
  */
 public static function activeTextInput($model, $attribute, $options = [])
 {
     if (isset($options['maxlength']) && $options['maxlength'] === true) {
         unset($options['maxlength']);
         $attrName = static::getAttributeName($attribute);
         foreach ($model->getActiveValidators($attrName) as $validator) {
             if ($validator instanceof StringValidator && $validator->max !== null) {
                 $options['maxlength'] = $validator->max;
                 break;
             }
         }
     }
     return static::activeInput('text', $model, $attribute, $options);
 }