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

Syccess::Validator - Syccess validator

VERSION

version 0.104

SYNOPSIS

  package MyValidators::Custom;

  use Moo;

  with qw(
    Syccess::Validator
  );

  sub validate {
    my ( $self, %params ) = @_;
    my $name = $self->syccess_field->name;
    # No error if there is no value
    return if !exists($params{$name});
    my $value = $params{$name};
    return if $value eq 'ok';
    return 'Your value for %s is not ok.';
  }

  1;

DESCRIPTION

A custom validator requires a validate function, which will be given the complete list of parameters that was given to the validate function on the Syccess object. If there is no error, then the function must return also nothing, as in, an empty list. Anything else given back will be converted into an error message. Most simple is giving a string, that may contain a %s, which will be filled with the label of the field.

Normally you don't need this role, most validation requirements will be fulfilled with the Syccess::ValidatorSimple. The case for this role is only given, if you need also access to values of other fields to decide the success.

By default, the argument for the validator will be stored in "arg", except if its a HashRef, in this case, it will be dereferenced and be used as arguments for the creation of the validator. Which means:

  Syccess->new( fields => [ myfield => [ length => 4 ] ] );

is the same as doing:

  Syccess->new( fields => [ myfield => [ length => { arg => 4 } ] ] );

This way allows to mix complex and straight forward usages. The core validator Syccess::Validator::Length is a very good example for this.

ATTRIBUTES

syccess_field

This attribute will be set automatically by Syccess, when it instantiate an object of the validator. There the validator can get the name to find its value in the parameters given on validate.

SUPPORT

IRC

  Join irc.perl.org and msg Getty

Repository

  http://github.com/Getty/p5-syccess
  Pull request and additional contributors are welcome

Issue Tracker

  http://github.com/Getty/p5-syccess/issues

AUTHOR

Torsten Raudssus <torsten@raudss.us>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Torsten Raudssus.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.