[html-formfu] H::F::C::DBIC::Unique self_stash typos

Tim Rayner tfrayner at gmail.com
Fri Sep 10 14:40:14 GMT 2010


On 9 September 2010 15:30, Carl Franks <fireartist at gmail.com> wrote:
> On 9 September 2010 15:06, Tim Rayner <tfrayner at gmail.com> 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
-------------- next part --------------
Index: t/constraints/dbic_unique_column.yml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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 =3D> 7;
+
+use HTML::FormFu;
+use lib 't/lib';
+use DBICTestLib 'new_schema';
+use MySchema;
+
+my $schema =3D new_schema();
+
+my $rs =3D $schema->resultset('User');
+
+# Pre-existing row to check against.
+$rs->create( {
+        name       =3D> 'a',
+        title      =3D> 'b',
+    } );
+
+# Basic form.
+{
+    my $form =3D HTML::FormFu->new;
+        =

+    $form->load_config_file('t/constraints/dbic_unique.yml');
+    =

+    $form->stash->{'schema'} =3D $schema;
+
+    $form->process( {
+            'name'                 =3D> 'a',
+            'title'                =3D> 'c',
+        } );
+    =

+    ok( !$form->submitted_and_valid );
+
+    is_deeply(
+        [
+            'name',
+        ],
+        [ $form->has_errors ],
+    );
+}
+
+# Form where DB column differs from form field name.
+{
+    my $form =3D HTML::FormFu->new;
+        =

+    $form->load_config_file('t/constraints/dbic_unique_column.yml');
+    =

+    $form->stash->{'schema'} =3D $schema;
+
+    $form->process( {
+            'username'             =3D> 'a',
+            'title'                =3D> 'c',
+        } );
+    =

+    ok( !$form->submitted_and_valid );
+
+    is_deeply(
+        [
+            'username',
+        ],
+        [ $form->has_errors ],
+    );
+}
+
+# Form tracking a multi-column unique key (name+title).
+{
+    my $form =3D HTML::FormFu->new;
+        =

+    $form->load_config_file('t/constraints/dbic_unique_others.yml');
+    =

+    $form->stash->{'schema'} =3D $schema;
+
+    $form->process( {
+            'name'                 =3D> 'a',
+            'title'                =3D> 'c',
+        } );
+    =

+    ok( $form->submitted_and_valid );
+
+    $form->process( {
+            'name'                 =3D> 'a',
+            'title'                =3D> 'b',
+        } );
+    =

+    ok( !$form->submitted_and_valid );
+
+    is_deeply(
+        [
+            'name',
+        ],
+        [ $form->has_errors ],
+    );
+}
Index: t/constraints/dbic_unique_others.yml
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- 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
+


More information about the HTML-FormFu mailing list