Welcome Guest, Not a member yet? Register   Sign In
Running $this->form_validation->run(###) more than once?
#1

[eluser]gevans[/eluser]
I have the follwoing code;

Code:
function _validate_step($step)
    {
        //over-ride error messages
        $this->form_validation->set_message('required', 'This field is required');
        $this->form_validation->set_message('alpha_numeric', 'Field may only contain letters and numbers');
        $this->form_validation->set_message('valid_email', 'This field must contain a valid email');
        $this->form_validation->set_message('matches', 'The email fields must match');

        //Run validation
        if($this->form_validation->run("step$step") === FALSE)
        {
            /**
             * if there's a validation error on a step
             * earlier than the one being checked
             * set to the earlier step
             */
            if($step < $this->step_to_show)
                $this->step_to_show = $step;
                
            #echo validation_errors();
        }
    }

This is called by a different method where $step sent to this method is an int and is used to reference the following;

Code:
&lt;?php


$config = array(

    "step1" => array(
        array(
            "field" => "title",
            "label" => "",
            "rules" => "trim|required"
        ),
        array(
            "field" => "fname",
            "label" => "",
            "rules" => "trim|required|alpha_numeric"
        ),
        array(
            "field" => "email",
            "label" => "",
            "rules" => "trim|required|valid_email"
        ),
        array(
            "field" => "conf-email",
            "label" => "",
            "rules" => "trim|required|valid_email|matches[email]"
        )
    ),
    
    "step2" => array(
        array(
            "field" => "address-line-1",
            "label" => "",
            "rules" => "trim|required"
        ),
        array(
            "field" => "address-line-2",
            "label" => "",
            "rules" => "trim|required"
        )
    ),
    
    "step3" => array(
    
    )
    
);

?&gt;

Is this do-able, calling run a few times?

Cheers,
gevans
#2

[eluser]Kamarg[/eluser]
You'll need something similar to the below code in MY_Form_Validation library. The form validation library doesn't have a way to clear out the data between calls to run() so you have to do it yourself.

Code:
function clear_rules() {
    $this->_field_data     = array();
    $this->_error_array    = array();
    $this->_error_messages = array();
}
#3

[eluser]gevans[/eluser]
Thanks for that, I got there myself Wink I'm doing this;
Code:
function _validate_step($step)
    {
        $this->form_validation->_field_data = array();
        //Run validation
        if($this->form_validation->run("step$step") === FALSE)
        {
            /**
             * if there's a validation error on a step
             * earlier than the one being checked
             * set to the earlier step
             */
            if($step < $this->step_to_show)
                $this->step_to_show = $step;
        }
        $this->_field_data[$step] = $this->form_validation->_field_data;
    }

then later doing this;

Code:
function set_errors()
    {
        $this->form_validation->_field_data = array();
        if($this->override !== false)
            $this->form_validation->_field_data = isset($this->_field_data[$this->override])? $this->_field_data[$this->override] : array() ;
        else
            $this->form_validation->_field_data = isset($this->_field_data[$this->step_to_show])? $this->_field_data[$this->step_to_show] : array() ;

    }

Allows me to work through a 4 stage sig up process, backwards and forward, storing and validating previosuly submitted fields!




Theme © iAndrew 2016 - Forum software by © MyBB