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

[eluser]modano[/eluser]
Hello,

For example, a validation messages look like this:
"The %s field must contain a valid email address."

My problem is, I need to send the varaible %s twice, for example:
"%s The %s field must contain a valid email address."

But it will only replace %s with the form object name once, the second instance will be totally ignored and blank.

I cant find the code for where it replaces %s with the form object, but its aparently not looping through it, just looking for it once, how can i solve this?

regards,
modano
#2

[eluser]drewbee[/eluser]
I believe they use a type of sprintf for this.

Try using %1$s and see it that makes it come up each time.
#3

[eluser]modano[/eluser]
Thanks for your reply, but CI spits out this error message now:
Message: Undefined variable: s
#4

[eluser]drewbee[/eluser]
Hmm. I could have swore I had to do something similiar as well.

Maybe a second parameter is being passed ?

Try: %2$s

Other then that, I must be wrong on the sprintf thing.

I am also working from the 1.7 version to, so that may have something to do with it.
#5

[eluser]xwero[/eluser]
The sprintf is done near the end of the run method, in the Form_validation it's done in the _execute method but it's the same prucedure in both libraries. The first placeholder is for the field and the second for the rule parameter. The field and rule parameter are only known locally so you can't change them in a validation rule.

So if you want a message like "Attention the E-mail field must contain a valid email address." You need to set up your message like drewbee suggested "%2$s the %1$s field must contain a valid email address." and in the rules config do something like
Code:
$rules['email'] = 'valid_email[Attention]';
#6

[eluser]modano[/eluser]
Thanks for your replies guys, but thats not gonna do it either.
What im trying to do is to pass the name of the field at the very beginning of the error message, for example:
"username|The username field is required."
Why? Because Im using jquery to make an ajax call for the validation, and i want it to spit out the error message, and also highlight the textbox or object that was wrong. But to do that I must know the html tag ID of the cell, span, div or what, that the object in question is in.
So the first I would do is split the returned string by the pipe |, and use the first part to identify the html area to highlight, and then the second part to print out as a regular error message.

I'd hate to hard code it into each message, much rather use a varaible such as %s....


Also, Im using CI 1.6.3
#7

[eluser]xwero[/eluser]
In the new Form_validation library the field is added as a key to the error_array so you could make method in MY_Form_validation.php
Code:
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;
        }

        // Generate the error string
        $str = '';
        foreach ($this->_error_array as $field=>$val) // changed
        {
            if ($val != '')
            {
                $str .= $field.'|'.$prefix.$val.$suffix."\n"; // changed
            }
        }

        return $str;
    }
This is only a very slightly changed version of the error_string method in the library but if you want you can format it to a json string which is much easier to process in javascript.

For the Validation library you will have to change the last part of the run method to get the same result.
#8

[eluser]Ignacio[/eluser]
GREAT!
#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!




Theme © iAndrew 2016 - Forum software by © MyBB