Validation rules setter.

@param mixed $table Name or instance of the Table
@param string $field Field name
@param \Cake\Validation\Validator $validator Validator instance
@param mixed[] $options Field options
@return \Cake\Validation\Validator
427933
public function setValidationRules($table, string $field, Validator $validator, array $options = []) : Validator
    {
        $handler = self::getByTableField($table, $field);
        $validator = $handler->setValidationRules($validator, $options);
        if (! $validator instanceof Validator) {
            throw new RuntimeException(
                sprintf('Field Handler returned value must be an instance of %s.', Validator::class)
            );
        }

        return $validator;
    }
1164615
public function validate($field, $value, $validator) {

		if(empty($value)) return false;
		if(!filter_var($value, FILTER_VALIDATE_URL)) return false;

		$url = parse_url($value);
		if(!in_array($url['scheme'], $this->allowed_protocols)) return false;

		if($this->allowed_domains === null) return true;

		return in_array($url['host'], $this->allowed_domains);

	}
330144
public function createValidator($validatorIdentifier, array $options = [])
    {
        $validatorPresets = $this->getRootForm()->getValidatorPresets();
        if (isset($validatorPresets[$validatorIdentifier]) && is_array($validatorPresets[$validatorIdentifier]) && isset($validatorPresets[$validatorIdentifier]['implementationClassName'])) {
            $implementationClassName = $validatorPresets[$validatorIdentifier]['implementationClassName'];
            $defaultOptions = isset($validatorPresets[$validatorIdentifier]['options']) ? $validatorPresets[$validatorIdentifier]['options'] : [];

            $options = Arrays::arrayMergeRecursiveOverrule($defaultOptions, $options);

            $validator = new $implementationClassName($options);
            $this->addValidator($validator);
            return $validator;
        } else {
            throw new ValidatorPresetNotFoundException('The validator preset identified by "' . $validatorIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328710202);
        }
    }
34710
public function addRule(ValidatorRule $rule, $field, $flags = 0x00000) {
    $key = $this->getKey($field);
    $this->rules[$key][] = $rule;
    
    if ($rule instanceof FieldValidatorRule) {
      $rule->setField($field);
    }
    
    if ($flags & self::OPTIONAL) {
      $this->setOptional($field);
    }
  }
473610
public function setDomainValidator(
        ValidatorInterface $domainValidator,
        array $options = []
    ) {
        $this->domainValidator = get_class($domainValidator);
        $this->domainValidatorOptions = $options;
    }
268810
public function withValidator($validator)
    {
        // Find RTF (markdown, wysiwyg) based on their posted structure ($field["text"])
        $richTextFields = collect($this->all())->filter(function($value, $key) {
            return is_array($value) && array_key_exists("text", $value);
        })->keys()->all();

        // Initialize rules by getting all those which DO NOT refer to a RTF
        $newRules = collect($validator->getRules())
            ->filter(function($messages, $key) use($richTextFields) {
                return !in_array($key, $richTextFields);
            })
            ->all();

        // And then replace RTF rules with .text suffix
        collect($validator->getRules())->filter(function($messages, $key) use($richTextFields) {
            return in_array($key, $richTextFields);
        })->each(function($messages, $key) use(&$newRules) {
            $newRules["$key.text"] = $messages;
        });

        $validator->setRules($newRules);
    }
933762
public function addValidation($property, $validation, $options = array())
    {
        $validations = $this->getValidations($property);
        $validations[$validation] = $options;

        return $this;
    }
129545
private static function applyValidatorRules($validator, array &$rules)
    {
        $validatorRule = ValidatorRule::getInstance();
        foreach ($rules as $rule) {
            list($functionName, $arguments) = $validatorRule->parseRule($validator, $rule);
            $validator = \call_user_func_array([$validator, $functionName], $arguments);
        }

        return $validator;
    }
721552
public function validationEnabled(Validator $validator) : Validator
    {
        $className = App::shortName(get_class($this), 'Model/Table', 'Table');
        $config = (new ModuleConfig(ConfigType::MIGRATION(), $className))->parse();
        $config = json_encode($config);
        if (false === $config) {
            return $validator;
        }
        $config = json_decode($config, true);
        $factory = new FieldHandlerFactory();

        foreach ($config as $column) {
            $validator = $factory->setValidationRules($this, $column['name'], $validator);
        }

        return $validator;
    }
416236
public function validate($validator)
    {
        $values = $this->value;
        if (!$values) {
            return true;
        }
        if ($this->ajax) {
            return true;
        }

        $strValue = $values;
        if (is_array($strValue)) {
            $strValue = implode(',', $strValue);
        }
        $errorMessage = _t(
            'Select2Field.SOURCE_VALIDATION', "Please select a value within the list provided. %s is not a valid option", array('value' => $strValue)
        );
        $source = $this->getSourceAsArray();
        if (is_array($values)) {
            if (!array_intersect_key($source, array_flip($values))) {
                $validator->validationError(
                    $this->name, $errorMessage, "validation"
                );
                return false;
            }
        } else {
            if (!array_key_exists($this->value, $source)) {
                $validator->validationError(
                    $this->name, $errorMessage, "validation"
                );
                return false;
            }
        }
        return true;
    }
954009
public function setValidator(Validator $validator) {
        $this->_validator = $validator;

        // Only set model events if we plan on validating
        $this->on('model', $this);

        return $this;
    }
