CodeIgniter Forums
How to get single message for multiple objects? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to get single message for multiple objects? (/showthread.php?tid=981)



How to get single message for multiple objects? - Rajesh - 02-03-2015

Hi,
I am using custom validation and i have 3 fields to check but a single message coming for all 3 fields but i want i single message for my custom validation.

Here are the functions

public function save_product()
{
$title=$this->input->post('title');
$category=$this->input->post('category');
$sub_category=$this->input->post('sub_category');
$occasion=$this->input->post('occasion');
$this->form_validation->set_rules('title', 'Title', 'required');
//$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('category', 'Category', 'callback_fields_check');
$this->form_validation->set_rules('sub_category', 'Sub Category', 'callback_fields_check');
$this->form_validation->set_rules('occasion', 'Occasion', 'callback_fields_check');


if ($this->form_validation->run() == FALSE)
{

$this->load->view('product_form');
}
else
{
$
$this->load->view('welcome_message');
}
}

public function fields_check()
{
$category=$this->input->post('category');
$sub_category=$this->input->post('sub_category');
$occasion=$this->input->post('occasion');
if($category=='' && $sub_category=='' && $occasion=='')
{
$this->form_validation->set_message('fields_check', 'Any of category , sub_category ,occasion fields must fill');
return FALSE;
}
else
{
return TRUE;
}
}
Please check and let me know if possible.
thanks


RE: How to get single message for multiple objects? - RobertSF - 02-04-2015

Hi Rajesh. Fortunately, I think the solution to your problem is easy.

This line of code in your callback function
PHP Code:
if($category=='' && $sub_category=='' && $occasion==''

should be instead
PHP Code:
if($category=='' || $sub_category=='' || $occasion==''

This is because, when you use && (and), it's only TRUE if all three fields are '', but you want it to be TRUE if any field is '', so you must use || (or).