Skip to content

Latest commit

 

History

History
152 lines (96 loc) · 3.91 KB

validate_params.md

File metadata and controls

152 lines (96 loc) · 3.91 KB

Parameter validation

Parameter validation can be used to check if the parameter with the given name is valid. In case the parameter does not exist or is not valid the validation will evaluate to false, otherwise to true.

contains($contained): bool

Returns true if the request parameter contains the value given with $contained.

if ($request->validateParam($paramName)->contains('foo')) {
    // do something, given parameter exists and contains foo
}

containsAnyOf(array $elements): bool

Returns true if the request parameter contains any of the values given with $elements.

if ($request->validateParam($paramName)->containsAnyOf(['foo', 'bar', 'baz'])) {
    // do something, given parameter exists and contains foo, bar or baz
}

isEqualTo($expected): bool

Returns true if the request parameter is equal to the value given with $expected.

if ($request->validateParam($paramName)->isEqualTo('foo')) {
    // do something, given parameter exists and equals foo
}

isHttpUri(): bool

Returns true if the request parameter represents an HTTP URI.

if ($request->validateParam($paramName)->isHttpUri()) {
    // do something, given parameter exists and is a HTTP URI
}

Note: requires stubbles/peer.

isExistingHttpUri(): bool

Returns true if the request parameter represents an HTTP URI and this URI does exist.

if ($request->validateParam($paramName)->isExistingHttpUri()) {
    // do something, given parameter exists and is an existing HTTP URI
}

Note: requires stubbles/peer.

isIpAddress(): bool

Returns true if the request parameter represents a valid IP address, either IPv4 or IPv6.

if ($request->validateParam($paramName)->isIpAddress()) {
    // do something, given parameter exists and is a valid IP address
}

Note: requires stubbles/peer.

isIpV4Address(): bool

Returns true if the request parameter represents a valid IPv4 address.

if ($request->validateParam($paramName)->isIpV4Address()) {
    // do something, given parameter exists and is a valid IPv4 address
}

Note: requires stubbles/peer.

isIpV6Address(): bool

Returns true if the request parameter represents a valid IPv6 address.

if ($request->validateParam($paramName)->isIpV6Address()) {
    // do something, given parameter exists and is a valid IPv6 address
}

Note: requires stubbles/peer.

isMailAddress(): bool

Returns true if the request parameter represents a valid mail address.

if ($request->validateParam($paramName)->isMailAddress()) {
    // do something, given parameter exists and is a valid mail address
}

Note: requires stubbles/peer.

isOneOf(array $allowedValues): bool

Returns true if the request parameter is equal to one of the values given with $allowedValues.

if ($request->validateParam($paramName)->isOneOf(['foo', 'bar', 'baz'])) {
    // do something, given parameter exists and is either of value foo, bar or baz
}

matches(string $regex): bool

Returns true if the request parameter matches the regular expression given with $regex.

if ($request->validateParam($paramName)->matches('/^[0-9]+$/')) {
    // do something, given parameter exists and is matched by given regular expression
}

with(callable $predicate): bool

Available since release 3.0.0

Returns true if the request parameter can be validated with the given closure.

if ($request->validateParam($paramName)->with(function($value) { return 303 == $value; })) {
    // do something, given parameter exists and is approved by given callable
}