Validate the grade.

For performance reason we do not validate that the grade is a valid item of the
scale associated with the competency or framework.

@param int $value The value.
@return true|lang_string
964703
protected function validate_grade($value) {
        if ($value !== null && $value <= 0) {
            return new lang_string('invalidgrade', 'core_competency');
        }

        $action = $this->get('action');
        if ($value === null && $action == self::ACTION_COMPLETE) {
            return new lang_string('invalidgrade', 'core_competency');

        } else if ($value !== null && $action == self::ACTION_LOG) {
            return new lang_string('invalidgrade', 'core_competency');
        }

        if ($value !== null) {
            // TODO MDL-52243 Use a core method to validate the grade_scale item.
            // Check if grade exist in the scale item values.
            $competency = $this->get_competency();
            if (!array_key_exists($value - 1, $competency->get_scale()->scale_items)) {
                return new lang_string('invalidgrade', 'core_competency');
            }
        }

        return true;
    }
710656
protected function validate_ruletype($value) {
        if ($value === null) {
            return true;
        }

        if (!class_exists($value) || !is_subclass_of($value, 'core_competency\\competency_rule')) {
            return new lang_string('invaliddata', 'error');
        }

        return true;
    }
1221721
public function isValid($value)
    {
        if ($this->skipValidation)
            return true;

        $datatype = $this->validation['datatype'];

        //NULL check, empty strings are considered null
        if(in_array($datatype, array('string', 'url', 'email', 'slug', 'slugwithslash', 'html', 'binary','json'))
          && strlen(trim($value)) == 0)
            $value = null;

        if ($this->validation['nullable'] === false && $value === null && $datatype != 'boolean') {
            $this->failureCode = 'nullable';
            $this->failureMessage = 'cannot be empty';
            return false;
        }

        //Nothing more to validate if the value is null...
        if ($value === null)
            return true;


        //Check date makes sense
        if ($datatype === 'date') {

            //todo: not sure how to check validity of date... it's already a DateTime instance.
            if (false) {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is an invalid date';
                return false;
            }
        }


        //Validate MIN
        $min = $this->validation['min'];

        if ($min != null) {
            if ($datatype === 'float') {
                if($value < floatval($min)) {
                    $this->failureCode = 'min';
                    $this->failureMessage = 'is less than the minimum value';
                    return false;
                }
            } else if ($datatype === 'int') {
                if($value < intval($min)) {
                    $this->failureCode = 'min';
                    $this->failureMessage = 'is less than the minimum value';
                    return false;
                }
            } else {

                if (is_string($value) && strlen($value) < intval($min)) {
                    $this->failureCode = 'minlength';
                    $this->failureMessage = 'must be at least '.($min).' characters';
                    return false;
                }
            }
        }


        //Validate MAX
        $max = $this->validation['max'];

        if ($max != null) {
            if ($datatype === 'float') {
                if($value > floatval($max)) {
                    $this->failureCode = 'max';
                    $this->failureMessage = 'is more than the maximum value';
                    return false;
                }
            } else if ($datatype === 'int') {
                if($value > intval($max)) {
                    $this->failureCode = 'max';
                    $this->failureMessage = 'is more than the maximum value';
                    return false;
                }
            } else {

                $maxbytes = intval($max);

                if(intval($max) < 255)
                {
                    // count characters

                    if (is_string($value) && StringUtils::charCount($value) > intval($max)) {
                        $this->failureCode = 'maxlength';
                        $this->failureMessage = 'must be a maximum of '.($max).' characters';
                        return false;
                    }

                    $maxbytes = 255;

                }

                // count bytes
                if (is_string($value) && StringUtils::byteCount($value) > intval($maxbytes)) {
                    $this->failureCode = 'maxlength';
                    $this->failureMessage = 'must be a maximum of '.($maxbytes).' bytes';
                    return false;
                }
            }
        }

        if($datatype === 'slug')
        {
            if(!SlugUtils::isSlug($value, false))
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid slug, cannot contain slashes';
                return false;
            }
        }

        if($datatype === 'slugwithslash')
        {
            if(!SlugUtils::isSlug($value, true))
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid slug';
                return false;
            }
        }

        if($datatype === 'url')
        {
            if(!URLUtils::isUrl($value))
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid URL';
                return false;
            }
        }

        if($datatype === 'email')
        {
            if(!EmailUtils::isEmailAddress($value))
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid email address';
                return false;
            }
        }

        if($datatype === 'json')
        {
            if(!JSONUtils::isValid($value)) //todo: get exact error message here
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid json string';
                return false;
            }
        }

        //Validate MATCH expression
        $match = $this->validation['match'];

        if ($match != null) {
            // Automatically escape unescaped slashes in the match before running it
            $match = preg_replace('/([^\\\\])\//', '$1\\/', $match);

            if (preg_match('/'.$match.'/s', $value) === 0) {
                $this->failureCode = 'invalid';
                //$this->failureMessage = 'is invalid (' . substr($value, 0, 255) . ')';
                $this->failureMessage = 'is invalid';
                return false;
            }
        }

        // Validate all custom functions
        foreach ( $this->validation['callback'] as $callback ) {
            if (!empty($callback) && call_user_func($callback, $value) === false) {
                $this->failureCode = $callback;
                $this->failureMessage = 'is invalid';
                return false;
            }
        }

        return true;
    }
