Aaron Alfonso
Aaron Alfonso

Reputation: 516

Codeigniter: Array Submission Validation

I am having a problem on validating using empty() on codeigniter.

It seemed that it always returns true, empty or not.

<input type="text" name="contactname[]" value="<?php echo set_value('contactname[]');?>">

Model:

if(empty($this->input->post('contactname'))) {
           return TRUE;
      } else {
            return FALSE;
          }

I really don't know what's the cause of this issue.

Upvotes: 1

Views: 173

Answers (4)

safin chacko
safin chacko

Reputation: 1390

Try this.

if (count($this->input->post('contactname')) > 0)
           return TRUE;
          } else {
          return FALSE;
          }

Upvotes: 0

naseeba c
naseeba c

Reputation: 1050

try this

$contact_name = $this->input->post('contactname[]');

if($contact_name != null) 
{
   return TRUE;
} else{
       return FALSE;
}

Upvotes: 0

Vikash Kumar
Vikash Kumar

Reputation: 1111

CodeIgniter provides a comprehensive form validation and data prepping class that helps minimize the amount of code you'll write. You can load library like this in your controller or model :

$this->load->library('form_validation');

To set validation rules you will use the set_rules() function like this :

$this->form_validation->set_rules('contactname[]', 'Contact name', 'required');

So you need to update your code with given below code -

$this->load->library('form_validation');
$this->form_validation->set_rules('contactname[]', 'Contact name', 'required');
if ($this->form_validation->run() == FALSE)
{
    echo validation_errors();
}
else
{
   // wrire you code here after validation run success
}

For for reference see this link - http://www.codeigniter.com/userguide2/libraries/form_validation.html

Upvotes: 0

Visarut Sae-Pueng
Visarut Sae-Pueng

Reputation: 353

try this

$contactname = $this->input->post('contactname')
if(empty($contactname)) {
           return TRUE;
      } else {
            return FALSE;
          }

Upvotes: 1

Related Questions