Welcome Guest, Not a member yet? Register   Sign In
Validate Multiple Inputs Together?
#1

[eluser]dustinsenos[/eluser]
Heyo,

I'm trying to validate a user's age. In order to do so, I need to validate the combination of three input boxes:

month, day, year.

Am I able to do so using custom _callbacks? Can I attach a _callback on the *entire* form before it's submitted? Can I throw a 'form validation error' myself outside of the _callback hook?

Really appreciate the help,
D
#2

[eluser]dustinsenos[/eluser]
So I've spent some time and found a solution that doesn't require custom code and is quite straight forward.

Synopses:
Store each input's value in a private instance variable through a custom _callback applied to each input and then validate the combination of their values in a hidden input's custom _callback.

Proof of Concept:

views/ageView.php
Code:
<form>
    <input type="text" name="ageMonth" value="<?= set_value('ageMonth'); ?>" />
    <input type="text" name="ageDay" value="<?= set_value('ageDay'); ?>" />
    <input type="text" name="ageYear" value="<?= set_value('ageYear'); ?>" />
    <input type="hidden" name="finalAgeValidation" />
</form>

config/form_validation.php:
Code:
$config = array('ageCheck' => array(array(
    'field'   => 'ageMonth',
    'label'   => 'Month Born',
    'rules'   => 'trim|required|max_length[2]|numeric|callback_monthCheck'
),
array(
    'field'   => 'ageDay',
    'label'   => 'Day Born',
    'rules'   => 'trim|required|max_length[2]|numeric|callback_dayCheck'
),
array(
    'field'   => 'ageYear',
    'label'   => 'Year Born',
    'rules'   => 'trim|required|min_length[4]|max_length[4]|numeric|callback_yearCheck'
),
array(
    'field'    => 'finalAgeValidation',
    'label'   => 'Age Validation',
    'rules'   => 'callback_finalAgeValidation'
)));

controller/AgeController.php
Code:
<?php
class Age extends Controller {

    private $month;
    private $day;
    private $year;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->form_validation->run('ageCheck');
    }
    
    public function monthCheck($month)
    {
        $this->month = $month;
    }
    
    public function dayCheck($day)
    {
        $this->day = $day;
    }
    
    public function yearCheck($year)
    {
        $this->year = $year;
    }
    
    public function finalAgeValidation()
    {

        echo '<h1>Age:</h1>';
        echo '<h2>Month: ' . $this->month . '</h2>';
        echo '<h2>Day: ' . $this->day . '</h2>';
        echo '<h2>Year: ' . $this->year . '</h2>';
        
        if (check what you have with your values...)
        {
            $this->form_validation->set_message('finalAgeValidation', "You must be born in the month of May!");
            return false;
        }
        
        return true;
        
    }

?&gt;

Hope that helps!

Cheers,
Dustin
#3

[eluser]pistolPete[/eluser]
You could simplify it a bit: You don't need monthCheck(), dayCheck() and yearCheck().

Code:
public function finalAgeValidation()
{
        $month = $this->input->post('ageMonth');
        $day = $this->input->post('ageDay');
        $year = $this->input->post('ageYear');

...
}

Another thought:
Why don't you use a single input field where the date is entered like "01/02/1967"; you could then split the values into day, month and year in a single callback.
#4

[eluser]dustinsenos[/eluser]
Ah good call on pulling out the $_POST vars.

In regards to the single input field, I don't have control over the UI for this particular implementation.

D




Theme © iAndrew 2016 - Forum software by © MyBB