1257041
public function isValueValid($value) {
		return $value instanceof Type\CreativeWork || $value instanceof Type\HowToSection || $value instanceof Type\HowToStep || $value instanceof DataType\TextType;
	}
698285
public function isValueValid($value) {
		return $value instanceof DataType\TextType || $value instanceof Type\Thing || $value instanceof DataType\URLType;
	}
207205
public function isValid($value) {
		$valid = true;
		if(!empty($this->validation)) {
			if(!empty($this->label))
				$element = $this->label;
			elseif(!empty($this->_attributes["placeholder"]))
				$element = $this->_attributes["placeholder"];
			else
				$element = $this->_attributes["name"];

			if(substr($element, -1) == ":")
				$element = substr($element, 0, -1);

			foreach($this->validation as $validation) {
				if(!$validation->isValid($value)) {
					/*In the error message, %element% will be replaced by the element's label (or 
					name if label is not provided).*/
					$this->_errors[] = str_replace("%element%", $element, $validation->getMessage());
					$valid = false;
				}	
			}
		}
		return $valid;
	}
333365
public function isValid($value)
    {
        $this->_setValue($value);

        if (!$value || !is_string($value) && !is_int($value) && !is_float($value)) {
            $this->_error(self::INVALID_PRICE);
            return false;
        }

        if (false === $this->_options['negative'] && '-' === $value[0]) {
            $this->_error(self::PRICE_NEGATIVE, $value);
            return false;
        }

        $status = @preg_match($this->_pattern, $value);
        if (false === $status) {
            $this->_error(self::INVALID_PRICE);
            return false;
        }

        if (!$status) {
            $this->_error(self::INVALID_PRICE);
            return false;
        }

        return true;
    }
1183522
public function validate($value)
    {
        if (!$this->test($value)) {
            // TODO: Use exception after 4.0
            $this->setError($this->formatMessage($this->getMessage(), $value));

            return false;
        }

        return true;
    }
466898
public function isValid($value)
    {
        if (!is_string($value)) {
            $this->_error(self::INVALID);
            return false;
        }

        $this->_setValue($value);
        if ($this->_encoding !== null) {
            $length = iconv_strlen($value, $this->_encoding);
        } else {
            $length = iconv_strlen($value);
        }

        if ($length < $this->_min) {
            $this->_error(self::TOO_SHORT);
        }

        if (null !== $this->_max && $this->_max < $length) {
            $this->_error(self::TOO_LONG);
        }

        if (count($this->_messages)) {
            return false;
        } else {
            return true;
        }
    }
