Cyclone
Cyclone

Reputation: 15269

Use another rule if callback returns false

I'm using the following rule for my input:

callback_validate_host

I need to make the following condition:

if callback_validate_host is FALSE afterwards it should use the valid_ip validation rule.

So if validation of both: callback_validate_host and valid_ip on one input if FALSE then is should throw an error message.

How can I do that?

Upvotes: 0

Views: 128

Answers (1)

Colin Brock
Colin Brock

Reputation: 21565

How about using your existing validate_host() method in conjunction with the Input class' $this->input->valid_ip($ip) method to create a single callback? Something like this:

public function your_custom_rule($input) {
    if (! $this->validate_host($input) && ! $this->input->valid_ip($input)) {
        // validate_host() returned FALSE *and* it's not a valid IP
        $this->form_validation->set_message('your_custom_rule', 'Error msg');
        return FALSE;
    } else {
        return TRUE;
    }
}

Upvotes: 1

Related Questions