Welcome Guest, Not a member yet? Register   Sign In
CI 1.7.0 Form_Validation rules/callback function possible bug
#1

[eluser]i_am_using_ci[/eluser]
Hi,

I Have controller with couple of functions,

Code:
<?php

class Test extends Controller {

    public function __controller() {
    
        parent::__controller();
        
        $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');
            $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;

    }

}

?>

The problem is:

When I set:

Code:
<?php

$rules = array(
    'field' => 'to_delete[]',
    'label' => 'IDs',
    'rules' => 'callback__ids_handler'
);

?>

without checking template_id, everything was working normally, after adding template_id rule (and making $rules multidimensional array) everything came broken, $ids now string, not array, maybe I mistaked somewhere?

I've re-checked 3 times, so please help Smile
#2

[eluser]pistolPete[/eluser]
Your constructors aren't correct, they need to be changed to:
Code:
<?php

class Test extends Controller {

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

Set your rules to required as well:
Code:
$rules = array(
    
            array(
                'field' => 'to_delete[]',
                'label' => 'IDs',
                'rules' => 'required|callback__ids_handler'
            ),
        
            array(
                'field' => 'template_id',
                'label' => 'Template ID',
                'rules' => 'required|callback__template_exists'
            )
        
        );

How are your html input-fields named?
Does the callback function _template_exists() exist?
#3

[eluser]i_am_using_ci[/eluser]
My controller __constuct() method is correct, I am using php5 way, and it works, so don't worry.

I show just a part of my controller, _template_exists() method exists, my input fields named: 'to_delete[]' and 'template_id' as you can see.

One more example for avoiding any similar questions:

Code:
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);
        
        echo 'Before $this->form_validation->run(): ' . br();
        var_dump($_POST);

        if($this->form_validation->run()) {
        
            echo 'After $this->form_validation->run(): ' . br();
            var_dump($_POST);

            $ids = $this->input->post('to_delete');
            $template_id = $this->input->post('template_id');
            
            ...
        
        }
        
    }

I am getting result:

Code:
Before $this->form_validation->run():
array
  'template_id' => string '29' (length=2)
  'to_delete' =>
    array
      0 => string '57' (length=2)

After $this->form_validation->run():
array
  'template_id' => string '29' (length=2)
  'to_delete' =>
    array
      0 =>
        array
          empty
#4

[eluser]TheFuzzy0ne[/eluser]
[quote author="i_am_using_ci" date="1234728704"]My controller __constuct() method is correct, I am using php5 way, and it works, so don't worry.[/quote]

No, your constructor method is incorrect, and it doesn't work... Try putting a call to log_message() in there, or an echo statement. I promise you it won't be called. The "PHP5 way" is to use the __construct() method.

If you can sort out the obvious problem first, someone might be happy to help you further.
#5

[eluser]i_am_using_ci[/eluser]
Oh, sorry, my fault, I wasn't checking my source this way, of course parent::__construct(), It's just a typo mistake.

Please help with the main problem Smile
#6

[eluser]TheFuzzy0ne[/eluser]
This is not a bug. Please repost in the [url="http://ellislab.com/forums/viewforum/53/"]Code and Application Development[/url] forums, where there will be more people looking to help other users.
#7

[eluser]i_am_using_ci[/eluser]
Nice explanation, btw would you like to share your opinion why it's not a bug and how can I validate my form correctly?
#8

[eluser]i_am_using_ci[/eluser]
Got code working, it's not a bug, details: http://ellislab.com/forums/viewthread/105965/




Theme © iAndrew 2016 - Forum software by © MyBB