NAME

DBIx::Class::Result::Validation - DBIx::Class component to manage validation on result object

VERSION

Version 0.13

SYNOPSIS

DBIx::Class::Result::Validation component call validate function before insert or update object and unauthorized these actions if validation set result_errors accessor.

In your result class load_component :

Package::Schema::Result::MyClass;

use strict;
use warning;

__PACKAGE__->load_component(qw/ ... Result::Validation /);

defined your _validate function which will be called by validate function

sub _validate
{
  my $self = shift;
  #validate if this object exist whith the same label
  my @other = $self->result_source->resultset->search({ label => $self->label
                                                        id => {"!=", $self->id}});
  if (scalar @other)
  {
    $self->add_result_error('label', 'label must be unique');
  }

}

When you try to create or update an object Package::Schema::Result::MyClass, if an other one with the same label exist, this one will be not created, validate return 0 and $self->result_errors will be set.

$self->result_errors return :

{ label => ['label must be unique'] }

Otherwise, object is create, validate return 1 and $self->result_errors is undef.

It is possible to set more than one key error and more than one error by key

$self->add_result_error('label', 'label must be unique');
$self->add_result_error('label', "label must not be `$self->label'");
$self->add_result_error('id', 'id is ok but not label');

$self->result_errors return :

{
  label => [
          'label must be unique',
          "label must not be `my label'"
          ],
  id => [
       'id is ok but not label'
        ]
}

Reserved Accessor

DBIx::Class::Result::Validation component create a new accessor to Result object.

$self->result_errors

This field is used to store all errors

SUBROUTINES/METHODS

validate

This validate function is called before insert or update action. If result_errors is not defined it return true

You can redefined it in your Result object and call back it with :

return $self->next::method(@_);

error_reporting

function to configure on object to find what is wrong after a Database throw

_validate

_validate function is the function to redefine with validation behaviour object

add_result_error

$self->add_result_error($key, $error_string)

Add a string error attributed to a key (field of object)

insert

call before DBIx::Calss::Base insert

Insert is done only if validate method return true

update

Call before DBIx::Class::Base update

Update is done only if validate method return true

_erase_result_error

this function is called to re-init result_errors before call validate function

SEE ALSO

"DBIx::Class"

AUTHOR

Nicolas Oudard <nicolas@oudard.org>

CONTRIBUTORS

LICENSE

You may distribute this code under the same terms as Perl itself.