<?xml version="1.0" encoding="windows-1252"?>
<node id="996341" title="[more detail added] Trying to get DBIx::Class::Result::Validation to work" created="2012-09-29 02:26:10" updated="2012-09-29 02:26:10">
<type id="115">
perlquestion</type>
<author id="61104">
cLive ;-)</author>
<data>
<field name="doctext">
&lt;p&gt;Last night I was wiped, but here's an expanded version...&lt;/p&gt;

&lt;p&gt;I wrote a standlone, minimal example, after reading &lt;a href="http://dbix-class.35028.n2.nabble.com/DBIx-Class-Validation-croak-s-With-The-Validation-Result-td6006401.html"&gt;this discussion&lt;/a&gt; (about the only one I could find on the module).&lt;/p&gt;

&lt;code&gt;
#!/usr/bin/perl
use strict;
use Overhaul::DB;

my $dbx = Overhaul::DB-&gt;new('track');
my $schema = $dbx-&gt;schema;

# this works as expected
my $valid_result = $schema-&gt;resultset('track::Result')-&gt;create({
                     first_name =&gt; 'Clive',
                     last_name  =&gt; 'Testing',
                   });
$valid_result-&gt;delete;

my $bad_result = try { $schema-&gt;resultset('track::Result')-&gt;create({
                         last_name  =&gt; 'Testing',
                       });
                 } catch { $_ };

print "**************** We made it to here\n";
&lt;/code&gt;
&lt;p&gt;When I run it though, it dumps the error:&lt;/p&gt;
&lt;code&gt;
Overhaul::DB::Schema::Result::track::Result: Validation failed.
{
  'first_name' =&gt; [
    'First name is required'
  ]
}
&lt;/code&gt;
&lt;p&gt;(yay, I wanted that). But then it dumps a stack trace and quits. Am I meant to overload the DBIx::Class exception handler?&lt;/p&gt;
&lt;code&gt;
 at /usr/local/share/perl5/DBIx/Class/Result/Validation.pm line 214
 at /usr/local/share/perl5/DBIx/Class/Result/Validation.pm line 208
        DBIx::Class::Result::Validation::__ANON__('Overhaul::DB::Schema::Result::track::Result: Validation faile...') called at /usr/local/share/perl5/Try/Tiny.pm line 100
        Try::Tiny::try('CODE(0x1e3ab10)', 'Try::Tiny::Catch=REF(0x1933640)') called at /usr/local/share/perl5/DBIx/Class/Result/Validation.pm line 214
        DBIx::Class::Result::Validation::_try_next_method('Overhaul::DB::Schema::Result::track::Result=HASH(0x1e3ad98)', 'CODE(0xc591f0)', undef) called at /usr/local/share/perl5/DBIx/Class/Result/Validation.pm line 169
        DBIx::Class::Result::Validation::insert('Overhaul::DB::Schema::Result::track::Result=HASH(0x1e3ad98)', undef) called at Overhaul/DB/Schema/Result/track/Result.pm line 62
        Overhaul::DB::Schema::Result::track::Result::insert('Overhaul::DB::Schema::Result::track::Result=HASH(0x1e3ad98)') called at /usr/share/perl5/DBIx/Class/ResultSet.pm line 2196
        DBIx::Class::ResultSet::create('DBIx::Class::ResultSet=HASH(0x1e3ad80)', 'HASH(0x1c4dcb8)') called at tmp.pl line 18
&lt;/code&gt;

&lt;p&gt;When I switch it out to an eval, it works:&lt;/p&gt;
&lt;code&gt;
my $bad_result;
eval {
  $bad_result = $schema-&gt;resultset('track::Result')-&gt;create({
                  last_name  =&gt; 'Testing', 
                });        
};
&lt;/code&gt;
&lt;p&gt;$@ contains:&lt;/p&gt;
&lt;code&gt; Create failed: Overhaul::DB::Schema::Result::track::Result: Validation failed.
{
  'first_name' =&gt; [
    'First name is required'
  ]
}
 at /usr/local/share/perl5/DBIx/Class/Result/Validation.pm line 214
&lt;/code&gt;
&lt;p&gt;However, I still can't work out how to get to the error data (I'm guessing the module author is French, with limited English, so the docs are not that clear).&lt;/p&gt;

&lt;p&gt;I think I'm missing something on the paradigm shift from plain DBI to DBIx::Class. In DBI, say the error was at the data level (wrong type, say), it would throw you like this (from the DB rather than internal validation):&lt;/p&gt;

&lt;code&gt;$sth-&gt;execute || die "Query failed"&lt;/code&gt;

&lt;p&gt;So, to sum up, I think it's two questions. What am I missing here? Is this the best way to run DBIx::Class data validation, or should I be using a different approach?&lt;/p&gt;


</field>
</data>
</node>
