DBIx::Class::Result::Validation - DBIx::Class component to manage validation on result object
Version 0.14
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');
{ label => [ 'label must be unique', "label must not be `my label'" ], id => [ 'id is ok but not label' ] }
DBIx::Class::Result::Validation component create a new accessor to Result object.
$self->result_errors
This field is used to store all errors
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(@_);
function to configure on object to find what is wrong after a Database throw
_validate function is the function to redefine with validation behaviour object
$self->add_result_error($key, $error_string)
Add a string error attributed to a key (field of object)
call before DBIx::Calss::Base insert
Insert is done only if validate method return true
Call before DBIx::Class::Base update
Update is done only if validate method return true
this function is called to re-init result_errors before call validate function
set of function to validate fields
validation of the enum field, should return a validation error if the field is set and is not in the list of enum
validation of field which must be ascii characters, return error if the field is not ascii
validation of field which must be defined, return error if the field is not defined
validation of a field which can be null but can't be empty
validation of a field which can be null and not equal to 0 this can be used for data_type integer
"DBIx::Class"
Nicolas Oudard <nicolas@oudard.org>
You may distribute this code under the same terms as Perl itself.
To install DBIx::Class::Result::Validation, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Class::Result::Validation
CPAN shell
perl -MCPAN -e shell install DBIx::Class::Result::Validation
For more information on module installation, please visit the detailed CPAN module installation guide.