NAME

Valiant::Validator::OnlyOf - Limit the number of fields not blank in a group

SYNOPSIS

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'
      ]
};

DESCRIPTION

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.

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.

<Uses only_of as the translation tag and you can set that to override the message.

SHORTCUT FORM

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'],
  },
  ... );

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