Validation::url
Check the format of a URL
Description
public boolean Validation::url( string $name, mixed $value, [ mixed $code = '' ] )
Valid when the value starts with http://, https:// or ftp:// and contains only the characters allowed in a URL.
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $name | string | Field name (key of the error message) | |
| $value | mixed | Value to check | |
| $code | mixed | '' | Error code, array of error code and parameters, or error message (see Configuration) |
Example
Controller
use Fegg\Tool\Validation;
$validation = new Validation();
$validation->url('success', 'https://example.com/', '<strong>URL</strong> の形式が正しくありません'); // -> true
$validation->url('failed', 'javascript:alert(1)', '<strong>URL</strong> の形式が正しくありません'); // -> false
var_dump($validation->getErrorMessage());
// -> array('failed' => '<strong>URL</strong> の形式が正しくありません')
note
The default validation_error_message.php has no @url code. Pass the error message itself, or add the code to your site/config/validation_error_message.php.