1

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.

2
  • Use isset() function isset ($this->input->post('contactname')) in if condition Commented Mar 4, 2016 at 4:03
  • set_value('contactname[]'), this function may cause the problem. Maybe contactname[] becomes empty on form submission as u don't pass any value as second parameter. Commented Mar 4, 2016 at 5:42

4 Answers 4

1

try this

$contactname = $this->input->post('contactname')
if(empty($contactname)) {
           return TRUE;
      } else {
            return FALSE;
          }
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Yes yes. I know the form validation. I wasn't talking about form validation though
0

try this

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

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

5 Comments

Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in... I'm having that error
eny error?? what is the result off ` $contact_name`
no error. but, still, the problem it is always returning TRUE, even though $this->input->post('contactname[]') has no value or is null.
what is the result of $this->input->post('contactname')
0

Try this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.