jlocker
jlocker

Reputation: 1488

codeigniter ignores empty post data..issue with validation

how can i show the empty field that user submit??

if($this->input->post('name')){
    $data['name'] = $this->input->post('name');
}elseif(!empty($details)) {
    $data['name'] = $details['name'];
}else{
    $data['name'] = '';
}

But if user clear the input field and submit, it wont show the input as empty it will execute the elseif statement though user submit post request.

How to avoid this?

i want to say the field is empty

Upvotes: 0

Views: 83

Answers (2)

splash58
splash58

Reputation: 26153

$this->input->post returns false only when name is absent in POST array

$data['name'] = $this->input->post('name');
// here empty($data['name']) gives you correct answer
if (!$data['name'])
   $data['name'] = empty($details) ? '' : $details['name'];

Upvotes: 1

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

This validation checks whether the field in the $_POST exists or not.

If you want to restrict the user to avoid empty entries. There 2 approaches for that:

1) Client side (use jquery or javascript or just use html5's tag required='required' in <input tag that would work)

2) Server side,

if($this->input->post('name')){
   $setErrorMessage = "Empty field is not allowed";  
}

and return the user to the page along with the error message(s).

Upvotes: 1

Related Questions