Final Class Yiisoft\Validator\Rule\Url
Defines validation options for a value that is a valid HTTP or HTTPS URL.
Note that the handler only checks if the URL scheme and host parts are correct. It does not check the remaining parts of a URL.
See also Yiisoft\Validator\Rule\UrlHandler.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Validator\Rule\Url | |
| getHandler() | Yiisoft\Validator\Rule\Url | |
| getIncorrectInputMessage() | Get a message used when the input is incorrect. | Yiisoft\Validator\Rule\Url |
| getMessage() | Get a message used when the value is not valid. | Yiisoft\Validator\Rule\Url |
| getName() | Yiisoft\Validator\Rule\Url | |
| getOptions() | Yiisoft\Validator\Rule\Url | |
| getPattern() | Get ready to use regular expression pattern applied for URL validation. | Yiisoft\Validator\Rule\Url |
| getSkipOnEmpty() | A getter for $skipOnEmpty property. |
Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait |
| getValidSchemes() | Get valid URI schemas. | Yiisoft\Validator\Rule\Url |
| getWhen() | A getter for $when property. |
Yiisoft\Validator\Rule\Trait\WhenTrait |
| isIdnEnabled() | Whether the validation process must take
{@link https://en.wikipedia.org/wiki/Internationalized_domain_name IDN (internationalized domain names)}
into account. false means that validation of URLs containing IDN will always
fail. Note that in order to use IDN validation you have to install and enable intl PHP
extension, otherwise an exception will be thrown. |
Yiisoft\Validator\Rule\Url |
| shouldSkipOnError() | A getter for $skipOnError property. |
Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait |
| skipOnEmpty() | An immutable setter to change $skipOnEmpty property. |
Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait |
| skipOnError() | An immutable setter to change $skipOnError property. |
Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait |
| when() | An immutable setter to change $when property. |
Yiisoft\Validator\Rule\Trait\WhenTrait |
Method Details
| public mixed __construct ( string $pattern = '/^{schemes}:\\/\\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\\d{1,5})?([?\\/#].*$|$)/', string[] $validSchemes = ['http', 'https'], boolean $enableIdn = false, string $incorrectInputMessage = '{Property} must be a string. {type} given.', string $message = '{Property} is not a valid URL.', boolean|callable|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ) | ||
| $pattern | string |
The regular expression used to validate the value.
The pattern may contain a Note that if you want to reuse the pattern in HTML5 input, it should have |
| $validSchemes | string[] |
List of URI schemes which should be considered valid. By default, http and https are considered to be valid schemes. |
| $enableIdn | boolean |
Whether the validation process must take
{@link https://en.wikipedia.org/wiki/Internationalized_domain_name IDN (internationalized domain names)}
into account . Defaults to |
| $incorrectInputMessage | string |
A message used when the input is incorrect. You may use the following placeholders in the message:
|
| $message | string |
@var string A message used when the value is not valid. You may use the following placeholders in the message:
|
| $skipOnEmpty | boolean|callable|null |
Whether to skip this rule if the validated value is empty. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}. |
| $skipOnError | boolean |
Whether to skip this rule if any of the previous rules gave an error. See {@see \Yiisoft\Validator\SkipOnErrorInterface}. |
| $when | Closure|null |
A callable to define a condition for applying the rule. See {@see \Yiisoft\Validator\WhenInterface}. |
| throws | RuntimeException |
If intl extension is not enabled and {@see $enableIdn} is true. |
|---|---|---|
public function __construct(
string $pattern = '/^{schemes}:\/\/(([a-zA-Z0-9][a-zA-Z0-9_-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_-]*)+)(?::\d{1,5})?([?\/#].*$|$)/',
private array $validSchemes = ['http', 'https'],
private bool $enableIdn = false,
private string $incorrectInputMessage = '{Property} must be a string. {type} given.',
private string $message = '{Property} is not a valid URL.',
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
$pattern = $this->preparePattern($pattern);
if ($pattern === '') {
throw new InvalidArgumentException('Pattern can\'t be empty.');
}
/**
* @psalm-var non-empty-string $pattern
*/
$this->pattern = $pattern;
if ($enableIdn && !function_exists('idn_to_ascii')) {
// Tested via separate CI configuration (see ".github/workflows/build.yml").
// @codeCoverageIgnoreStart
throw new RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
// @codeCoverageIgnoreEnd
}
$this->skipOnEmpty = $skipOnEmpty;
}
Get a message used when the input is incorrect.
| public string getIncorrectInputMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
Get a message used when the value is not valid.
| public string getMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getMessage(): string
{
return $this->message;
}
| public array getOptions ( ) |
public function getOptions(): array
{
return [
'pattern' => $this->getPattern(),
'validSchemes' => $this->validSchemes,
'enableIdn' => $this->enableIdn,
'incorrectInputMessage' => [
'template' => $this->incorrectInputMessage,
'parameters' => [],
],
'message' => [
'template' => $this->message,
'parameters' => [],
],
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
];
}
Get ready to use regular expression pattern applied for URL validation.
| public string getPattern ( ) | ||
| return | string |
Regular expression pattern applied for URL validation. |
|---|---|---|
public function getPattern(): string
{
return $this->pattern;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::getSkipOnEmpty()
A getter for $skipOnEmpty property.
| public boolean|callable|null getSkipOnEmpty ( ) | ||
| return | boolean|callable|null |
A current raw (non-normalized) value. |
|---|---|---|
public function getSkipOnEmpty(): bool|callable|null
{
return $this->skipOnEmpty;
}
Get valid URI schemas.
| public string[] getValidSchemes ( ) | ||
| return | string[] |
List of URI schemes which should be considered valid. By default, http and https are considered to be valid schemes. |
|---|---|---|
public function getValidSchemes(): array
{
return $this->validSchemes;
}
Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()
A getter for $when property.
| public Closure|null getWhen ( ) | ||
| return | Closure|null |
Current value:
|
|---|---|---|
public function getWhen(): ?Closure
{
return $this->when;
}
Whether the validation process must take
{@link https://en.wikipedia.org/wiki/Internationalized_domain_name IDN (internationalized domain names)}
into account. false means that validation of URLs containing IDN will always
fail. Note that in order to use IDN validation you have to install and enable intl PHP
extension, otherwise an exception will be thrown.
| public boolean isIdnEnabled ( ) | ||
| return | boolean |
Whether to enable IDN validation. |
|---|---|---|
public function isIdnEnabled(): bool
{
return $this->enableIdn;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()
A getter for $skipOnError property.
| public boolean shouldSkipOnError ( ) | ||
| return | boolean |
Current value. |
|---|---|---|
public function shouldSkipOnError(): bool
{
return $this->skipOnError;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::skipOnEmpty()
An immutable setter to change $skipOnEmpty property.
| public $this skipOnEmpty ( boolean|callable|null $value ) | ||
| $value | boolean|callable|null |
A new value. |
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function skipOnEmpty(bool|callable|null $value): static
{
$new = clone $this;
$new->skipOnEmpty = $value;
return $new;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::skipOnError()
An immutable setter to change $skipOnError property.
| public $this skipOnError ( boolean $value ) | ||
| $value | boolean |
A new value. |
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function skipOnError(bool $value): static
{
$new = clone $this;
$new->skipOnError = $value;
return $new;
}
Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::when()
An immutable setter to change $when property.
| public $this when ( Closure|null $value ) | ||
| $value | Closure|null |
A new value:
|
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function when(?Closure $value): static
{
$new = clone $this;
$new->when = $value;
return $new;
}
Signup or Login in order to comment.