Jump to content

Form validation in CodeIgniter


booxvid

Recommended Posts

Requiring fields validation is already done but my other validations wouldn't work.

 

Here's my controller:

my validation process is placed with a validation function:

public function reg_validation(){
	       $this->form_validation->set_rules('firstname','First Name','required|min_length[5]');
	       $this->form_validation->set_rules('lastname','Last Name','required');
	       $this->form_validation->set_rules('gender','Gender','required');
	       $this->form_validation->set_rules('course','Course','required');
	       $this->form_validation->set_rules('interest','Interest','required');
	       $this->form_validation->set_rules('desc','Description','required');
	       $this->form_validation->set_rules('username','Username','required|min_length[5]|max_length[12]|is_unique[user.username]');
	       $this->form_validation->set_rules('password','Password','required|min_length[6]|max_length[12]');		
	       
	       $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
	       if($this->form_validation->run()==FALSE){
		       
		       $this->load->view('view_registration');
		    
		       }
	       else{
		       $this->create();
		       redirect();
			       
	       }	
	}

 

then here's my view_registration.php

 

[code=php:0]
                  <div>
                   
		<div class="form">

			<?php 
				$attributes = array('class'=>'well form-inline','id'=>'thisForm');
				echo form_open('',$attributes)?>
			<table cellspacing="2" cellpadding="10" border=0>
				<tr>
					<td><?php echo heading('REGISTRATION FORM',3);?></td>
				</tr>
				<tr>
					<td><?php echo form_label('First Name:','firstname')?></td>
					<td><?php 
							$data_input_fname = array('class'=>'input-medium',
												'name'=>'firstname',
												'id'=>'firstname',
												'maxlength'=>'100',
												'value'=>set_value());		

							echo form_input($data_input_fname);
							echo br(1);
							if(!$this->input->post('firstname')){
								echo form_error('firstname');}?>
					</td>
				</tr>
				<tr>
					<td><?php echo form_label('Last Name:','lastname')?></td>
					<td><?php 
							$data_input_lname = array('class'=>'input-medium',
												'name'=>'lastname',
												'id'=>'lastname',
												'maxlength'=>'100');

							echo form_input('lastname');
							echo br(1);
							if(!$this->input->post('lastname')){
							echo form_error('lastname');}?></td>
				</tr>
				<tr>
					<td><?php echo form_label('Gender:','gender')?></td>
					<td><?php
							$gender_f = array('name'=>'gender',
											'type'=>'radio'
											,'value'=>'Female');
							$gender_m = array('name'=>'gender',
											'type'=>'radio'
											,'value'=>'Male'); 
							echo form_radio($gender_f).' Female';
							echo nbs(3);
							  echo form_radio($gender_m).' Male';
							  echo br(1);
							  if(!$this->input->post('gender')){
							echo form_error('gender');}?></td>
				</tr>
				<tr>
					<td><?php echo form_label('Course:','course')?></td>
					<td><?php 
							$options = array(''=>'No Course',
											'Bachelor of Science in Information Technology'=>'BSIT',
											'Bachelor of Science in Computer Science'=>'BSCS',
											'Bachelor of Science in Accountancy'=>'BSA',
											'Bachelor of Science in Nursing'=>'BSN');

							echo form_dropdown('course',$options,'');
							echo br(1);
							 if(!$this->input->post('course')){
							echo form_error('course');}?></td>
				</tr>
				<tr>
					<td><?php echo form_label('Interests:','interest')?></td>
					<td><?php echo form_checkbox('interest','Music','').' Music';
							  echo br(1);
							  echo form_checkbox('interest','Art','').' Art';
							  echo br(1);
							  echo form_checkbox('interest','Speech','').' Speech';
							  echo br(1);
							  echo form_checkbox('interest','Logic','').' Logic';
							  echo br(1);
							   if(!$this->input->post('interest')){
							echo form_error('interest');}?></td>
				</tr>
				<tr>
					<td><?php echo form_label('Description:','desc')?></td>
					<td><?php 
							$data_textarea = array('type'=>'textarea',
											'name'=>'desc',
											'row'=>5,
											'cols'=>25);
							echo form_textarea($data_textarea);
							echo br(1);
							 if(!$this->input->post('desc')){
							echo form_error('desc');}?></td>
				</tr>
				<tr>
					<td><?php echo form_label('Username:','username')?></td>
					<td><?php 	$data_input = array('class'=>'input-large',
												'name'=>'username',
												'id'=>'username',
												'maxlength'=>'100');
								echo form_input($data_input);
						?> <span id="usr_verify" class="verify"></span><?php
								echo br(1);
								 if(!$this->input->post('username')){
							echo form_error('username');}?></td>
				</tr>
				<tr>
					<td><?php echo form_label('Password:','password')?></td>
					<td><?php 	$data_pass = array('class'=>'input-large',
												'name'=>'password',
												'id'=>'password',
												'maxlength'=>'100',
												'type'=>'password');
								echo form_password($data_pass);
								echo br(1);
								 if(!$this->input->post('password')){
							echo form_error('password');}?></td>
				</tr>
				<tr>
					<td colspan="2"><center>
					<?php 
							$submit_data = array('type'=>'submit',
												'class'=>'white button',
												'name'=>'submit_form',
												'value'=>'Submit!');
							echo form_submit($submit_data);

							$reset_data = array('type'=>'reset',
												'class'=>'white button',
												'name'=>'reset_form',
												'value'=>'Reset!');
							echo form_reset($reset_data);?></center>
					</td>
				</tr>
				<?php form_close();?>
			</table>
		</div>
	  </div>

[/code]

 

I don't know where I go wrong, it could give an error message for require fields but for the other rules that I set especially to the user and password, it won't display an error message

Link to comment
https://forums.phpfreaks.com/topic/268333-form-validation-in-codeigniter/
Share on other sites

This bit here is just plain wrong:

$this->form_validation->set_rules('password','Password','required|min_length[6]|max_length[12]');

 

I'm referring to, of course, the artificial limitation of the password's maximum length. Don't ever put such limitations on a password, as you're severely reducing the security of your users. A hash is the same length, no matter who long the original password is, so having a max length on it doesn't do anything good at all. (You are salting & hashing it, right?)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.