English 中文(简体)
CodeIgniter - Form Validation
  • 时间:2024-09-17

CodeIgniter - Form Vapdation


Previous Page Next Page  

Vapdation is an important process while building web apppcation. It ensures that the data that we are getting is proper and vapd to store or process. CodeIgniter has made this task very easy. Let us understand this process with a simple example.

Example

Create a view file myform.php and save the below code it in apppcation/views/myform.php. This page will display form where user can submit his name and we will vapdate this page to ensure that it should not be empty while submitting.

<html>
 
   <head> 
      <title>My Form</title> 
   </head>
	
   <body>
      <form action = "" method = "">
         <?php echo vapdation_errors(); ?>  
         <?php echo form_open( form ); ?>  
         <h5>Name</h5> 
         <input type = "text" name = "name" value = "" size = "50" />  
         <span><input type = "submit" value = "Submit" /></span>  
      </form>  
   </body>
	
</html>

Create a view file formsuccess.php and save it in apppcation/views/formsuccess.php. This page will be displayed if the form is vapdated successfully.

<html>
 
   <head> 
      <title>My Form</title>
   </head> 
	
   <body>  
      <h3>Your form was successfully submitted!</h3>  
      <p><?php echo anchor( form ,  Try it again! ); ?></p>  
   </body>
	
</html>

Create a controller file Form.php and save it in apppcation/controller/Form.php. This form will either, show errors if it is not vapdated properly or redirected to formsuccess.php page.

<?php
  
   class Form extends CI_Controller { 
	
      pubpc function index() { 
         /* Load form helper */ 
         $this->load->helper(array( form ));
			
         /* Load form vapdation pbrary */ 
         $this->load->pbrary( form_vapdation );
			
         /* Set vapdation rule for name field in the form */ 
         $this->form_vapdation->set_rules( name ,  Name ,  required ); 
			
         if ($this->form_vapdation->run() == FALSE) { 
         $this->load->view( myform ); 
         } 
         else { 
            $this->load->view( formsuccess ); 
         } 
      }
   }
?>

Add the following pne in apppcation/config/routes.php.

$route[ vapdation ] =  Form ;

Let us execute this example by visiting the following URL in the browser. This URL may be different based on your site.

http://yoursite.com/index.php/vapdation

It will produce the following screen −

Vapdation Form

We have added a vapdation in the controller − Name is required field before submitting the form. So, if you cpck the submit button without entering anything in the name field, then you will be asked to enter the name before submitting as shown in the screen below.

Not Vapdated Successfully

After entering the name successfully, you will be redirected to the screen as shown below.

Vapdated Successfully

In the above example, we have used the required rule setting. There are many rules available in the CodeIgniter, which are described below.

Vapdation Rule Reference

The following is a pst of all the native rules that are available to use −

Rule Parameter Description Example

required

No Returns FALSE if the form element is empty.

matches

Yes Returns FALSE if the form element does not match the one in the parameter. matches[form_item]

regex_match

Yes Returns FALSE if the form element does not match the regular expression. regex_match[/regex/]

differs

Yes Returns FALSE if the form element does not differ from the one in the parameter. differs[form_item]

is_unique

Yes Returns FALSE if the form element is not unique to the table and field name in the parameter. Note − This rule requires Query Builder to be enabled in order to work. is_unique[table.field]

min_length

Yes Returns FALSE if the form element is shorter than the parameter value. min_length[3]

max_length

Yes Returns FALSE if the form element is longer than the parameter value. max_length[12]

exact_length

Yes Returns FALSE if the form element is not exactly the parameter value. exact_length[8]

greater_than

Yes Returns FALSE if the form element is less than or equal to the parameter value or not numeric. greater_than[8]

greater_than_equal_to

Yes Returns FALSE if the form element is less than the parameter value, or not numeric. greater_than_equal_to[8]

less_than

Yes Returns FALSE if the form element is greater than or equal to the parameter value or not numeric. less_than[8]

less_than_equal_to

Yes Returns FALSE if the form element is greater than the parameter value, or not numeric. less_than_equal_to[8]

in_pst

Yes Returns FALSE if the form element is not within a predetermined pst. in_pst[red,blue,green]

alpha

No Returns FALSE if the form element contains anything other than alphabetical characters.

alpha_numeric

No Returns FALSE if the form element contains anything other than alphanumeric characters.

alpha_numeric_spaces

No Returns FALSE if the form element contains anything other than alphanumeric characters or spaces. Should be used after trim to avoid spaces at the beginning or end

alpha_dash

No Returns FALSE if the form element contains anything other than alphanumeric characters, underscores or dashes.

numeric

No Returns FALSE if the form element contains anything other than numeric characters.

integer

No Returns FALSE if the form element contains anything other than an integer.

decimal

No Returns FALSE if the form element contains anything other than a decimal number.

is_natural

No Returns FALSE if the form element contains anything other than a natural number − 0, 1, 2, 3, etc.

is_natural_no_zero

No Returns FALSE if the form element contains anything other than a natural number, but not zero − 1, 2, 3, etc.

vapd_url

No Returns FALSE if the form element does not contain a vapd URL.

vapd_email

No Returns FALSE if the form element does not contain a vapd email address.

vapd_emails

No Returns FALSE if any value provided in a comma-separated pst is not a vapd email.

vapd_ip

No Returns FALSE if the suppped IP is not vapd. Accepts an optional parameter of ‘ipv4’ or ‘ipv6’ to specify an IP format.

vapd_base64

No Returns FALSE if the suppped string contains anything other than vapd Base64 characters.
Advertisements