On 9 September 2010 15:30, Carl Franks <[email protected]> wrote:
> On 9 September 2010 15:06, Tim Rayner <[email protected]> wrote:
>> That's fair enough. I guess the addition I'd really like to see is a
>> method allowing the user to specify more than one column to check
>> against, in cases where the database table has a compound unique key.
>> Charlie's "method_name" argument might well be a better way of doing
>> this, but in the attached example patch this method is called "others"
>> for the sake of argument. Alternatively it could be modified such that
>> the column method accepts an arrayref; I'm open to suggestions and/or
>> opinions. If this is already covered by a patch in the pipeline then
>> that's fine by me; the offer to work up a test suite still stands,
>> though, since I'd like to see this module released to CPAN so I don't
>> have to maintain a local fork.
>
> Hi,
> The use of 'others' is convenient, as the first column-name can be
> omitted, and it'll be automatically calculated from the field name.
> However, it may be easier to maintain you had to list all column-names
> in a single array-ref.
> I'm undecided, so we may as well go with the way you've done it,
> unless anyone else can think of any problems it might cause.
>
> I've committed both of these patches - thanks.
>
> If you're able to come up with even some basic tests, it can be
> included in the cpan release.
Thanks very much. I've attached a patch with a handful of tests;
hopefully they will suffice?
All the best,
Tim
Index: t/constraints/dbic_unique_column.yml
===================================================================
--- t/constraints/dbic_unique_column.yml (revision 0)
+++ t/constraints/dbic_unique_column.yml (revision 0)
@@ -0,0 +1,21 @@
+---
+auto_fieldset: 1
+
+elements:
+ - type: Hidden
+ name: id
+
+ - type: Text
+ name: username
+ constraints:
+ - Required
+ - type: DBIC::Unique
+ resultset: User
+ column: name
+
+ - type: Text
+ name: title
+
+ - type: Submit
+ name: submit
+
Index: t/constraints/dbic_unique.yml
===================================================================
--- t/constraints/dbic_unique.yml (revision 0)
+++ t/constraints/dbic_unique.yml (revision 0)
@@ -0,0 +1,20 @@
+---
+auto_fieldset: 1
+
+elements:
+ - type: Hidden
+ name: id
+
+ - type: Text
+ name: name
+ constraints:
+ - Required
+ - type: DBIC::Unique
+ resultset: User
+
+ - type: Text
+ name: title
+
+ - type: Submit
+ name: submit
+
Index: t/constraints/dbic_unique.t
===================================================================
--- t/constraints/dbic_unique.t (revision 0)
+++ t/constraints/dbic_unique.t (revision 0)
@@ -0,0 +1,94 @@
+use strict;
+use warnings;
+use Test::More tests => 7;
+
+use HTML::FormFu;
+use lib 't/lib';
+use DBICTestLib 'new_schema';
+use MySchema;
+
+my $schema = new_schema();
+
+my $rs = $schema->resultset('User');
+
+# Pre-existing row to check against.
+$rs->create( {
+ name => 'a',
+ title => 'b',
+ } );
+
+# Basic form.
+{
+ my $form = HTML::FormFu->new;
+
+ $form->load_config_file('t/constraints/dbic_unique.yml');
+
+ $form->stash->{'schema'} = $schema;
+
+ $form->process( {
+ 'name' => 'a',
+ 'title' => 'c',
+ } );
+
+ ok( !$form->submitted_and_valid );
+
+ is_deeply(
+ [
+ 'name',
+ ],
+ [ $form->has_errors ],
+ );
+}
+
+# Form where DB column differs from form field name.
+{
+ my $form = HTML::FormFu->new;
+
+ $form->load_config_file('t/constraints/dbic_unique_column.yml');
+
+ $form->stash->{'schema'} = $schema;
+
+ $form->process( {
+ 'username' => 'a',
+ 'title' => 'c',
+ } );
+
+ ok( !$form->submitted_and_valid );
+
+ is_deeply(
+ [
+ 'username',
+ ],
+ [ $form->has_errors ],
+ );
+}
+
+# Form tracking a multi-column unique key (name+title).
+{
+ my $form = HTML::FormFu->new;
+
+ $form->load_config_file('t/constraints/dbic_unique_others.yml');
+
+ $form->stash->{'schema'} = $schema;
+
+ $form->process( {
+ 'name' => 'a',
+ 'title' => 'c',
+ } );
+
+ ok( $form->submitted_and_valid );
+
+ $form->process( {
+ 'name' => 'a',
+ 'title' => 'b',
+ } );
+
+ ok( !$form->submitted_and_valid );
+
+ is_deeply(
+ [
+ 'name',
+ ],
+ [ $form->has_errors ],
+ );
+}
Index: t/constraints/dbic_unique_others.yml
===================================================================
--- t/constraints/dbic_unique_others.yml (revision 0)
+++ t/constraints/dbic_unique_others.yml (revision 0)
@@ -0,0 +1,21 @@
+---
+auto_fieldset: 1
+
+elements:
+ - type: Hidden
+ name: id
+
+ - type: Text
+ name: name
+ constraints:
+ - Required
+ - type: DBIC::Unique
+ resultset: User
+ others: title
+
+ - type: Text
+ name: title
+
+ - type: Submit
+ name: submit
+
_______________________________________________
HTML-FormFu mailing list
[email protected]
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu