• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Callback for many input

#1
I would have the need to use a callback for more 2 two inputs.

For example:
I have 4 inputs that do not have to be equal to each other.
Otherwise you receive an error.

How can I fix this problem?
Reply

#2
The docs are pretty good on this
http://www.codeigniter.com/user_guide/li...on-methods

Briefly it might look something like:

Controller
Code:
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends CI_Controller {

    public function index()
    {
        $page_data = array();
        $message = FALSE;
        
        $this->form_validation->set_rules('test1', 'Test Box 1', 'trim|required|max_length[2]');
        $this->form_validation->set_rules('test2', 'Test Box 2', 'trim|required|max_length[2]');
        $this->form_validation->set_rules('test3', 'Test Box 3', 'trim|required|max_length[2]');
        $this->form_validation->set_rules('test4', 'Test Box 4', 'trim|required|max_length[2]|callback_check_all_equal');
        if ($this->form_validation->run()) 
        {
            $message = 'All four the same';
        }
        
        $page_data['message'] = $message;
        $this->load->view('test_view', $page_data);

    }
    
    public function check_all_equal($test4)
    {
        $test1 = $this->input->post('test1');
        $test2 = $this->input->post('test2');
        $test3 = $this->input->post('test3');

        if (($test1 == $test2) && ($test2 == $test3) && ($test3 == $test4))
        {
            return TRUE;
        }
        else
        {
            $this->form_validation->set_message('check_all_equal', 'All the values must be equal');
            return FALSE;
        }
        
    }
}

Test View
Code:
<p><?php echo validation_errors(); ?></p>


<?php echo form_open('test'); ?>
    <input type="text" name="test1" id="test1" value="<?php echo set_value('test1'); ?>">
    <input type="text" name="test2" id="test2" value="<?php echo set_value('test2'); ?>">
    <input type="text" name="test3" id="test3" value="<?php echo set_value('test3'); ?>">
    <input type="text" name="test4" id="test4" value="<?php echo set_value('test4'); ?>">
    <input type="submit" value="save">
</form>
<?php if ($message) { ?>
    <?php echo $message; ?>
<?php } ?>

Hope that helps.

Best wishes,

Paul.

PS Edited as I forgot to prefix callback with callback_
Reply

#3
Thanks for the help, I have solved alone.
But really thanks Paul Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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