yii\base\Model::getActiveValidators PHP Method

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]].
    public function getActiveValidators($attribute = null)
    {
        $validators = [];
        $scenario = $this->getScenario();
        foreach ($this->getValidators() as $validator) {
            if ($validator->isActive($scenario) && ($attribute === null || in_array($attribute, $validator->attributes, true))) {
                $validators[] = $validator;
            }
        }
        return $validators;
    }

Usage Example

 /**
  * @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;
 }
All Usage Examples Of yii\base\Model::getActiveValidators