499960
protected function useValidator(Validator $validator)
    {
        if ($validator !== $this->validator9) {
            return true;

        }
        $set1 = substr($this->account, 0, 2);
        $set2 = substr($this->account, 0, 3);
        return ($set1 >= 51  && $set1 <= 59)
            || ($set2 >= 901 && $set2 <= 910);
    }
1158687
public function setValidator(Validator $validator)
    {
        $this->validator = $validator;
        
        $validator->configureRule($this);
        
        return $this;
    }
867226
public function validate($validator)
    {
        // Check if valid value is given
        $selected = $this->Value();
        $validValues = $this->getValidValues();

        if (strlen($selected)) {
            // Use selection rules to check which are valid
            foreach ($validValues as $formValue) {
                if ($this->isSelectedValue($formValue, $selected)) {
                    return true;
                }
            }
        } else {
            if ($this->getHasEmptyDefault() || !$validValues || in_array('', $validValues)) {
                // Check empty value
                return true;
            }
            $selected = '(none)';
        }

        // Fail
        $validator->validationError(
            $this->name,
            _t(
                'SilverStripe\\Forms\\DropdownField.SOURCE_VALIDATION',
                "Please select a value within the list provided. {value} is not a valid option",
                array('value' => $selected)
            ),
            "validation"
        );
        return false;
    }
1340584
public function addValidation(Validator $validator) {
		/** @var \Captcha\Model\Table\CaptchasTable $Captchas */
		$Captchas = TableRegistry::get('CaptchasValidator', ['class' => 'Captcha.Captchas']);

		$Captchas->setValidator(null, $validator);

		$Captchas->addBehavior('Captcha.Captcha');
		/** @var \Captcha\Model\Behavior\CaptchaBehavior $Captchas */
		$Captchas->addValidation($validator);
	}
406577
public function validator($validator, $condition = true)
    {
        if (!$condition) {
            return $this;
        }

        if ($validator instanceof \Closure) {
            $this->validators[] = $validator;
        } else {
            $this->rule($validator);
        }

        return $this;
    }
1239528
public function valid($options = array())
    {
        $options && $this->setOption($options);

        // Initialize the validation result to be true
        $this->result = true;

        $this->beforeValidate && call_user_func($this->beforeValidate, $this, $this->wei);

        foreach ($this->rules as $field => $rules) {
            $data = $this->getFieldData($field);

            /**
             * Process simple rule
             * FROM
             * 'username' => 'required'
             * TO
             * 'username' => array(
             *     'required' => true
             * )
             */
            if (is_string($rules)) {
                $rules = array($rules => true);
            } elseif ($rules instanceof BaseValidator) {
                $rules = array($rules);
            } elseif (!is_array($rules)) {
                throw new \InvalidArgumentException(sprintf(
                    'Expected argument of type array, string or instance of Wei\Validator\BaseValidator, "%s" given',
                    is_object($rules) ? get_class($rules) : gettype($rules)
                ));
            }

            // Make sure the "required" rule at first
            if (!isset($rules['required'])) {
                $isRequired = true;
            } else {
                $isRequired = (bool) $rules['required'];
                unset($rules['required']);
            }
            $rules = array('required' => $isRequired) + $rules;

            // Start validation
            foreach ($rules as $rule => $params) {
                // Prepare property options for validator
                $props = $this->prepareProps($field, $rule);

                // The current rule validation result
                /* @var $validator Validator\BaseValidator */
                $validator = null;
                $result = $this->validateOne($rule, $data, $params, $validator, $props);

                if (is_object($params)) {
                    $rule = get_class($params);
                }

                // Record the rule validators
                $this->ruleValidators[$field][$rule] = $validator;

                // If any rule is invalid, the result would always be false in the whole validation flow
                if (false === $result) {
                    $this->result = false;
                }

                // Record the valid/invalid rule
                $method = $result ? 'addValidRule' : 'addInvalidRule';
                $this->$method($field, $rule);

                // Trigger the ruleValid/ruleInvalid callback
                $callback = $result ? 'ruleValid' : 'ruleInvalid';
                if ($this->$callback && false === call_user_func($this->$callback, $rule, $field, $this, $this->wei)) {
                    return $this->result;
                }

                if ($result) {
                    // The field data is empty and optional, skip the remaining validation rules
                    /** @var $validator Validator\Required */
                    if ('required' === $rule && $validator->isInvalid($data)) {
                        break;
                    }
                } else {
                    // Break the validation flow when any field's rule is invalid
                    if ('required' === $rule || $this->breakRule || $this->skip) {
                        break;
                    }
                }
            }

            // Trigger the fieldValid/fieldInvalid callback
            $callback = $this->isFieldValid($field) ? 'fieldValid' : 'fieldInvalid';
            if ($this->$callback && false === call_user_func($this->$callback, $field, $this, $this->wei)) {
                return $this->result;
            }

            if (!$this->result && $this->skip) {
                continue;
            }

            // Break the validation flow when any field is invalid
            if (!$this->result && ($this->breakRule || $this->breakField)) {
                break;
            }
        }

        // Trigger the success/failure callback
        $callback = $this->result ? 'success' : 'failure';
        $this->$callback && call_user_func($this->$callback, $this, $this->wei);

        return $this->result;
    }