Welcome Guest, Not a member yet? Register   Sign In
Validation, multiple instances of %s?
#9

[eluser]Ignacio[/eluser]
Library extend (MY_Form_validation.php)
Code:
<?php

class My_Form_validation extends CI_Form_validation
{
    
    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;
    }    
    
    function ajax_error_string($prefix = '', $suffix = '')
    {
        // No errrors, validation passes!
        if (count($this->_error_array) === 0)
        {
            return '';
        }

        if ($prefix == '')
        {
            $prefix = $this->_error_prefix;
        }

        if ($suffix == '')
        {
            $suffix = $this->_error_suffix;
        }

        $str = '';

    foreach ($this->_error_array as $field=>$val)
    {
        if ($val != '')
        {
            $arr['errors'][] = array(
                                     "field" => $field,
                                     "message" =>  $prefix.$val.$suffix
                                     );
        }
    }

    $arr['errorCount'] = count($this->_error_array);
    
    return $this->json_format(json_encode($arr));
    }

}

?&gt;

Controller
Code:
$data['json'] = $this->form_validation->ajax_error_string();
$this->load->view('myview', $data);

Return Json like this
Code:
{
  "errors": [
    {
      "field": "name",
      "message": "<p>The Name field is required.<\/p>"
    },
    {
      "field": "email",
      "message": "<p>The Email field is required.<\/p>"
    },
    {
      "field": "username",
      "message": "<p>The Username field is required.<\/p>"
    },
    {
      "field": "password",
      "message": "<p>The Password field is required.<\/p>"
    }
  ],
  "errorCount": 4
}


Note: you can take away the json_format() function, its just for doing a pretty format of json. Hope this works for anybody. Thanks!


Messages In This Thread
Validation, multiple instances of %s? - by El Forum - 10-02-2008, 02:25 PM
Validation, multiple instances of %s? - by El Forum - 10-02-2008, 02:42 PM
Validation, multiple instances of %s? - by El Forum - 10-02-2008, 02:49 PM
Validation, multiple instances of %s? - by El Forum - 10-02-2008, 03:02 PM
Validation, multiple instances of %s? - by El Forum - 10-03-2008, 12:37 AM
Validation, multiple instances of %s? - by El Forum - 10-03-2008, 03:31 AM
Validation, multiple instances of %s? - by El Forum - 10-03-2008, 03:45 AM
Validation, multiple instances of %s? - by El Forum - 11-10-2008, 12:47 PM
Validation, multiple instances of %s? - by El Forum - 11-10-2008, 02:36 PM



Theme © iAndrew 2016 - Forum software by © MyBB