Valiant::Validator::OnlyOf - Limit the number of fields not blank in a group
package Local::Test::OnlyOf; use Moo; use Valiant::Validations; has opt1 => (is=>'ro'); has opt2 => (is=>'ro'); has opt3 => (is=>'ro'); validates opt1 => ( only_of => { members => ['opt2','opt3'], max_allowed => 1, # This is the default value ); my $object = Local::Test::OnlyOf->new(); $object->validate; warn $object->errors->_dump; $VAR1 = { 'opt1' => [ 'Opt1 please choose only 1 field' ] };
Limits the number of fields in a group that can be not blank. Useful when you have a group of optional fields that you want the user to fill in only one (or a subset of). Default is to allow only one member of the defined group to be not blank, and you can override that with the max_allowed paramters.
max_allowed
Both members and max_allowed can be a subref that gets the first argument as the object and is expected to return something valid. Useful if for example you have different rules for the group members or size based on the data.
members
<Uses only_of as the translation tag and you can set that to override the message.
only_of
This validator supports the follow shortcut forms:
validates attribute => ( only_of => ['opt2','opt3'], ... );
Which is the same as:
validates attribute => ( only_of => { members => ['opt2','opt3'], }, ... );
This validator supports all the standard shared parameters: if, unless, message, strict, allow_undef, allow_blank.
if
unless
message
strict
allow_undef
allow_blank
Valiant, Valiant::Validator, Valiant::Validator::Each.
See Valiant
To install Valiant, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Valiant
CPAN shell
perl -MCPAN -e shell install Valiant
For more information on module installation, please visit the detailed CPAN module installation guide.