/** * Method that validates fields of the entity based on its restrictions */ protected function validateFields() { $validator = new Validator($this->data, [], 'en'); //@todo: if use external i18n library? if ($this->action == 'new') { $validator->rules(array_merge($this->rulesNew, $this->rulesGlobal)); } else { $validator->rules(array_merge($this->rulesModify, $this->rulesGlobal)); } if (!$validator->validate()) { $this->errors = array_merge($this->errors, $validator->errors()); } }
static function callValitron($arr, $rules, $throw = TRUE) { $v = new Validator($arr); $v->rules($rules); if (!$v->validate()) { if ($throw) { throw (new \RoyalMail\Exception\RequestException())->withErrors($v->errors()); } return $v->errors(); } else { return TRUE; } }
/** * Validate the IodefElement's attributes and value. * @return boolval */ protected function validate() { // If attributes are set, validate them. if (!empty($this->attributes)) { // It's possible that there are no rules for the attributes available. // If so, skip attribute validation. if (sizeof($this->getAttributeRules()) > 0) { $validate_attributes = new Validator($this->attributes); $validate_attributes->rules($this->getAttributeRules()); // Validation failed to succeeed, show errors if (!$validate_attributes->validate()) { echo 'Some attributes failed to pass the validator:'; foreach ($validate_attributes->errors() as $failed_attr) { echo ' ' . $failed_attr[0]; } return false; } } } // If a value is set, validate it. if (array_key_exists('value', $this)) { // It's possible that there is no value rule set. // If so, skip value validation. if (sizeof($this->getValueRule()) > 0) { $validate_value = new Validator(['value' => $this->value]); $validate_value->rules($this->getValueRule()); // Validation failed to succeeed, show errors if (!$validate_value->validate()) { echo 'The value failed to pass the validator:'; foreach ($validate_value->errors() as $message) { echo ' ' . $message; } return false; } } } return true; }
/** * Run validation against $this->rules * * @param array $data * @return Message|bool */ public static function validate(array $data) { $validator = new Validator($data); $validator->rules(static::$rules); if ($validator->validate()) { return true; } else { return new Message($validator->errors()); } }
public function validate($data) { $v = new Validator($data); $v->rules($this->rules); return $v->validate(); }
public function testInstanceOfWithAlternativeSyntaxInvalid() { $v = new Validator(array('attributeName' => new stdClass())); $v->rules(array('instanceOf' => array(array('attributeName', 'SomeOtherClassInAlternativeSyntaxInvalid')))); $v->validate(); $this->assertFalse($v->validate()); }
/** * Validate an object using the data and rules passed in * * Will update the last result with validation errors * * @param array $aData The data to validate * @param array $aRules The results to apply * * @return boolean true if valid false otherwise */ public function validate($aData, $aRules) { $oValidator = new Validator($aData); $oValidator->rules($aRules); $bValid = $oValidator->validate(); if (false === $bValid) { $this->aLastResult['result'] = false; $this->aLastResult['msg'] = $oValidator->errors(); } return $bValid; }
/** * @param array $values * @return boolean */ public function isValid(array $values) { $this->validator = new Valitron($values, [], 'en', $this->pathToTranslations); $this->validator->rules($this->rules); return $this->validator->validate(); }