[jifty-devel] Validating distinct fields

Edward Funnekotter efunneko at gmail.com
Sun Jun 4 20:53:13 EDT 2006


Hi,

I find for a number of models that I am using in my current project, I am
creating actions for adding records so that I can validate that certain
fields are not duplicated in the database rather than just using the
Create<Name>.pm automatic action.  I think that I could do this my having a
validation function specified in the record definition within my model file,
but is there a reason that we don't just use the 'is distinct' attribute to
get the Action.pm default validator to do the checking always?

I hacked this into my local copy and it seemed to work fine:

Index: lib/Jifty/Action/Record.pm
===================================================================
--- lib/Jifty/Action/Record.pm  (revision 1124)
+++ lib/Jifty/Action/Record.pm  (working copy)
@@ -227,7 +227,7 @@
       }

       # If we're hand-coding a render_as, hints or label, let's use it.
-      for (qw(render_as label hints length mandatory sort_order)) {
+      for (qw(render_as label hints length distinct mandatory sort_order))
{

         if ( defined $column->$_ ) {
           $info->{$_} = $column->$_;
Index: lib/Jifty/Action.pm
===================================================================
--- lib/Jifty/Action.pm (revision 1124)
+++ lib/Jifty/Action.pm (working copy)
@@ -699,6 +699,15 @@
         }
     }

+    if ( $field_info->{distinct} && defined $value) {
+      my $u = $self->record_class->new();
+      $u->load_by_cols( $field => $value);
+      if ($u->id) {
+        return $self->validation_error(
+                  $field => q{There is already an entry with that value} );
+      }
+    }
+
     # If we have a set of allowed values, let's check that out.
     # XXX TODO this should be a validate_valid_values sub
     if ( $value && $field_info->{valid_values} ) {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bestpractical.com/pipermail/jifty-devel/attachments/20060604/c0ef7b47/attachment.htm


More information about the jifty-devel mailing list