0

i have a problem with form validation, this is just standart form validation.

this is weird, even i set the condition, result always pass through the condition and run "$this->model_item_management->tambah_item($item);".

my data always saved without passing form validation. I tried with dummy input $user_name and passing the data to form_validation, but didn't work either. Thank in advanced. this is my code.

public function index(){
       
       $this->load->helper(array('form', 'url'));
       $this->load->library('form_validation');
       
       //this is dummy post
       $user_name =  $this->input->post('user_name');
        
       //i try to pass an input into variable and use it on form validation
       $this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');
       $this->form_validation->set_rules('namaitem', 'namaitem', 'required');
       $this->form_validation->set_rules('specitem', 'specitem', 'required');
       $this->form_validation->set_rules('masagaransi', 'masagaransi', 'required');
                
       if ($this->form_validation->run() == FALSE) {
			
           $this->load->view('template/header');
           $this->load->view('template/navigation');
           $this->load->view('pages/form_tambah_item');
           $this->load->view('template/footer');
		
       } else {
	   $this->load->view('pages/form_sukses_simpan_item');
       }
                
       $nama_item =  $this->input->post('namaitem');
       $spec_item =  $this->input->post('specitem');
       $garansi_item=  $this->input->post('masagaransi');
       
       $item = array(
           'NAMA_ITEM'=>$nama_item,
           'SPEC_ITEM'=>$spec_item,
           'MASA_GARANSI'=>$garansi_item
      );
       
        
        $this->model_item_management->tambah_item($item);
    }

<div class="container">
    
    <?php 
    
    
    $attributes = array('class' => 'form-horizontal', 'id' => 'form_tambah_item');
    echo form_open('kontrol_tambah_item',$attributes); ?>

    <div id="legend">
      <legend class="">Tambah Item Form</legend>
    </div>
    <?php echo form_label( 'username', 'user_name' ); ?>
            <?php echo form_input( 'user_name' ); ?>
    
    <div class="control-group">
      <!-- Nama item -->
      <label class="control-label"  for="namaitem">Nama Item</label>
      <div class="controls">
        <input type="text" id="namaitem" name="namaitem" placeholder="" class="input-xlarge">
        <p class="help-block">Masukkan nama dari items</p>
      </div>
    </div>
    
   <div class="control-group">
      <!-- spec item-->
      <label class="control-label"  >Spesifikansi Item</label>
      
      <div class="controls">
          <textarea class="input-xlarge" id="specitem" name="specitem" cols="40" rows="5"></textarea>
          <p class="help-block">Isi Spesifikasi Item</p>
      </div>
      </div>
    
    <div class="control-group">
      <!-- Masa garansi -->
      <label class="control-label"  for="masagaransi">Masa garansi</label>
      <div class="controls">
        <input type="text" id="masagaransi" name="masagaransi" placeholder="" class="input-xlarge">
        <p class="help-block">Isi Masa Garansi Item</p>
      </div>
    </div>
    
    <div class="control-group">
      <!-- Button -->
      <div class="controls">
          <button type="submit" class="btn btn-success">Tambah</button>
      </div>
    </div>
</div>

1
  • Try changing if ($this->form_validation->run() == FALSE) to if ($this->form_validation->run($this) == FALSE) Commented Dec 30, 2014 at 3:31

3 Answers 3

2

Current:-
$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');

Change to : -
$this->form_validation->set_rules('user_name', 'username', 'required|min_length[4]');

Things to know :
The above function takes three parameters as input:

1.The field name - the exact name you've given the form field.
2.A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". Note: If you would like the field name to be stored in a language file, please see Translating Field Names.
3.The validation rules for this form field.

Reference : https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

Thanks

Sign up to request clarification or add additional context in comments.

Comments

0

change

$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');

to

$this->form_validation->set_rules('user_name', 'username', 'required|min_length[4]');

Comments

0

this is so dumb, i forgot to add

<?php echo validation_errors(); ?>

in the view,

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.