Welcome Guest, Not a member yet? Register   Sign In
set_error_delimiters problem
#1

[eluser]Mitja B.[/eluser]
The source code of error is

<div class="errorValid">The Naslov field is required.</div>
<div class="errorValid">The URL field is required.</div>

and i am using

$this->validation->set_error_delimiters('<div class="errorValid">', '</div>');

How can i make that all errors will be inside one div like

<div class="errorValid">
The Naslov field is required.<br />
The URL field is required.
</div>

Thx
#2

[eluser]pistolPete[/eluser]
I assume you are using CI 1.7.0+
The function which builds the error string looks like:
Code:
(...)
        // Generate the error string
        $str = '';
        foreach ($this->_error_array as $val)
        {
            if ($val != '')
            {
                $str .= $prefix.$val.$suffix."\n";
            }
        }

If you want to change that behaviour, you have to subclass the form validation library:
MY_Form_validation.php
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

    function 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 = $prefix;
        foreach ($this->_error_array as $val)
        {
            if ($val != '')
            {    
                // I added a line break here
                $str .= $val.'<br />';
            }
        }
        
        return $str.$suffix;
    }

}

/* End of file MY_Form_validation.php */
/* Location: ./system/application/libraries/MY_Form_validation.php */
#3

[eluser]Mitja B.[/eluser]
Thx for your help it works
#4

[eluser]TheFuzzy0ne[/eluser]
Any reason why
Code:
&lt;?php echo validation_errors(); ?&gt;
won't work?
#5

[eluser]pistolPete[/eluser]
It's just a wrapper function:
Code:
function validation_errors($prefix = '', $suffix = '')
{
        if (FALSE === ($OBJ =& _get_validation_object()))
        {
            return '';
        }

        return $OBJ->error_string($prefix, $suffix);
}
#6

[eluser]TheFuzzy0ne[/eluser]
Sure, but it returns ALL of the errors together does it not? Isn't that what the OP wanted, or am I misunderstanding something (as usual)?
#7

[eluser]pistolPete[/eluser]
Yes it returns all errors, but each error is wrapped into the error_delimiters.
But he wanted to use the delimiters only once.

Normal behaviour:
Code:
<delimiter>error message 1</delimiter>
<delimiter>error message 2</delimiter>
<delimiter>error message 3</delimiter>

Changed to:
Code:
<delimiter>
   error message 1
   error message 2
   error message 3
</delimiter>
#8

[eluser]TheFuzzy0ne[/eluser]
I believe:
Code:
$this->validation->set_error_delimiters('', '<br />');
$data['errors'] = '<div class="errorValid">' . validation_errors() . '</div>';
may be a better solution. I usually put this in my view:
Code:
&lt;?php if ($errors = validation_errors()): ?&gt;
    <div class="formError">&lt;?php echo $errors; ?&gt;</div>
&lt;?php endif; ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB