The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBIx::Class::Valiant::Validator::SetSize - Verify a DBIC related resultset

SYNOPSIS

    package Example::Schema::Result::Person;

    use base 'Example::Schema::Result';

    __PACKAGE__->load_components(qw/
      Valiant::Result
      Core
    /);

    __PACKAGE__->table("person");

    __PACKAGE__->add_columns(
      id => { data_type => 'bigint', is_nullable => 0, is_auto_increment => 1 },
      username => { data_type => 'varchar', is_nullable => 0, size => 48 },
      first_name => { data_type => 'varchar', is_nullable => 0, size => 24 },
      last_name => { data_type => 'varchar', is_nullable => 0, size => 48 },
      password => {
        data_type => 'varchar',
        is_nullable => 0,
        size => 64,
      },
    );

    __PACKAGE__->has_many(
      credit_cards =>
      'Example::Schema::Result::CreditCard',
      { 'foreign.person_id' => 'self.id' }
    );

    __PACKAGE__->validates(
      credit_cards => (
        set_size=>+{ skip_if_empty=>1, min=>2, max=>4 }, 
      )
    );

DESCRIPTION

Validations on related resultsets. This constrains minimum / maximum sizes on the set and permits optional sets (where the min/max is only applied when the set has entries).

ATTRIBUTES

This validator supports the following attributes:

skip_if_empty

Allows you to skip validations if the resultset is empty (has zero rows). Useful if you want to do validations only if there are rows found, such as when you have an optional relationship. Defaults to false.

min

max

The minimum or maximum number of rows that the resultset can contain. Optional (but I suspect you'd set at least one otherwise why bother with this constraint?

too_few_msg

too_many_msg

Error messages associated with the 'min' or 'max' constraints. Defaults to 'too_few' or 'too_many' translation tags.

SHORTCUT FORM

This validator supports the follow shortcut forms:

    validates attribute => ( result_set => [1,4], ... );

Which is the same as:

    validates attribute => (
      result_set => {
        min => 1,
        max => 4,
      }
    );

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