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

Tim Rayner tfrayner at gmail.com
Thu Sep 9 14:06:09 GMT 2010


On 9 September 2010 14:32, Carl Franks <fireartist at gmail.com> wrote:
> On 9 September 2010 12:01, Tim Rayner <tfrayner at gmail.com> wrote:
>>
>> I've just found what I think are a couple of typos in the recent
>> changes to H::F::Constraint::DBIC::Unique. The attached patch
>> illustrates and fixes the problem.
>
> Hi,
> Thanks for this - though do notice that Constraint::DBIC::Unique is
> listed in MANIFEST.SKIP, meaning it won't be included in any cpan
> releases.
> The reason for this is simply that there isn't any tests for it - once
> there are, it can be included.
>
>> Incidentally, would there be any
>> interest in me adding in a couple of our local improvements to this
>> module, and maybe working up a test suite?
>
> I'd recommend describing to us the changes first, so you can get
> feedback on them.
> They won't be included until there's tests for them, so you don't want
> to waste time if they don't get a positive feedback.

Hi,

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.

Cheers,

Tim
-------------- next part --------------
--- ../../src/svn/html-formfu-read-only/HTML-FormFu-Model-DBIC/lib/HTML/For=
mFu/Constraint/DBIC/Unique.pm	2010-09-09 11:53:36.000000000 +0100
+++ lib/HTML/FormFu/Constraint/DBIC/Unique.pm	2010-09-09 14:54:14.000000000=
 +0100
@@ -6,14 +6,14 @@
 =

 use Carp qw( carp croak );
 =

-__PACKAGE__->mk_accessors(qw/ model resultset column self_stash_key /);
+__PACKAGE__->mk_accessors(qw/ model resultset column self_stash_key others=
 /);
 =

 sub constrain_value {
     my ( $self, $value ) =3D @_;
 =

     return 1 if !defined $value || $value eq '';
 =

-    for (qw/ resultset column /) {
+    for (qw/ resultset /) {
         if ( !defined $self->$_ ) {
             # warn and die, as errors are swallowed by HTML-FormFu
             carp  "'$_' is not defined";
@@ -51,12 +51,22 @@
     }
 =

     my $column =3D $self->column || $self->parent->name;
+    my %others;
+    if ( $self->others ) {
+        my @others =3D ref $self->others ? @{ $self->others }
+                       : $self->others;
+
+        my $param =3D $self->form->input;
+        %others =3D map { $_ =3D> $param->{$_} }
+                  grep { defined $param->{$_} && $param->{$_} ne q{} } @ot=
hers;
+
+    }
 =

     my $existing_row =3D eval {
-        $resultset->find( { $column =3D> $value } );
+        $resultset->find( { %others, $column =3D> $value } );
     };
-
-    if ( defined( my $error =3D $@ ) ) {
+    =

+    if ( my $error =3D $@ ) {
         # warn and die, as errors are swallowed by HTML-FormFu
         carp  $error;
         croak $error;
@@ -130,7 +140,7 @@
           - Required
           - type: DBIC::Unique
             model: DBIC::User
-            field: username
+            column: username
 =

 =

 =3Dhead1 DESCRIPTION
@@ -147,12 +157,35 @@
 =

 Arguments: $string # a DBIC resultset name like 'User'
 =

-=3Dhead2 myself
+=3Dhead2 self_stash_key
 =

 reference to a key in the form stash. if this key exists, the constraint
 will check if the id matches the one of this element, so that you can =

 use your own name.
 =

+=3Dhead2 others
+
+Use this key to manage unique compound database keys which consist of
+more than one column. For example, if a database key consists of
+'category' and 'value', use a config file such as this:
+
+    ---
+    elements: =

+      - type:  Text
+        name:  category
+        label: Category
+        constraints:
+          - Required
+    =

+      - type:  Text
+        name:  value
+        label: Value
+        constraints:
+          - Required
+          - type:       DBIC::Unique
+            resultset:  ControlledVocab
+            others:     category
+
 =3Dhead2 SEE ALSO
 =

 Is a sub-class of, and inherits methods from L<HTML::FormFu::Constraint>


More information about the HTML-FormFu mailing list