Validate field based on its special type

@return bool
543516
public function validateSpecialFields()
    {
        try {
            $validator = SpecialFormFieldValidatorFactory::create($this, $this->getValue());
            return $validator->validate();
        } catch (FormFieldException $exception) {
            // @TODO this is very suboptimal. do not return? Can not validate this type, not validator
            return false;
        }
    }
588867
protected function validateParentCategory()
    {
        $field = 'parent_id';
        $value = $this->getSubmitted($field);

        if (empty($value)) {
            return null;
        }

        $label = $this->translation->text('Parent category');

        if (!is_numeric($value)) {
            $this->setErrorNumeric($field, $label);
            return false;
        }

        $category = $this->getSubmitted('category');

        if (isset($category['category_id']) && $category['category_id'] == $value) {
            $this->setErrorInvalid($field, $label);
            return false;
        }

        $parent_category = $this->category->get($value);

        if (empty($parent_category['category_id'])) {
            $this->setErrorUnavailable($field, $label);
            return false;
        }

        return true;
    }
236363
function valid()
    {
        list($this->_fieldId, $this->_field) = each($this->_fields);
        return $this->_fieldId !== null;
    }
51992
protected function validateCommons(/** @noinspection PhpUnusedParameterInspection */
        &$document_data, &$data, array $data_pointer, array $parent_data_pointer, ISchema $document, $schema, ValidationResult $bag, array &$defaults = null): bool
    {
        $ok = true;

        // type
        if (property_exists($schema, 'type')) {
            if (is_string($schema->type)) {
                if (!$this->helper->typeExists($schema->type)) {
                    throw new SchemaKeywordException(
                        $schema,
                        'type',
                        $schema->type,
                        "'type' keyword contains unknown value: " . $schema->type
                    );
                }
            } elseif (is_array($schema->type)) {
                /** @noinspection PhpParamsInspection */
                if (count($schema->type) === 0) {
                    throw new SchemaKeywordException(
                        $schema,
                        'type',
                        $schema->type,
                        "'type' keyword must not be an empty array"
                    );
                }
                /** @noinspection PhpParamsInspection */
                if ($schema->type != array_unique($schema->type)) {
                    throw new SchemaKeywordException(
                        $schema,
                        'type',
                        $schema->type,
                        "'type' keyword contains duplicate items"
                    );
                }
                foreach ($schema->type as $type) {
                    if (!is_string($type)) {
                        throw new SchemaKeywordException(
                            $schema,
                            'type',
                            $type,
                            "'type' keyword must have only strings if array, found " . gettype($type)
                        );
                    }
                    if (!$this->helper->typeExists($type)) {
                        throw new SchemaKeywordException(
                            $schema,
                            'type',
                            $type,
                            "'type' keyword contains unknown value: " . $type
                        );
                    }
                }
                unset($type);
            } else {
                throw new SchemaKeywordException(
                    $schema,
                    'type',
                    $schema->type,
                    "'type' keyword must be a string or an array of strings, " . gettype($schema->type) . " given"
                );
            }

            if (!$this->helper->isValidType($data, $schema->type)) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'type', [
                    'expected' => $schema->type,
                    'used' => $this->helper->type($data, true),
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            }
        }

        // const
        if (property_exists($schema, 'const')) {
            if (!$this->helper->equals($data, $schema->const, $defaults)) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'const', [
                    'expected' => $schema->const,
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            }
        }

        // enum
        if (property_exists($schema, 'enum')) {
            if (!is_array($schema->enum)) {
                throw new SchemaKeywordException(
                    $schema,
                    'enum',
                    $schema->enum,
                    "'enum' keyword must be an array, " . gettype($schema->enum) . " given"
                );
            }
            if (count($schema->enum) === 0) {
                throw new SchemaKeywordException(
                    $schema,
                    'enum',
                    $schema->enum,
                    "'enum' keyword must not be empty"
                );
            }

            $found = false;
            foreach ($schema->enum as $v) {
                if ($this->helper->equals($data, $v, $defaults)) {
                    $found = true;
                    break;
                }
            }
            unset($v);
            if (!$found) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'enum', [
                    'expected' => $schema->enum,
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            }
            unset($found);
        }

        return $ok;
    }
967633
public function validate()
    {
        $valid = true;
        $message = null;
        $type = $this->Type;

        // Check if empty strings
        switch ($type) {
            case 'URL':
            case 'Email':
            case 'Phone':
                if ($this->{$type} == '') {
                    $valid = false;
                    $message = _t(
                        __CLASS__ . '.VALIDATIONERROR_EMPTY'.strtoupper($type),
                        'You must enter a {TypeLabel}',
                        [
                            'TypeLabel' => $this->TypeLabel
                        ]
                    );
                }
                break;
            case 'File':
            case 'SiteTree':
                if ($type && empty($this->{$type.'ID'})) {
                    $valid = false;
                    $message = _t(
                        __CLASS__ . '.VALIDATIONERROR_OBJECT',
                        'Please select a {TypeLabel}',
                        [
                            'TypeLabel' => $this->TypeLabel
                        ]
                    );
                }
                break;
        }
        // if its already failed don't bother checking the rest
        if ($valid) {
            switch ($type) {
                case 'URL':
                    $allowedFirst = ['#', '/'];
                    if (!in_array(substr($this->URL, 0, 1), $allowedFirst) && !filter_var($this->URL, FILTER_VALIDATE_URL)) {
                        $valid = false;
                        $message = _t(
                            __CLASS__ . '.VALIDATIONERROR_VALIDURL',
                            'Please enter a valid URL.  Be sure to include http:// for an external URL. or begin your internal url/anchor with a "/" character'
                        );
                    }
                    break;
                case 'Email':
                    if (!filter_var($this->Email, FILTER_VALIDATE_EMAIL)) {
                        $valid = false;
                        $message = _t(
                            __CLASS__ . '.VALIDATIONERROR_VALIDEMAIL',
                            'Please enter a valid Email address'
                        );
                    }
                    break;
                case 'Phone':
                    if (!preg_match("/^\+?[0-9a-zA-Z\-\s]*[\,\#]?[0-9\-\s]*$/", $this->Phone)) {
                        $valid = false;
                        $message = _t(
                            __CLASS__ . '.VALIDATIONERROR_VALIDPHONE',
                            'Please enter a valid Phone number'
                        );
                    }
                    break;
            }
        }

        $result = ValidationResult::create();
        if (!$valid) {
            $result->addError($message);
        }

        $this->extend('updateValidate', $result);

        return $result;
    }
442831
protected function validateNumber(/** @noinspection PhpUnusedParameterInspection */
        &$document_data, &$data, array $data_pointer, array $parent_data_pointer, ISchema $document, $schema, ValidationResult $bag): bool
    {
        $ok = true;

        // minimum, exclusiveMinimum
        if (property_exists($schema, 'minimum')) {
            if (!is_int($schema->minimum) && !is_float($schema->minimum)) {
                throw new SchemaKeywordException(
                    $schema,
                    'minimum',
                    $schema->minimum,
                    "'minimum' keyword must be an integer or a float, " . gettype($schema->minimum) . " given"
                );
            }

            $exclusive = false;
            if (property_exists($schema, 'exclusiveMinimum')) {
                if (!is_bool($schema->exclusiveMinimum)) {
                    throw new SchemaKeywordException(
                        $schema,
                        'exclusiveMinimum',
                        $schema->exclusiveMinimum,
                        "'exclusiveMinimum' keyword must be a boolean if 'minimum' keyword is present, " . gettype($schema->exclusiveMinimum) . " given"
                    );
                }
                $exclusive = $schema->exclusiveMinimum;
            }

            if ($exclusive && $data == $schema->minimum) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'exclusiveMinimum', [
                    'min' => $schema->minimum
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            } elseif ($data < $schema->minimum) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'minimum', [
                    'min' => $schema->minimum
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            }
        } elseif (property_exists($schema, 'exclusiveMinimum')) {
            if (!is_int($schema->exclusiveMinimum) && !is_float($schema->exclusiveMinimum)) {
                throw new SchemaKeywordException(
                    $schema,
                    'exclusiveMinimum',
                    $schema->exclusiveMinimum,
                    "'exclusiveMinimum' keyword must be an integer or a float if 'minimum' keyword is not present, " . gettype($schema->exclusiveMinimum) . " given"
                );
            }
            if ($data <= $schema->exclusiveMinimum) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'exclusiveMinimum', [
                    'min' => $schema->exclusiveMinimum
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            }
        }

        // maximum, exclusiveMaximum
        if (property_exists($schema, 'maximum')) {
            if (!is_int($schema->maximum) && !is_float($schema->maximum)) {
                throw new SchemaKeywordException(
                    $schema,
                    'maximum',
                    $schema->maximum,
                    "'maximum' keyword must be an integer or a float, " . gettype($schema->maximum) . " given"
                );
            }

            $exclusive = false;
            if (property_exists($schema, 'exclusiveMaximum')) {
                if (!is_bool($schema->exclusiveMaximum)) {
                    throw new SchemaKeywordException(
                        $schema,
                        'exclusiveMaximum',
                        $schema->exclusiveMaximum,
                        "'exclusiveMaximum' keyword must be a boolean is 'maximum' keyword is present, " . gettype($schema->exclusiveMaximum) . " given"
                    );
                }
                $exclusive = $schema->exclusiveMaximum;
            }

            if ($exclusive && $data == $schema->maximum) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'exclusiveMaximum', [
                    'max' => $schema->maximum
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            } elseif ($data > $schema->maximum) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'maximum', [
                    'max' => $schema->maximum
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            }
        } elseif (property_exists($schema, 'exclusiveMaximum')) {
            if (!is_int($schema->exclusiveMaximum) && !is_float($schema->exclusiveMaximum)) {
                throw new SchemaKeywordException(
                    $schema,
                    'exclusiveMaximum',
                    $schema->exclusiveMaximum,
                    "'exclusiveMaximum' keyword must be an integer or a float if 'maximum' keyword is not present, " . gettype($schema->exclusiveMaximum) . " given"
                );
            }
            if ($data >= $schema->exclusiveMaximum) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'exclusiveMaximum', [
                    'max' => $schema->exclusiveMaximum
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            }
        }

        // multipleOf
        if (property_exists($schema, 'multipleOf')) {
            if (!is_int($schema->multipleOf) && !is_float($schema->multipleOf)) {
                throw new SchemaKeywordException(
                    $schema,
                    'multipleOf',
                    $schema->multipleOf,
                    "'multipleOf' keyword must be an integer or a float, " . gettype($schema->multipleOf) . " given"
                );
            }
            if ($schema->multipleOf <= 0) {
                throw new SchemaKeywordException(
                    $schema,
                    'multipleOf',
                    $schema->multipleOf,
                    "'multipleOf' keyword must be greater than 0"
                );
            }
            if (!$this->helper->isMultipleOf($data, $schema->multipleOf)) {
                $ok = false;
                $bag->addError(new ValidationError($data, $data_pointer, $parent_data_pointer, $schema, 'multipleOf', [
                    'divisor' => $schema->multipleOf
                ]));
                if ($bag->isFull()) {
                    return false;
                }
            }
        }

        return $ok;
    }
