NAME
Valiant::Validator::Format - Validate a value based on a regular expression
SYNOPSIS
package Local::Test::Format;
use Moo;
use Valiant::Validations;
has phone => (is=>'ro');
has name => (is=>'ro');
has email => (is=>'ro');
validates phone => (
format => +{
match => qr/\d\d\d-\d\d\d-\d\d\d\d/,
},
);
validates name => (
format => +{
without => qr/\d+/,
},
);
validates email =>
format => 'email';
my $object = Local::Test::Format->new(
phone => '387-1212',
name => 'jjn1056',
);
$object->validate;
warn $object->errors->_dump;
$VAR1 = {
'phone' => [
'Phone does not match the required pattern'
],
'name' => [
'Name contains invalid characters'
],
'email' => [
'Email is not an email address'
],
};
DESCRIPTION
Validates that the attribute value either matches a given regular expression (match) or that it fails to match an exclusion expression (
without
).
Values that fail the match
condition (which can be a regular expression or a reference to a method that provides one) will add an error matching the tag invalid_format_match
, which can be overridden as with other tags.
Values that match the without
conditions (also either a regular expression or a coderef that provides one) with all an error matching the tag invalid_format_without
which can also be overridden via a passed parameter.
A number of format shortcuts are built in to help you bootstrap faster. Use the text string in place of the regexp (for example:)
validates email => (
format => +{
match => 'email',
}
);
You can add to the prebuilts by adding keys to the global %prebuilt_formats
hash. See code for more.
-
Uses a regexp to validate that a string looks like an email. This is naive but probably acceptable for most uses. For real detailed eail checking you'll need to build something on Email::Valid. Uses translation tag
not_email
. - alpha
-
Text can only be upper or lowercase letter. Uses translation tag
not_alpha
. - alpha_numeric
-
Text can only be upper or lowercase letters, numbers or '_'. Uses translation tag
not_alpha_numeric
. - words
-
Only letters and spaces. Error message default is translation tag
not_words
. - word
-
Must contain only a single word. Adds error
not_word
if fails. - zip
- zip5
- zip9
-
Match US zipcodes. This is a pattern match only, we don't actually check if the zip is a true valid zipcode, just that it looks like one.
zip5
matches a common 5 digit zipcode and returnnot_zip5
translation tag if not.zip9
looks for the 'Zip +4' extended zipcode, such as 11111-2222 (the separator can be either '-' or a space). Adds errorsnot_zip9
if not. Finallyzip
will match either zip or zip +4 and add errornot_zip
on failure to match. - ascii
-
Must contain only ASCII characters. Adds error
not_ascii
if fails.
SHORTCUT FORM
This validator supports the follow shortcut forms:
validates attribute => ( format => qr/\d\d\d/, ... );
Which is the same as:
validates attribute => (
format => +{
match => qr/\d\d\d/,
},
);
We choose to shortcut the 'match' pattern based on experiene that suggested it is more common to required a specific pattern than to have an exclusion pattern.
This also works for the coderef form.
validates attribute => ( format => \&pattern, ... );
Which is the same as:
validates attribute => (
format => +{
match => \&pattern,
},
);
And of course its very handy for using one of the built in pattern tags:
validates email => (format => 'email');
Which is the same as:
validates email => (
format => +{
match => 'email',
}
);
GLOBAL PARAMETERS
This validator supports all the standard shared parameters: if
, unless
, message
, strict
, allow_undef
, allow_blank
.
SEE ALSO
Valiant, Valiant::Validator, Valiant::Validator::Each.
AUTHOR
See Valiant
COPYRIGHT & LICENSE
See Valiant
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 243:
Unterminated C<...> sequence
- Around line 310:
You forgot a '=back' before '=head1'