2
votes

How do I use a logical operator in codeigniter form validation rules

I have this code

$this->form_validation->set_rules('company_name', 'First name',  'trim|required');

which checks only one field. I have two fields: 1- company_name 2- username

what I want to do is something like this return true if either field if filled.

set_rules('company_name', 'Company Name' || 'username', 'User Name', 'trim|required'); (this will not work )

how do I achieve this using codeigniter form validation with || Or Logical Operator

3

3 Answers

2
votes

You can also use callback in rules

$this->form_validation->set_rules('company_name', 'First name',  'trim|required|callback_functionName');

function functionName(){
//do something and check anything here. return true this in any condition.
}
0
votes
$this->form_validation->set_rules('company_name', 'First name',  'trim|required');
$this->form_validation->set_rules('username', 'User Name',  'trim|required');

You can use like this

0
votes

You can set condition with:

if (!empty($this->input->post('company_name'))) {
    $this->form_validation->set_rules('company_name', 'Company name',  'trim|required');
} else {
    $this->form_validation->set_rules('username', 'User name',  'trim|required');
}