• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI Form_validation problem

#1
[eluser]i_am_using_ci[/eluser]
Code:
<?php

class Test extends Controller {

    public function __construct() {
    
        parent::__construct();
        
        $this->load->library('form_validation');
    
    }

    function delete() {

        $rules = array(
    
            array(
                'field' => 'to_delete[]',
                'label' => 'IDs',
                'rules' => 'callback__ids_handler'
            ),
        
            array(
                'field' => 'template_id',
                'label' => 'Template ID',
                'rules' => 'callback__template_exists'
            )
        
        );
        
        $this->form_validation->set_rules($rules);
        
        if($this->form_validation->run()) {
        
            /*
                here after validation process I am getting $ids as string, NOT ARRAY AS IT MUST BE!
            */
        
            $ids = $this->input->post('to_delete'); /* $ids COMES HERE AS STRING, WHY??? */
            $template_id = $this->input->post('template_id');
            
            ...
        
        }
        
    }
    
    function _ids_handler($ids = array()) {

        /* ids comes here as string, but why? */

        $to_return = array();

        if(!empty($ids)) {

            $ids = array_map('intval', array_map('trim', $ids));

            foreach($ids as $oneID) {
    
                if($id > 0) {
                    $to_return[] = $id;    
                }

            }

        }

        return $to_return;

    }
    
    function _template_exists($template_id = 0) {
    
        ...
    
    }

}

?>

The problem is:

When I submit my form I am getting integer from to_delete[] array after $this->form_validation->run(), I've tried to post it as bug, but got an advice to ask here, maybe I made mistake somewhere...

#2
[eluser]TheFuzzy0ne[/eluser]
The validation callback should return TRUE if validation was successful, and FALSE is it wasn't. Also, you'll need to grab the array by reference.

Code:
function _ids_handler(&$ids = array()) { // grab the argument by reference.

    /* ids comes here as string, but why? */

    $to_return = array();

    if(!empty($ids)) {
        $ids = array_map('intval', array_map('trim', $ids));
        foreach($ids as $id) {
            if($id > 0) {
                $to_return[] = $id;    
            }
        }
        $ids = $to_return; // Set $ids with the contents $to_return.
        return TRUE; // Return TRUE.
    }
    return FALSE; // Otherwise, it's FALSE.
}
The code above is untested

With a bit of luck, that should work for you.

#3
[eluser]TheFuzzy0ne[/eluser]
And your constructor is still wrong...

#4
[eluser]pistolPete[/eluser]
[quote author="TheFuzzy0ne" date="1234834097"]The validation callback should return TRUE if validation was successful, and FALSE is it wasn't.[/quote]

Well, not really. The user guide says:
Quote:If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data.

#5
[eluser]TheFuzzy0ne[/eluser]
I missed that. Now I remember, because trim passes back the data. Also, I've realised that it is a string that being passed to the validation function and not an array as I'd thought.

#6
[eluser]i_am_using_ci[/eluser]
Same typo error for class constructor, sorry, let me try to use reference (but without template_id with only to_delete[] argument validation was successfull and correct)

#7
[eluser]TheFuzzy0ne[/eluser]
That validation library appears to iterate through the object, and pass each one to the function (so the validation function is called once for each row in your array).

#8
[eluser]TheFuzzy0ne[/eluser]
I think this thread may help you. http://ellislab.com/forums/viewthread/51260/

#9
[eluser]i_am_using_ci[/eluser]
Ah, okay, now understand that logic. Got my code working.

But still same question, how can it be when I have validation method for associative array and it's the only thing that I send throught post form - it's get validated successfully, and when I set more than 1 rule in addition to that array rule, I need validation method for every array element?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.