Author: Krapulator
Date: 2010-02-16 20:35:20 +0100 (Tue, 16 Feb 2010)
New Revision: 28061

Modified:
   
plugins/sfJqueryFormValidationPlugin/lib/sfJqueryFormValidationRules.class.php
   
plugins/sfJqueryFormValidationPlugin/modules/sfJqueryFormVal/templates/indexSuccess.php
Log:
Continued work on new, updated version.


Modified: 
plugins/sfJqueryFormValidationPlugin/lib/sfJqueryFormValidationRules.class.php
===================================================================
--- 
plugins/sfJqueryFormValidationPlugin/lib/sfJqueryFormValidationRules.class.php  
    2010-02-16 19:00:05 UTC (rev 28060)
+++ 
plugins/sfJqueryFormValidationPlugin/lib/sfJqueryFormValidationRules.class.php  
    2010-02-16 19:35:20 UTC (rev 28061)
@@ -11,6 +11,8 @@
        
         'sfValidatorEmail' => array(
           'rules' => array('email' => true),
+       'keymap' =>  array('pattern' => 'invalid'),
+       'msgmap' =>  array('pattern' => 'email'),
      ),
      
      'sfValidatorFile' => array(
@@ -44,8 +46,8 @@
      
        );
        private static $keymap = array(
-        'min_length' => 'minlength',
-        'max_length' => 'maxlength',
+                'min_length' => 'minlength',
+                'max_length' => 'maxlength',
        );
        
        public function __construct(sfForm $form)
@@ -76,8 +78,8 @@
     public function generateMessages()
     {
       $message = sizeof($this->messages) > 0 ? 
stripslashes(json_encode($this->messages)) : '{}';
-      // this is a nasty hack to return a function as a value
-      // see line 185 for the matching hackery
+      // this is a nasty hack to return a javascript function as an unquoted 
value
+      // see line 229 for the matching hackery
       $message = str_replace('"[[', 'function(a, elem)', $message);
       $message = str_replace(']]"', '', $message);
       $message = str_replace('\" +', '" +', $message);
@@ -152,15 +154,10 @@
     
     private function processMessages($validation_name, sfWidget $objField)
     {
-       $field_options = $objField->getOptions();
-       $messages = $objField->getMessages();
-       $class = get_class($objField);
-       foreach($field_options as $key => $val)
-       {
-               $msg_key = $this->parseMessageKey($key, $objField);
-               if($messages[$msg_key])
-                 $this->addMessage($validation_name, 
isset(self::$widgets[$class]['msgmap'][$key]) ? 
self::$widgets[$class]['msgmap'][$key] : $key, 
$this->parseMessageVal($messages[$msg_key], $objField));
-       }
+      foreach($objField->getOptions() as $key => $val)
+      {
+        $this->addMessage($validation_name, $this->outputMessageKey($key, 
$objField), $this->parseMessageVal($key, $objField));
+      }
     }
     
     private function parseMessageKey($key, sfWidget $objField)
@@ -177,11 +174,46 @@
        return $key;
     }
     
