(Sent to the RDBO list too because this is mostly an RDBO issue.)

On 1/9/06 7:11 AM, Sean Davis wrote:
> In a simple form, I have a text field with length => 10.  When I call
> validate() with an input to that field longer than 10, I get a fatal error.
> 
> Caught exception "Dog::DB::Dog: Value for gender() is too long.  Maximum
> length is 10 characters.  Value is 17 characters: sdfasdfsdfasdfasd at
> /Library/Perl/5.8.6/Rose/HTML/Form.pm line 527"
> 
> How can I set that behavior to NOT be fatal?

The fatal error is being triggered by the gender() column accessor on your
Dog::DB::Dog object, not by the textfield.

In Rose::HTML::Objects land:

    $field = Rose::HTML::Form::Field::Text->new(name => 'gender',
                                                size => 17);

    # No fatal error thrown here
    $field->input_value('this value is too long');

In Rose::DB::Object land:

    $dog = Dog::DB::Dog->new;

    $dog->gender($field->internal_value); # fatal error: value too long!

The Rose::DB::Object behavior can be changed on a per-column basis by
setting the "overflow" attribute of the column, which exists in the Scalar
column class and is inherited by the various character-based columns:

http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/Metadata/Colum
n/Scalar.pm#overflow

This value needs to be set before the column methods are created in your
Rose::DB::Object-derived class.  That is, before initialize() (or
auto_initialize) is called.  (Well, strictly, before make_column_methods()
is called.)  If you're calling initialize() manually, it's easy:

  Dog::DB::Dog->meta->columns
  (
    ...
    gender => { type => 'varchar', length => 17, overflow => 'truncate' },
  );

  Dog::DB::Dog->meta->initialize;

If you are using auto_initialize(), then you can use the pre_init_hook()
feature of the metadata object:

http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/Metadata.pm#pr
e_init_hook

(Whoops, I'll fix that POD formatting error in my next release.)

    Dog::DB::Dog->meta->pre_init_hook(sub
    {
      foreach my $column (Dog::DB::Dog->meta->columns)
      {
        $column->overflow('truncate')  if($column-> type eq 'varchar');
      }
    });

The Loader supports the same parameter, and simply passes it on to the
metadata object at the appropriate time:

http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/Loader.pm#pre_
init_hook

There is currently no "ignore" value for the overflow attribute, but I will
add one if someone considers it useful.

-John




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to