yii\base\Model::validate PHP Method

validate() public method

This method executes the validation rules applicable to the current [[scenario]]. The following criteria are used to determine whether a rule is currently applicable: - the rule must be associated with the attributes relevant to the current scenario; - the rules must be effective for the current scenario. This method will call Model::beforeValidate and Model::afterValidate before and after the actual validation, respectively. If Model::beforeValidate returns false, the validation will be cancelled and Model::afterValidate will not be called. Errors found during the validation can be retrieved via Model::getErrors, Model::getFirstErrors and Model::getFirstError.
public validate ( array $attributeNames = null, boolean $clearErrors = true ) : boolean
$attributeNames array list of attribute names that should be validated. If this parameter is empty, it means any attribute listed in the applicable validation rules should be validated.
$clearErrors boolean whether to call [[clearErrors()]] before performing validation
return boolean whether the validation is successful without any error.
    public function validate($attributeNames = null, $clearErrors = true)
    {
        if ($clearErrors) {
            $this->clearErrors();
        }
        if (!$this->beforeValidate()) {
            return false;
        }
        $scenarios = $this->scenarios();
        $scenario = $this->getScenario();
        if (!isset($scenarios[$scenario])) {
            throw new InvalidParamException("Unknown scenario: {$scenario}");
        }
        if ($attributeNames === null) {
            $attributeNames = $this->activeAttributes();
        }
        foreach ($this->getActiveValidators() as $validator) {
            $validator->validateAttributes($this, $attributeNames);
        }
        $this->afterValidate();
        return !$this->hasErrors();
    }

Usage Example

Esempio n. 1
0
 /**
  * Validate attribute & throw exception
  *
  * @param array $attributeNames
  * @param boolean $clearErrors
  * @param boolean $throwException
  * @return boolean
  * @throws ModelException
  */
 public function validate($attributeNames = null, $clearErrors = true, $throwException = true)
 {
     if (!parent::validate($attributeNames, $clearErrors) && $throwException) {
         throw new ModelException($this, Code::GENERAL_INVALID_USER_INPUT);
     }
     return !$this->hasErrors();
 }
All Usage Examples Of yii\base\Model::validate