[Jifty-commit] r6016 - in jifty/trunk: lib/Jifty share/web/templates/__jifty

Jifty commits jifty-commit at lists.jifty.org
Tue Nov 18 13:05:09 EST 2008


Author: alexmv
Date: Tue Nov 18 13:04:43 2008
New Revision: 6016

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Action.pm
   jifty/trunk/lib/Jifty/Result.pm
   jifty/trunk/share/web/templates/__jifty/validator.xml

Log:
 r39237 at kohr-ah:  chmrr | 2008-11-18 13:00:46 -0500
  * Add an option for force ajax vaidation failures on empty form fields
  * Allow earlier fields to set validation failures on later fields


Modified: jifty/trunk/lib/Jifty/Action.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Action.pm	(original)
+++ jifty/trunk/lib/Jifty/Action.pm	Tue Nov 18 13:04:43 2008
@@ -972,6 +972,11 @@
         return $self->$default_validator( $value, $self->argument_values );
     }
 
+    # Check if we already have a failure for it, from some other field
+    elsif ( $self->result->field_error($field) or $self->result->field_warning($field) ) {
+        return 0;
+    }
+
     # If none of the checks have failed so far, then it's ok
     else {
         return $self->validation_ok($field);
@@ -1147,63 +1152,73 @@
     return $vv;
 }
 
-=head2 validation_error ARGUMENT => ERROR TEXT
+=head2 validation_error ARGUMENT => ERROR TEXT, [OPTIONS]
 
 Used to report an error during validation.  Inside a validator you
 should write:
 
   return $self->validation_error( $field => "error");
 
-..where C<$field> is the name of the argument which is at fault.
+..where C<$field> is the name of the argument which is at fault.  Any
+extra C<OPTIONS> are passed through to L<Jifty::Result/field_error>.
 
 =cut
 
 sub validation_error {
     my $self = shift;
-    my $field = shift;
-    my $error = shift;
-  
-    $self->result->field_error($field => $error); 
-  
+    my ($field, $error, %args) = @_;
+    $self->log->warn("No such field '$field' -- did you forget to specify a field?")
+        unless $self->arguments->{$field};
+
+    $self->result->field_error($field => $error, %args);
+
     return 0;
 }
 
-=head2 validation_warning ARGUMENT => WARNING TEXT
+=head2 validation_warning ARGUMENT => WARNING TEXT, [OPTIONS]
 
 Used to report a warning during validation.  Inside a validator you
 should write:
 
   return $self->validation_warning( $field => _("warning"));
 
-..where C<$field> is the name of the argument which is at fault.
+..where C<$field> is the name of the argument which is at fault.  Any
+extra C<OPTIONS> are passed through to L<Jifty::Result/field_warning>.
 
 =cut
 
 sub validation_warning {
     my $self = shift;
-    my $field = shift;
-    my $warning = shift;
-  
-    $self->result->field_warning($field => $warning); 
-  
+    my ($field, $warning, %args) = @_;
+    $self->log->warn("No such field '$field' -- did you forget to specify a field?")
+        unless $self->arguments->{$field};
+
+    $self->result->field_warning($field => $warning, %args); 
+
     return 0;
 }
 
-=head2 validation_ok ARGUMENT
+=head2 validation_ok ARGUMENT, [OPTIONS]
 
 Used to report that a field B<does> validate.  Inside a validator you
 should write:
 
   return $self->validation_ok($field);
 
+Any extra C<OPTIONS> are passed through to
+L<Jifty::Result/field_warning> and L<Jifty::Result/field_error> when
+unsetting them.
+
 =cut
 
 sub validation_ok {
     my $self = shift;
-    my $field = shift;
+    my ($field, %args) = @_;
+    $self->log->warn("No such field '$field' -- did you forget to specify a field?")
+        unless $self->arguments->{$field};
 
-    $self->result->field_error($field => undef);
-    $self->result->field_warning($field => undef);
+    $self->result->field_error($field => undef, %args);
+    $self->result->field_warning($field => undef, %args);
 
     return 1;
 }

Modified: jifty/trunk/lib/Jifty/Result.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Result.pm	(original)
+++ jifty/trunk/lib/Jifty/Result.pm	Tue Nov 18 13:04:43 2008
@@ -82,10 +82,13 @@
     return $self->{error};
 }
 
-=head2 field_error FIELD [ERROR]
+=head2 field_error FIELD [ERROR] [OPTIONS]
 
 Gets or sets the error string for a specific field on the action.
-This also automatically sets the result to be a failure.
+This also automatically sets the result to be a failure.  C<OPTIONS>
+is an optional set of key-value pairs; the only currently supported
+option is C<force>, which sets the L</ajax_force_validate> for this
+field.
 
 =cut
 
@@ -95,6 +98,10 @@
 
     $self->failure(1) if @_ and $_[0];
     $self->{field_errors}{ $field } = shift if @_;
+
+    my %args = @_;
+    $self->{ajax_force_validate}{ $field } = $args{force} if exists $args{force};
+
     return $self->{field_errors}{ $field };
 }
 
@@ -110,9 +117,12 @@
     return %{$self->{field_errors} || {}};
 }
 
-=head2 field_warning FIELD [WARNING]
+=head2 field_warning FIELD [WARNING] [OPTIONS]
 
-Gets or sets the warning string for a specific field on the action.
+Gets or sets the warning string for a specific field on the
+action. C<OPTIONS> is an optional set of key-value pairs; the only
+currently supported option is C<force>, which sets the
+L</ajax_force_validate> for this field.
 
 =cut
 
@@ -121,6 +131,10 @@
     my $field = shift;
 
     $self->{field_warnings}{ $field } = shift if @_;
+
+    my %args = @_;
+    $self->{ajax_force_validate}{ $field } = $args{force} if exists $args{force};
+
     return $self->{field_warnings}{ $field };
 }
 
@@ -136,6 +150,24 @@
     return %{$self->{field_warnings} || {}};
 }
 
+=head2 ajax_force_validate FIELD [VALUE]
+
+Gets or sets the flag which determines if warnings and errors are set
+using ajax validation, even if the field is empty.  By default,
+validation warnings and errors are I<not> shown for empty fields, as
+yelling to users about mandatory fields they've not gotten to yet is
+poor form.  You can use this method to force ajax errors to show even
+on empty fields.
+
+=cut
+
+sub ajax_force_validate {
+    my $self = shift;
+    my $field = shift;
+    $self->{ajax_force_validate}{ $field } = shift if @_;
+    return $self->{ajax_force_validate}{$field};
+}
+
 =head2 field_canonicalization_note FIELD [NOTE]
 
 Gets or sets a canonicalization note for a specific field on the action.

Modified: jifty/trunk/share/web/templates/__jifty/validator.xml
==============================================================================
--- jifty/trunk/share/web/templates/__jifty/validator.xml	(original)
+++ jifty/trunk/share/web/templates/__jifty/validator.xml	Tue Nov 18 13:04:43 2008
@@ -18,8 +18,10 @@
         if ( not $action->arguments->{$arg}->{ajax_validates} ) {
             $writer->emptyTag( "ignored", id => $action->error_div_id($arg) );
             $writer->emptyTag( "ignored", id => $action->warning_div_id($arg) );
-        } elsif ( not defined $action->argument_value($arg)
-                  or length $action->argument_value($arg) == 0 ) {
+        } elsif ( (not defined $action->argument_value($arg)
+                  or length $action->argument_value($arg) == 0)
+                      and not $action->result->ajax_force_validate($arg)
+              ) {
             $writer->emptyTag( "blank", id => $action->error_div_id($arg) );
             $writer->emptyTag( "blank", id => $action->warning_div_id($arg) );
         } elsif ( $action->result->field_error($arg) ) {


More information about the Jifty-commit mailing list