CodeIgniter Forums
Ignored function in MY_Form_validation library - 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: Ignored function in MY_Form_validation library (/showthread.php?tid=13558)



Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]Geoffrey[/eluser]
I've got a couple of fields in a form that are required but only if another field has a certain value. I originally created a callback function that worked flawlessly except I couldn't privatise it and as such it was available via a url. I thought I'd try and extend the form validation class and put my function in there but I'm finding the function gets ignored if the fields are empty.

Is there anyway round this? Without have to rebuild the _execute function?

MY_Form_validation.php file is as follows...
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
    function MY_Form_validation($config = array())
    {
        parent::CI_Form_validation($config);
        $this->CI = get_instance();
    }
    
    function hodcheck($str)
    {
        $regType = $this->CI->input->post("regType");
        if ($regType == "Student Registration")
        {
            if (empty($str))
            {
                $this->set_message('hodcheck', 'The %s field can not be empty for a student registration');
                return FALSE;
            }
        }
        return TRUE;
    }
}

... and my call in the controller...
Code:
$this->form_validation->set_rules('hod_name', 'HoD Name', 'trim|xss_clean|hodcheck');
$this->form_validation->set_rules('hod_email', 'HoD Email Address', 'trim|valid_email|xss_clean|hodcheck');



Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]simshaun[/eluser]
http://ellislab.com/forums/viewthread/96903/

I had the same problem..


Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]Geoffrey[/eluser]
Similar but I don't want the fields to match. I want 2 fields to be required only if a third field has a certain value.


Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]simshaun[/eluser]
Ah, misunderstood.

What's the harm in someone being able to access the callback via the url? Chances are, nobody ever would considering they'd have to know the callback name.

Edit:
(Best solution): here

But you could also use this.


Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]Geoffrey[/eluser]
Yeah, the underscore solution is what I am leaning towards but I'd have preferred being able to use an extended form validation class.

Oh well. It'll have to do for now.


Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]uptime[/eluser]
[quote author="Geoffrey" date="1227762937"]Yeah, the underscore solution is what I am leaning towards but I'd have preferred being able to use an extended form validation class.

Oh well. It'll have to do for now.[/quote]

What's wrong with the underscore solution? Looks like it's enough in your case...


Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]Geoffrey[/eluser]
I just would've preferred it as an extended class and for code reuse.


Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]uptime[/eluser]
I need the same thing done, I need to have a "state" field required if the user selected "United States" as a country.

The only problem is that it would take about a minute to create a callback so extending the class is definitely a waste of time...

If I do extend the class, I'll post my code here so you (or anyone else) can use it.


Ignored function in MY_Form_validation library - El Forum - 11-26-2008

[eluser]hugle[/eluser]
Hi.

maybe try somth like that

Code:
if ($_POST) { // we check if form was submited.
  if (array_key_exists('regType', $_POST)) {
    echo 'regType is NOT null.';
  } else {
    echo 'regType IS null.';
  }
}