1233806
public function isValid($value)
    {
        if (!is_scalar($value) || !is_numeric($value)) {
            $this->error(self::INVALID);

            return false;
        }

        $length = strlen($value);

        if ($length < 9) {
            $this->error(self::TOO_SHORT);

            return false;
        }

        if ($length > 9) {
            $this->error(self::TOO_LONG);

            return false;
        }

        if (!preg_match($this->pattern, $value, $matches)) {
            $this->error(self::NO_MATCH);

            return false;
        }

        // no digit group can be all 0's
        /** @var array $matches */
        foreach ($matches as $match) {
            if (0 === (int) $match) {
                $this->error(self::NO_MATCH);

                return false;
            }
        }

        // invalid first digit groups 666 or 900-999
        /** @noinspection TypeUnsafeComparisonInspection */
        if ($matches[1] == 666 || $matches[1] >= 900) {
            $this->error(self::NO_MATCH);

            return false;
        }

        // invalid for advertisements
        if ($value >= 987654320 && $value <= 987654329) {
            $this->error(self::NO_MATCH);

            return false;
        }
        // handle famous instance of 1938
        /** @noinspection TypeUnsafeComparisonInspection */
        if ($value == '078051120') {
            $this->error(self::NO_MATCH);

            return false;
        }

        return true;
    }
638976
public function set($value) {
        if (empty($this->grade->id)) {
            if (empty($value)) {
                return false;
            }

            $gradeitem = $this->grade->grade_item;

            // Fill in arbitrary grade to be excluded.
            $gradeitem->update_final_grade(
                $this->grade->userid, null, 'singleview', null, FORMAT_MOODLE
            );

            $gradeparams = array(
                'userid' => $this->grade->userid,
                'itemid' => $this->grade->itemid
            );

            $this->grade = grade_grade::fetch($gradeparams);
            $this->grade->grade_item = $gradeitem;
        }

        $state = $value == 0 ? false : true;

        $this->grade->set_excluded($state);

        $this->grade->grade_item->get_parent_category()->force_regrading();
        return false;
    }
1260685
public function isValid($value)
    {
        $ok = true;
        
        if (!preg_match('/^[0-9]+(\.[0-9]+)?$/', $value)) {
            $this->error(self::SYNTAX);
            return false;
        }
        
        if ($value < $this->options['min']) {
            $this->error(self::MIN);
            $ok = false;
        }
        
        if ($value > $this->options['max']) {
            $this->error(self::MAX);
            $ok = false;
        }
        
        $parts = explode('.', $value);
        if (isset($parts[1]) && strlen($parts[1]) > $this->options['precision']) {
            if ($this->options['precision']) {
                $this->error(self::PRECISION);
            } else {
                $this->error(self::NO_COMMA);
            }
            $ok = false;
        }

        return $ok;
    }
593835
public function isValueValid($value) {
		return $value instanceof DataType\TextType || $value instanceof Type\ThingType || $value instanceof DataType\URLType;
	}
96204
public function isValueValid($value) {
		return $value instanceof DataType\NumberType || $value instanceof Type\QuantitativeValue || $value instanceof DataType\TextType;
	}
841269
protected function _validate($value)
    {
        $value = trim(str_replace('\\', '/', $value));
        return (is_string($value) && (preg_match(self::PATTERN, $value)) == 1);
    }
126182
public function normalize($value)
    {
        if (is_null($value) || $value === '') {
            return;
        }
        if (is_bool($value)) {
            return $value;
        }

        if ($value === $this->valueTrue) {
            return true;
        }

        if ($value === $this->valueFalse) {
            return false;
        }

        if (is_numeric($value)) {
            return (bool) $value;
        }

        throw new ValidationException([$this->name => 'Must be a boolean value']);
    }
1319492
protected function isValueValid($value) {
		$this->checkValue($value);
		
		if ($this->validatable === null) {
			return true;
		}
		
		return $this->validatable->testEiFieldValue($this->eiu, $value);
	}