Skip to content
Advertisement

Facing issue while using set_value() function in form_validation library in Codeigniter

I am using form_validation library in Codeigniter, I am facing an issue while using set_value() function.

It not sets the value when an error occure in form validation. Here is my form ==>

  <div class="form-group">
    <label for="first_name">First Name*</label>
    <input type="text" name="first_name" id="first_name" required placeholder="Enter First Name" class="form-control" <?php set_value('first_name'); ?>>
    <span id = "err_first_name"></span>
    <?php echo form_error('first_name', '<div class="error">', '</div>'); ?>
  </div>

Here is my Controller code ==>

$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');            
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|regex_match[/^[a-zA-Z]+$/]',array('required' => 'Please enter %s.', 'regex_match' => 'Only Characters allowed, no space'));
if ($this->form_validation->run() == FALSE)
{                
  $this->load->view('admin/user/add_user');
}            

Here I am attaching an image of the output. It does not show the previous entered value.

an empty textboxt with error message

Advertisement

Answer

Use set_value() as the value of value attribute

<input type="text" name="first_name" id="first_name" required placeholder="Enter First Name" class="form-control" value="<?php echo set_value('first_name'); ?>">
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement