Welcome Guest, Not a member yet? Register   Sign In
Form validation combination of two fields
#1

[eluser]Unknown[/eluser]
Hi!

I have a form which has 2 fields, hours(text input) and minutes(dropdown) which make total duration. Total duration must be positive. How can i validate this?

I have a form_validation rule:
Code:
$this->form_validation->set_rules("minutes", 'Minutes', 'required|callback_duration_is_positive');

And i have a callback (which obviously do not work atm):
Code:
function duration_is_positive($minutes) {
        if ($minutes == 0) {
            $this->form_validation->set_message('duration_is_positive', 'Duration has to be positive.');
            return false;
        }
        else
            return true;
    }
Can i pass hours to callback somehow?

Thank you very much!
#2

[eluser]imn.codeartist[/eluser]
[quote author="Kolmiloikka" date="1276691324"]Hi!

I have a form which has 2 fields, hours(text input) and minutes(dropdown) which make total duration. Total duration must be positive. How can i validate this?

I have a form_validation rule:
Code:
$this->form_validation->set_rules("minutes", 'Minutes', 'required|callback_duration_is_positive');

And i have a callback (which obviously do not work atm):
Code:
function duration_is_positive($minutes) {
        if ($minutes == 0) {
            $this->form_validation->set_message('duration_is_positive', 'Duration has to be positive.');
            return false;
        }
        else
            return true;
    }
Can i pass hours to callback somehow?

Thank you very much![/quote]

Yes you can pass hours to call back

Code:
function duration_is_positive($minutes) {
        $hours=$this->input->post('your_hours'); //This is how you call other fields in callback

        if ($minutes == 0) {
            $this->form_validation->set_message('duration_is_positive', 'Duration has to be positive.');
            return false;
        }
        else
            return true;
    }
#3

[eluser]Unknown[/eluser]
Thanks, dixcoder! Yes it works!
#4

[eluser]Timothy_[/eluser]
Very Helpful,

Just what I was looking for.

Thanks!

Tim




Theme © iAndrew 2016 - Forum software by © MyBB