Form::Factory::Feature::Control::MatchCode - Greps the control value for correctness


Form-Factory documentation Contained in the Form-Factory distribution.

Index


Code Index:

NAME

Top

Form::Factory::Feature::Control::MatchCode - Greps the control value for correctness

VERSION

Top

version 0.020

SYNOPSIS

Top

  has_control even_value => (
      control => 'text',
      features => {
          match_code => {
              message => 'the value in %s is not even',
              code    => sub { shift % 2 == 0 },
          },
      },
  );

DESCRIPTION

Top

Runs the control value against a code reference during the check. If that code reference returns a false value, an error is generated.

ATTRIBUTES

Top

code

This is the code reference. It should expect a single argument to be passed, which is the value to check.

METHODS

Top

check_control

No op.

check

Does the work of running the given subroutine over the control value and reports an error if the code reference runs and returns a false value.

AUTHOR

Top

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

COPYRIGHT AND LICENSE

Top


Form-Factory documentation Contained in the Form-Factory distribution.
package Form::Factory::Feature::Control::MatchCode;
BEGIN {
  $Form::Factory::Feature::Control::MatchCode::VERSION = '0.020';
}
use Moose;

with qw( 
    Form::Factory::Feature 
    Form::Factory::Feature::Role::Check
    Form::Factory::Feature::Role::Control
    Form::Factory::Feature::Role::CustomControlMessage
);

use Carp ();

has code => (
    is        => 'ro',
    isa       => 'CodeRef',
    required  => 1,
);

sub check_control { }

sub check {
    my $self    = shift;
    my $control = $self->control;
    my $value   = $control->current_value;

    unless ($self->code->($value)) {
        $self->control_error('the %s is not correct');
        $self->result->is_valid(0);
    }

    $self->result->is_valid(1) unless $self->result->is_validated;
}

1;