Welcome Guest, Not a member yet? Register   Sign In
MY_Form_validation working for some functions
#1

[eluser]ash0803[/eluser]
Hi guys

I am having trouble using extending the CI_Form_validation class. Some of the functions work fine (validating on text input boxes) while others don't work at all. The functions don't even get invoked.

The functions that don't get invoked are called from validating an array of checkboxes and from validating a file upload input. If I use callback functions from within the actual controllers there's no problem. It's only when I put them in the MY_Form_validation class.

Here's a sample. Any ideas?

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation
{

    function __construct()
    {
        parent::__construct();
    }

    function check_valid_postcode($pc) // works absolutely fine
    {
        $ci=& get_instance();
        $ci->load->model('postcode_model');
        // first make the postcode correct format
        return $ci->postcode_model->validate_postcode($pc);
    }

    function check_nickname_unique($nickname,$cid) // also fine
    {
        $ci=& get_instance();
        if(!$ci->customer_account_model->is_nickname_unique($nickname,$cid))
        {
            $ci->form_validation->set_message('check_nickname_unique', 'Nickname is taken. Please choose another');
            return FALSE;
        }
        return TRUE;
    }

    function handle_upload($image,$v=array())
    {
        show_error('hello'); // this doesn't get invoked
        $ci=& get_instance();
        if(!empty($v)) $v='_'.$v;
        $ci->load->library('upload');

        if($ci->upload->do_upload('image_upload'.$v))
        {
            $_POST['image_upload'.$v] = $this->upload->data();
            return TRUE;
        }
        else
        {
            $ci->form_validation->set_message('handle_upload', $ci->upload->display_errors());
            return FALSE;
        }
    }

}

Thanks
#2

[eluser]CroNiX[/eluser]
Form validation expects either a boolean TRUE/FALSE to be returned, or the NEW value that the field should be (like if you are running trim() on it). It won't show anything echoed as this isn't the place to do that. It only works with what is returned back to the controller or the error message set within the validation rule.
#3

[eluser]ash0803[/eluser]
Thanks for your time. I am aware that it requires a boolean return value. I was just demonstrating that the code doesn't get invoked at all. Anyway, I have figured out why the file upload validation doesn't happen. The CI validation class checks on the post variables for the field name but as its a file upload it doesn't exist in $_POST but in $_FILES instead. But if you have a callback validation function this overrides this check and the code gets run.

I still have a problem with invoking an extended validation function on an array of checkboxes still if anyone has an idea?

Thanks
#4

[eluser]Unknown[/eluser]
I know this thread is pretty old but I was wondering if you found a solution to the checkbox thing. I've got the same problem where I can validate a checkbox field via a callback function in the controller, but if I try to put the same function in MY_Form_validation.php, it doesn't even get called. I've got a few other functions that are working fine that way, but they are all validating text input.

Thanks for your time!




Theme © iAndrew 2016 - Forum software by © MyBB