src/ResourceValidationsTrait.php
protected function validateFormat($field, ?string $val, $format, $required = true)
    {
        if ($val !== null) {
            $regexp = $this->getKnownFormat($format);
            if (!$regexp) {
                $regexp = $format;
            }
            $result = preg_match($regexp, $val);
        } else {
            $result = !$required;
        }

        if ($result) {
            $this->clearError($field, 'validFormat');
            return true;
        } else {
            $error = [
                "title" => "Invalid Format for Field `$field`",
                "detail" => "Value for field `$field` must be a(n) $format."
            ];
            if ($format === $regexp) {
                $error["detail"] = "Value for field `$field` must match $format.";
            }
            $this->setError($field, 'validFormat', $error);
            return false;
        }
    }
protected function validateFormat($field, ?string $val, $format, $required = true)
    {
        if ($val !== null) {
            $regexp = $this->getKnownFormat($format);
            if (!$regexp) {
                $regexp = $format;
            }
            $result = preg_match($regexp, $val);
        } else {
            $result = !$required;
        }

        if ($result) {
            $this->clearError($field, 'validFormat');
            return true;
        } else {
            $error = [
                "title" => "Invalid Format for Field `$field`",
                "detail" => "Value for field `$field` must be a(n) $format."
            ];
            if ($format === $regexp) {
                $error["detail"] = "Value for field `$field` must match $format.";
            }
            $this->setError($field, 'validFormat', $error);
            return false;
        }
    }
protected function validateFormat ( $ field , ? string $ val , $ format , $ required = true ) { if ( $ val !== null ) { $ regexp = $ this -> getKnownFormat ( $ format ) ; if ( ! $ regexp ) { $ regexp = $ format ; } $ result = preg_match ( $ regexp , $ val ) ; } else { $ result = ! $ required ; } if ( $ result ) { $ this -> clearError ( $ field , 'validFormat' ) ; return true ; } else { $ error = [ "title" => "Invalid Format for Field `$field`" , "detail" => "Value for field `$field` must be a(n) $format." ] ; if ( $ format === $ regexp ) { $ error [ "detail" ] = "Value for field `$field` must match $format." ; } $ this -> setError ( $ field , 'validFormat' , $ error ) ; return false ; } }
Validates that the value for a given field is of the specified format

@param string $field The name of the field (attribute or relationship) being validated
@param mixed $val The value to validate
@param string $format Either a named format or regexp string
@param bool $required Whether or not the value is required (this affects how `null` is handled)
@return bool Whether or not the validation has passed
Validates that the value for a given field is of the specified format