[Jifty-commit] r1032 - in jifty/trunk: . share/web/static/js share/web/templates/__jifty t/TestApp/lib/TestApp/Action t/TestApp/t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed May 10 17:25:13 EDT 2006


Author: seanmil
Date: Wed May 10 17:25:07 2006
New Revision: 1032

Added:
   jifty/trunk/t/TestApp/t/06-validation.t
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Action.pm
   jifty/trunk/share/web/static/js/jifty.js
   jifty/trunk/share/web/templates/__jifty/validator.xml
   jifty/trunk/t/TestApp/lib/TestApp/Action/DoSomething.pm

Log:
 r14 at pc102:  sean | 2006-05-10 17:20:33 -0400
 * Added AJAXified canonicalizations
 


Modified: jifty/trunk/lib/Jifty/Action.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Action.pm	(original)
+++ jifty/trunk/lib/Jifty/Action.pm	Wed May 10 17:25:07 2006
@@ -129,6 +129,20 @@
 See L<Jifty::Web::Form::Field> for the list of possible keys that each
 argument can have.
 
+In addition to the list there, you may use this additional key:
+
+=over
+
+=item ajax_canonicalizes
+
+This key takes a boolean value that determines if the value displayed in
+the form field is updated via AJAX with the result returned by this argument's
+L<canonicalize|Jifty::Manual::Glossary/canonicalize> function.
+
+Defaults to false.
+
+=back
+
 =cut
 
 sub arguments {

Modified: jifty/trunk/share/web/static/js/jifty.js
==============================================================================
--- jifty/trunk/share/web/static/js/jifty.js	(original)
+++ jifty/trunk/share/web/static/js/jifty.js	Wed May 10 17:25:07 2006
@@ -105,22 +105,34 @@
                     function (request) {
                         var response  = request.responseXML.documentElement;
                         for (var action = response.firstChild; action != null; action = action.nextSibling) {
-                            if ((action.nodeName != 'action') || (action.getAttribute("id") != id))
-                                continue;
-                            for (var field = action.firstChild; field != null; field = field.nextSibling) {
-                                // Possibilities for field.nodeName: it could be #text (whitespace),
-                                // or 'blank' (the field was blank, don't mess with the error div), or 'ok'
-                                // (clear the error and warning div!) or 'error' (fill in the error div, clear 
-				// the warning div!) or 'warning' (fill in the warning div and clear the error div!)
-                                if (field.nodeName == 'error' || field.nodeName == 'warning') {
-                                    var err_div = document.getElementById(field.getAttribute("id"));
-                                    if (err_div != null) {
-                                        err_div.innerHTML = field.firstChild.data;
+                            if ((action.nodeName == 'validationaction') && (action.getAttribute("id") == id)) {
+                                for (var field = action.firstChild; field != null; field = field.nextSibling) {
+                                    // Possibilities for field.nodeName: it could be #text (whitespace),
+                                    // or 'blank' (the field was blank, don't mess with the error div), or 'ok'
+                                    // (clear the error and warning div!) or 'error' (fill in the error div, clear 
+                                    // the warning div!) or 'warning' (fill in the warning div and clear the error div!)
+                                    if (field.nodeName == 'error' || field.nodeName == 'warning') {
+                                        var err_div = document.getElementById(field.getAttribute("id"));
+                                        if (err_div != null) {
+                                            err_div.innerHTML = field.firstChild.data;
+                                        }
+                                    } else if (field.nodeName == 'ok') {
+                                        var err_div = document.getElementById(field.getAttribute("id"));
+                                        if (err_div != null) {
+                                            err_div.innerHTML = '';
+                                        }
                                     }
-                                } else if (field.nodeName == 'ok') {
-                                    var err_div = document.getElementById(field.getAttribute("id"));
-                                    if (err_div != null) {
-                                        err_div.innerHTML = '';
+                                }
+                            } else if ((action.nodeName == 'canonicalizeaction') && (action.getAttribute("id") == id)) {
+                                for (var field = action.firstChild; field != null; field = field.nextSibling) {
+                                    // Possibilities for field.nodeName: it could be 'ignored', 'blank' or 'update'
+                                    if (field.nodeName == 'update') {
+                                        var field_name = field.getAttribute("name");
+                                        for (var form_number in document.forms) {
+                                            if (document.forms[form_number].elements[field_name] == null)
+                                                continue;
+                                            document.forms[form_number].elements[field_name].value = field.firstChild.data;
+                                        }
                                     }
                                 }
                             }

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	Wed May 10 17:25:07 2006
@@ -8,7 +8,7 @@
 $writer->startTag("validation");
 for my $ra ( Jifty->web->request->actions ) {
     my $action = Jifty->web->new_action_from_request($ra);
-    $writer->startTag( "action", id => $action->register_name );
+    $writer->startTag( "validationaction", id => $action->register_name );
     for my $arg ( $action->argument_names ) {
         if ( not $action->arguments->{$arg}->{ajax_validates} ) {
             $writer->emptyTag( "ignored", id => $action->error_div_id($arg) );
@@ -37,6 +37,22 @@
         }
     }
     $writer->endTag();
+    $writer->startTag( "canonicalizeaction", id => $action->register_name );
+    for my $arg ( $action->argument_names ) {
+        if ( not $action->arguments->{$arg}->{ajax_canonicalizes} ) {
+            $writer->emptyTag( "ignored", name => $action->form_field_name($arg) );
+        } elsif ( not defined $action->argument_value($arg)
+            or length $action->argument_value($arg) == 0 ) {
+            $writer->emptyTag( "blank", name => $action->form_field_name($arg) );
+        } else {
+            $writer->dataElement(
+                "update",
+                $action->argument_value($arg),
+                name => $action->form_field_name($arg)
+            );
+        }
+    }
+    $writer->endTag();
 }
 $writer->endTag();
 $m->out($output);

Modified: jifty/trunk/t/TestApp/lib/TestApp/Action/DoSomething.pm
==============================================================================
--- jifty/trunk/t/TestApp/lib/TestApp/Action/DoSomething.pm	(original)
+++ jifty/trunk/t/TestApp/lib/TestApp/Action/DoSomething.pm	Wed May 10 17:25:07 2006
@@ -2,6 +2,35 @@
 
 use base qw/Jifty::Action/;
 
+sub arguments {
+    return({
+        email => {
+            label => 'Email',
+            ajax_canonicalizes => 1,
+            ajax_validates => 1,
+        }
+    });
+}
+
+sub canonicalize_email {
+    my $self = shift;
+    my $address = shift;
+    
+    return lc($address);
+}
+
+sub validate_email {
+    my $self = shift;
+    my $address = shift;
+
+    if($address =~ /bad\@email\.com/) {
+        return $self->validation_error('email', "Bad looking email");
+    } elsif ($address =~ /warn\@email\.com/) {
+        return $self->validation_warning('email', "Warning for email");
+    }
+    return $self->validation_ok('email');
+}
+
 sub take_action {
     my $self = shift;
 

Added: jifty/trunk/t/TestApp/t/06-validation.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp/t/06-validation.t	Wed May 10 17:25:07 2006
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+BEGIN {chdir "t/TestApp"}
+use lib '../../lib';
+use Jifty::Test tests => 13;
+use Jifty::Test::WWW::Mechanize;
+
+my $server  = Jifty::Test->make_server;
+
+isa_ok($server, 'Jifty::Server');
+
+my $URL     = $server->started_ok;
+my $mech    = Jifty::Test::WWW::Mechanize->new();
+
+$mech->get_ok("$URL/__jifty/validator.xml?J:A-dosomething=TestApp::Action::DoSomething&J:A:F-email-dosomething=good\@address.com&J:VALIDATE=1&_=",
+    "Getting validator.xml output for valid form entry");
+$mech->content_contains('<ok id="errors-J:A:F-email-dosomething" />',
+    " ... validator returned ok for errors");
+$mech->content_contains('<ok id="warnings-J:A:F-email-dosomething" />',
+    " ... validator returned ok for warnings");
+
+$mech->get_ok("$URL/__jifty/validator.xml?J:A-dosomething=TestApp::Action::DoSomething&J:A:F-email-dosomething=warn\@email.com&J:VALIDATE=1&_=",
+    "Getting validator.xml output for a warning form entry");
+$mech->content_contains('<ok id="errors-J:A:F-email-dosomething" />',
+    " ... validator returned ok for errors");
+$mech->content_contains('<warning id="warnings-J:A:F-email-dosomething">Warning for email</warning>',
+    " ... validator returned warning");
+
+$mech->get_ok("$URL/__jifty/validator.xml?J:A-dosomething=TestApp::Action::DoSomething&J:A:F-email-dosomething=bad\@email.com&J:VALIDATE=1&_=",
+    "Getting validator.xml output for a warning form entry");
+$mech->content_contains('<error id="errors-J:A:F-email-dosomething">Bad looking email</error>',
+    " ... validator returned error");
+$mech->content_contains('<ok id="warnings-J:A:F-email-dosomething" />',
+    " ... validator returned ok for warnings");
+
+$mech->get_ok("$URL/__jifty/validator.xml?J:A-dosomething=TestApp::Action::DoSomething&J:A:F-email-dosomething=UPPER\@EMAIL.com&J:VALIDATE=1&_=",
+    "Getting validator.xml output for a canonicalization");
+$mech->content_contains('<update name="J:A:F-email-dosomething">upper at email.com</update>',
+    " ... canonicalizer returned all lower case (good)");
+


More information about the Jifty-commit mailing list