Welcome Guest, Not a member yet? Register   Sign In
How to print errors from my helper & form validation
#11

[eluser]InsiteFX[/eluser]
If you are extending the form_validation class the it should be MY_Form_validation !

InsiteFX
#12

[eluser]4ever[/eluser]
[quote author="InsiteFX" date="1305962473"]If you are extending the form_validation class the it should be MY_Form_validation !

InsiteFX[/quote]

Does it have some effect on result? I think no. This is not problem.

Is here on forum some CI programmer? Author of the original library is "ExpressionEngine Dev Team"... I would like to know the person who thought this up.

[quote author="noctrum" date="1305930131"]Since your error_string() method isn't returning anything, how about posting it?[/quote]

Now I remember in the class I did some changes...

Code:
$this->_field_data[$field] = array(
            'field'                => $field,
            'label'                => $label,
            'rules'                => $rules,
            '0'                    => $field,
            '1'                    => $label,
            '2'                    => $rules,
            'is_array'            => $is_array,
            'keys'                => $indexes,
            'postdata'            => NULL,
            'error'                => ''
        );

and exchanged $row[$field] to $row[0], $row[$label] to $row[1] and $row[$rules] to $row[3] ... this has no effect to error_array which is accessed directly in run();
#13

[eluser]4ever[/eluser]
New thing I have found!

(I run the original class now saved as MY_form_validation )

I now that I have to run() before I can run the validate_errors()...

So there is the array set. But not always. I thought the array was set before I run(). Not so.

There are all input data saved in $this->_field_data

Code:
run() >>> foreach ($this->_field_data ...) {_execute()}

So the _execute() generates the error_array messages! Great!

So there is line with this code:

Code:
$this->_error_array[$row['field']] = $message;

This saves the array data. The line is there twice.

Maybe things are more clear now. Any idea how to solve this? How to get the array to be accessed from outer function?

Edit:
It seems to me, that after I run() so the array is cleared? It looks so. It is not in $CI anymore... So why it is not in $CI? Why is it cleared? When I run validate_errors().. Should I to save all errors to any other outer array?
#14

[eluser]4ever[/eluser]
OK, so here is my solution. Tell me what do you think about it:

Code:
function error_string($prefix = '', $suffix = '')
{        
    $my_glob_err_count = count($GLOBALS['saved_errors']); // my lib solution
        
// No errrors, validation passes!
        if (count($this->_error_array) === 0 && $my_glob_err_count===0 )
        {
            return '';
        }
        elseif ($my_glob_err_count) $this->_error_array =& $GLOBALS['saved_errors'];

Code:
function _execute(){
... // code shortened
$GLOBALS['saved_errors'][$row['field']] = $message;
... // code shortened
}

Form validation
Code:
if ($this->my_form_validation->run()==true) echo validation_errors('<p class="error">')."</p>"; // vypíše případné chyby ke špatně vyplněnému formuláři

When I do not enter passwords to my form then I got this output:

The Password field is required.
The Password Confirmation field is required.

I think it works, but I'm mot sure it is right to use $GLOBALS
#15

[eluser]4ever[/eluser]
So this is my code how I use it.

Code:
$this->load->library("MY_form_validation"); // my library
     // $this->load->library("MY_form_create"); // my create
     $this->load->helper("MY_form"); // my helper
     $conf = array ( array("name", "Name", "trim|required"),
             array("surname", "SurName", "trim|required"),
             array("email","Email address", "trim|required|valid_email"),
             array("psw", "Password", "trim|required|min_length[4]|max_length[32]"),
             array("psw2", "Password Confirmation", "trim|required|min_length[4]|max_length[32]|matches[psw]")            
           );
     $this->my_form_validation->set_rules($conf);
     $data['main_content_page'] = 'includes/forms/signup_form';    
  if ($this->my_form_validation->run() == false ) die(validation_errors('<p class="error">')."</p>");

I hope this is better way then this:

Code:
$config = array(
               array(
                     'field'   => 'username',
                     'label'   => 'Username',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'passconf',
                     'label'   => 'Password Confirmation',
                     'rules'   => 'required'
                  ),  
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'required'
                  )
            );

And now I am going to do my own class for simple creating and validating forms.


If anybody would to found the problem of previous post of mine, please let me know. I still want to know where was/is problem.




Theme © iAndrew 2016 - Forum software by © MyBB