1126039
protected function checkType ()
    {
        $typeField = $this->getFields('002@/00');
        if (count($typeField) === 1) {
            $typeSubfield = reset($typeField)->getNthSubfield('0', 0);
            if ($typeSubfield) {
                $typeCode = $typeSubfield->getValue();
                if ($typeCode === 'T') {
                    return true;
                }
            }
        }
    }
396859
private function validateLowerThan(): bool
    {
        if ($this->options['max'] !== null) {
            if ($this->options['inclusive'] === false && $this->value >= $this->options['max']) {
                $this->setError(self::VALIDATOR_ERROR_NUMBER_MUST_BE_LOWER_THAN_MAX);
                return false;
            }

            if ($this->value > $this->options['max']) {
                $this->setError(self::VALIDATOR_ERROR_NUMBER_MUST_BE_LOWER_OR_EQUAL_TO_MAX);
                return false;
            }
        }
        return true;
    }
372033
public function validate()
    {
        /* @var $model \yii\db\ActiveRecord */
        $valid = true;

        foreach ($this->_relationsToSave as $model) {
            $model->{$this->relPrimaryKey} = ($this->owner->primaryKey) ? $this->owner->primaryKey : null;

            $attributes = array_diff($model->activeAttributes(), [$this->relPrimaryKey]);

            if (!$model->validate($attributes)) {
                $this->owner->addErrors($model->getErrors());

                $valid = false;
            }
        }

        return $valid;
    }
1263436
protected function validateEditField()
    {
        $this->setSubmitted('field');
        $this->setSubmitted('update', $this->data_field);

        $this->validateComponent('field');

        return !$this->hasErrors();
    }
420689
private function validate()
    {
        if (!$this->cfg['validate']) {
            return true;
        }
        $formIsValid = true;
        foreach ($this->currentFields as $field) {
            $isValid = $field->validate();
            $formIsValid = $formIsValid && $isValid;
        }
        return $formIsValid;
    }
1246548
protected function validate()
    {
        $props = $this->getProperties();
        foreach ($props as $name => $oneProperty) {
            $options = [];
            if (array_key_exists(FFCST::PROPERTY_OPTIONS, $oneProperty)) {
                $options = $oneProperty[FFCST::PROPERTY_OPTIONS];
            }
            if (in_array(FFCST::OPTION_REQUIRED, $options)) {
                $getter = 'get' . \FreeFW\Tools\PBXString::toCamelCase($name, true);
                $value  = $this->$getter();
                $public = $name;
                if (array_key_exists(FFCST::PROPERTY_PUBLIC, $oneProperty)) {
                    $public = $oneProperty[FFCST::PROPERTY_PUBLIC];
                }
                if ($value === null || (is_string($value) && $value == '')) {
                    $this->addError(
                        FFCST::ERROR_REQUIRED,
                        sprintf('%s field is required !', $public),
                        \FreeFW\Core\Error::TYPE_PRECONDITION,
                        $public
                    );
                }
            }
        }
        return $this;
    }
1371385
protected function validateData()
    {
        $field = 'data';

        if ($this->isExcluded($field)) {
            return null;
        }

        $value = $this->getSubmitted($field);

        if (!isset($value)) {
            $this->unsetSubmitted($field);
            return null;
        }

        if (!is_array($value)) {
            $this->setErrorInvalid($field, $this->translation->text('Data'));
            return false;
        }

        return true;
    }
253370
protected function validateType () : bool
	{
		// Checks if video type is allowed
		if ($this->validateMimeType() == FALSE) return FALSE;
		// Checks if video size is valid for upload
		if ($this->validateFileSize() == FALSE) return FALSE;
		// Finally uplods the file after successful evaluation
		if ($this->uploadFile() == FALSE) return FALSE;

		return TRUE;
	}
303236
public function validate(Validation $validation, $field) : bool
    {
        if (!is_string($field)) {
            throw new Exception(
                "Field name must be a string"
            );
        }



        $label = $this->getOption("label");

        if (empty($label)) {
            $label = $validation->getLabel($field);
        }



        $value = $validation->getValue($field);

        if ($this->isSetOption("allowEmpty") && empty($value)) {
            return true;
        }



        $uuidRegEx = "/^\{?[A-Z0-9]{8}-[A-Z0-9]{4}-[1-5][A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{12}\}?$/i";

        if (!preg_match($uuidRegEx, $value)) {
            $message = $this->getOption("message");

            if (empty($message)) {
                $message = "Field :field must be a valid UUID";
            }



            $replacePairs = [
                ":field" => $label,
            ];

            $validation->appendMessage(
                new Message(
                    strtr($message, $replacePairs),
                    $field,
                    "Uuid"
                )
            );

            return false;
        }



        $allowedVersions = $this->getOption("allowedVersions");

        if (empty($allowedVersions)) {
            $allowedVersions = [1, 2, 3, 4, 5];
        }



        if (!in_array(substr($value, 14, 1), $allowedVersions)) {
            $message = $this->getOption("messageVersion");

            if (empty($message)) {
                $message = "Field :field must be one of the following UUID versions: :versions";
            }



            $replacePairs = [
                ":field" => $label,
                ":versions" => implode(", ", $allowedVersions),
            ];

            $validation->appendMessage(
                new Message(
                    strtr($message, $replacePairs),
                    $field,
                    "Uuid"
                )
            );

            return false;
        }

        return true;
    }
263042
private function validateLowerThan(): bool
    {
        if ($this->options['inclusive'] === true) {
            $isValid = \mb_strlen($this->value, 'UTF-8') <= $this->options['length'] ? true : false;
            if (!$isValid) {
                $this->setError(self::VALIDATOR_ERROR_STRING_LENGTH_MUST_BE_LOWER_OR_EQUAL_TO_MAX);
            }
            return $isValid;
        }

        $isValid = \mb_strlen($this->value, 'UTF-8') < $this->options['length'] ? true : false;
        if (!$isValid) {
            $this->setError(self::VALIDATOR_ERROR_STRING_LENGTH_MUST_BE_LOWER_THAN_MAX);
        }
        return $isValid;
    }
1277961
protected function validatePost()
    {
        $oFormValidation = Factory::service('FormValidation');

        $aRules = [
            'ref'             => 'trim',
            'state'           => 'trim|required',
            'dated'           => 'trim|required|valid_date',
            'currency'        => 'trim|required|callback__callbackValidCurrency',
            'terms'           => 'trim|is_natural',
            'customer_id'     => 'trim',
            'additional_text' => 'trim',
            'items'           => '',
        ];

        $aRulesFV = [];
        foreach ($aRules as $sKey => $sRules) {
            $aRulesFV[] = [
                'field' => $sKey,
                'label' => '',
                'rules' => $sRules,
            ];
        }

        $oFormValidation->set_rules($aRulesFV);

        $oFormValidation->set_message('required', lang('fv_required'));
        $oFormValidation->set_message('valid_date', lang('fv_valid_date'));
        $oFormValidation->set_message('is_natural', lang('fv_is_natural'));
        $oFormValidation->set_message('valid_email', lang('fv_valid_email'));

        return $oFormValidation->run($this);
    }