CodeIgniter Forums
Form Validation (Twice) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form Validation (Twice) (/showthread.php?tid=15510)



Form Validation (Twice) - El Forum - 02-06-2009

[eluser]PV-Patrick[/eluser]
Is it possible to run a validation process twice, one inside the other?

Example:
Code:
if($this->form_validation->run('val1') == FALSE):
  // -- False
else:
    if($this->form_validation->run('val2') == FALSE):
        // -- False
    else:
    endif;
endif;

For some reason I can't seem to get it to work... any help would be appreciated, thanks!


Form Validation (Twice) - El Forum - 02-06-2009

[eluser]xzela[/eluser]
do you have a reason on why you would want to run it twice?


Form Validation (Twice) - El Forum - 02-06-2009

[eluser]PV-Patrick[/eluser]
Ya, I'm trying to get past some credit card authorization problems that are apart of the form.


Form Validation (Twice) - El Forum - 02-06-2009

[eluser]Michael Wales[/eluser]
I still don't understand why you would need to validate twice... maybe if you provide a more detailed example we can help you out.


Form Validation (Twice) - El Forum - 02-06-2009

[eluser]macigniter[/eluser]
I am running the validation twice in one of my controllers because I want to validate one rule before all others. This way I don't get all of the error message of the second validation if the first one didn't even pass.

I am using this approach:

Code:
// set first validation rule
$this->form_validation->set_rules(...);

// only if first rule passed, check for other rules            
if ($this->form_validation->run() == TRUE)
{
    $this->form_validation->set_rules(...);
    $this->form_validation->set_rules(...);
}

// if first rule didn't pass the form only displays an error for the first rule
if ($this->form_validation->run() == FALSE)
{
    // then re-populate form
}

Is that something you anticipated?


Form Validation (Twice) - El Forum - 02-06-2009

[eluser]PV-Patrick[/eluser]
Hi Michael,
I am trying a different route for dealing with credit card validation/processing. Before, I would authorize the card at the same time as validating the form and stored the authorization status in a session variable. So if the form didn't validate, it wouldn't try and authorize their card on each failed form validation attempt. Thus, I was using an AUTH_ONLY, then PREVIOUS_SALE - 2 function calls for the credit card.

I was trying to bypass this method and use 2 validations and not set session variables so I can then use an AUTH_CONVERT for the CC. I validate the form first as there are two payment methods, 1 for credit card and 1 for paypal, which has two different validation arrays.

I hope that helps, thanks guys!

EDIT: @ macigniter

I guess my way about it is a more nested approach.... Not sure if it matters? Might need to pull it out of each other and basically have it similar to your case.

Code:
if($this->form_validation->run('val1') == FALSE):
    // -- Stop processing
else:
    $bool = TRUE;
endif;

if($bool):
    // -- run val2
endif;



Form Validation (Twice) - El Forum - 02-06-2009

[eluser]PV-Patrick[/eluser]
As an update, I tried doing it without the nesting and it doesn't work either...


Form Validation (Twice) - El Forum - 02-07-2009

[eluser]PV-Patrick[/eluser]
Any updates to this? Can't get it working...


Form Validation (Twice) - El Forum - 02-12-2009

[eluser]PV-Patrick[/eluser]
Does anyone have suggestions on how to work cc processing with CI validation of forms? I really don't want to use a session variable to store an auth status...


Form Validation (Twice) - El Forum - 02-13-2009

[eluser]Unknown[/eluser]
Hi,

I had the same problem today, and I made a very quick fix (Surely there is another more fashionable method).

I extended CI_Form_validation class, and I set my version of run method:

---------------------------------------------------------------------------------------

Code:
function run($group = '', $use_only_group = false)
    {
        // Do we even have any data to process?  Mm?
        if (count($_POST) == 0)
        {
            return FALSE;
        }
        
        if ($use_only_group) {
            // If user tells to use explicitly use
            // configuration from argument, then
            // don't reuse last configuration
            $this->_field_data = array();
        }      
        // Does the _field_data array containing the validation rules exist?
        // If not, we look to see if they were assigned via a config file
        if (count($this->_field_data) == 0)
        {
            // No validation rules?  We're done...
            if (count($this->_config_rules) == 0)
            {
                return FALSE;
            }
            
            // Is there a validation rule for the particular URI being accessed?
            $uri = ($group == '') ? trim($this->CI->uri->ruri_string(), '/') : $group;
            
            if ($uri != '' AND isset($this->_config_rules[$uri]))
            {
                $this->set_rules($this->_config_rules[$uri]);
            }
            else
            {
                $this->set_rules($this->_config_rules);
            }
    
            // We're we able to set the rules correctly?
            if (count($this->_field_data) == 0)
            {
                log_message('debug', "Unable to find validation rules");
                return FALSE;
            }
        }
    
        // Load the language file containing error messages
        $this->CI->lang->load('form_validation');
                            
        // Cycle through the rules for each field, match the
        // corresponding $_POST item and test for errors
        foreach ($this->_field_data as $field => $row)
        {        
            // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
            // Depending on whether the field name is an array or a string will determine where we get it from.
            
            if ($row['is_array'] == TRUE)
            {
                $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
            }
            else
            {
                if (isset($_POST[$field]) AND $_POST[$field] != "")
                {
                    $this->_field_data[$field]['postdata'] = $_POST[$field];
                }
            }
        
            $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);        
        }

        // Did we end up with any errors?
        $total_errors = count($this->_error_array);

        if ($total_errors > 0)
        {
            $this->_safe_form_data = TRUE;
        }

        // Now we need to re-set the POST data with the new, processed data
        $this->_reset_post_array();
        
        // No errors, validation passes!
        if ($total_errors == 0)
        {
            return TRUE;
        }

        // Validation fails
        return FALSE;
    }