-    private function parseMessageVal($val, sfWidget $objField)
+    private function outputMessageKey($key, sfWidget $objField)
     {
-       
+      $class = get_class($objField);
+      if(isset(self::$widgets[$class]['msgmap'][$key]))
+      {
+        $key = self::$widgets[$class]['msgmap'][$key];
+      }
+      elseif(isset(self::$keymap[$key]))
+      {
+        $key = self::$keymap[$key];
+      }
+      return $key;
+    }    
+    
+    private function parseMessageVal($key, sfWidget $objField)
+    {
+
        $field_options = $objField->getOptions();
+       $messages = $objField->getMessages();
+       $val = "";
        
+       // if the field options for this item is empty, don't include it
+       if(strlen($field_options[$key]) == 0) return "";
+       if((isset($messages[$key]) || 
isset($messages[$this->parseMessageKey($key)])) && 
isset($this->rules[$validation_name])) return "";
+       
+       // find the actual error message
+       $mapped_key = $this->parseMessageKey($key, $objField);
+       if(isset($messages[$key]))
+       {
+               $val = $messages[$key];
+       }
+      else if(isset($messages[$mapped_key]))
+      {
+        $val = $messages[$mapped_key];
+      }        
+       else
+       {
+               return;
+       }
+       
        // Make the required message a bit more friendly
        if($val == 'Required.')
        {
@@ -192,7 +224,7 @@
        $val = addslashes($val);
        
        // replace any placeholder values
-       // this is a nasty hack (see line 74 for the matching hackery)
+       // this is a nasty hack (see line 81 for the matching hackery)
       if(strpos($val, '%value%') !== false)
       {
         $val = '[[{ return \'' . str_replace('%value%', "' + $(elem).val() + 
'", $val) . '\';}]]';
@@ -219,7 +251,8 @@
     
     private function addMessage($validation_name, $rule, $value)
     {
-      $this->messages[$validation_name][$rule] = $value;
+       if(strlen($value) > 0)
+        $this->messages[$validation_name][$rule] = $value;
     }
     
     private function createValidationName($form_name, $fieldname, $is_embedded)

Modified: 
plugins/sfJqueryFormValidationPlugin/modules/sfJqueryFormVal/templates/indexSuccess.php
===================================================================
--- 
plugins/sfJqueryFormValidationPlugin/modules/sfJqueryFormVal/templates/indexSuccess.php
     2010-02-16 19:00:05 UTC (rev 28060)
+++ 
plugins/sfJqueryFormValidationPlugin/modules/sfJqueryFormVal/templates/indexSuccess.php
     2010-02-16 19:35:20 UTC (rev 28061)
@@ -1,8 +1,8 @@
 jQuery(function($){
   
   $('#<?php echo $sf_jq_rules->getFirstFieldHtmlId() 
?>').parents('form').validate({
-    rules: <?php echo $sf_jq_rules->generateRules() ?>,
-    messages: <?php echo $sf_jq_rules->generateMessages() ?>,
+    rules: <?php echo ($sf_jq_rules->generateRules()) ?>,
+    messages: <?php echo ($sf_jq_rules->generateMessages()) ?>,
     wrapper: 'ul class=error_list',
     errorElement: 'li',
     errorPlacement: function(error, element) 
@@ -36,4 +36,85 @@
       return this.optional(element) || regexp.test(value);
   },
   "Invalid."
-);
\ No newline at end of file
+);
+
+<?php 
+function json_format($json)
+{
+
+    $tab = "  ";
+    $new_json = "";
+    $indent_level = 0;
+    $in_string = false;
+
+    $json_obj = json_decode($json);
+
+    if($json_obj === false)
+        return false;
+
+    $json = json_encode($json_obj);
+    $len = strlen($json);
+
+    for($c = 0; $c < $len; $c++)
+    {
+        $char = $json[$c];
+        switch($char)
+        {
+            case '{':
+            case '[':
+                if(!$in_string)
+                {
+                    $new_json .= $char . "\n" . str_repeat($tab, 
$indent_level+1);
+                    $indent_level++;
+                }
+                else
+                {
+                    $new_json .= $char;
+                }
+                break;
+            case '}':
+            case ']':
+                if(!$in_string)
+                {
+                    $indent_level--;
+                    $new_json .= "\n" . str_repeat($tab, $indent_level) . 
$char;
+                }
+                else
+                {
+                    $new_json .= $char;
+                }
+                break;
+            case ',':
+                if(!$in_string)
+                {
+                    $new_json .= ",\n" . str_repeat($tab, $indent_level);
+                }
+                else
+                {
+                    $new_json .= $char;
+                }
+                break;
+            case ':':
+                if(!$in_string)
+                {
+                    $new_json .= ": ";
+                }
+                else
+                {
+                    $new_json .= $char;
+                }
+                break;
+            case '"':
+                if($c > 0 && $json[$c-1] != '\\')
+                {
+                    $in_string = !$in_string;
+                }
+            default:
+                $new_json .= $char;
+                break;                   
+        }
+    }
+
+    return $new_json;
+}
+?>
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to