📜  CodeIgniter-表单验证

📅  最后修改于: 2020-10-26 05:32:17             🧑  作者: Mango


验证是构建Web应用程序时的重要过程。它确保我们获取的数据正确且有效地存储或处理。 CodeIgniter使这项任务非常容易。让我们用一个简单的例子来了解这个过程。

创建一个视图文件myform.php并将下面的代码保存在application / views / myform.php中。该页面将显示用户可以提交其姓名的表格,我们将对此页面进行验证,以确保提交时不应为空。

My Form 
   
    
   
      
Name

创建一个视图文件formsuccess.php并将其保存在application / views / formsuccess.php中。如果表单验证成功,将显示此页面。

My Form
    
    
     
      

Your form was successfully submitted!

创建一个控制器文件Form.php并将其保存在application / controller / Form.php中。如果未正确验证此表单,它将显示错误,或者将其重定向到formsuccess.php页面。

load->helper(array('form'));
            
         /* Load form validation library */ 
         $this->load->library('form_validation');
            
         /* Set validation rule for name field in the form */ 
         $this->form_validation->set_rules('name', 'Name', 'required'); 
            
         if ($this->form_validation->run() == FALSE) { 
         $this->load->view('myform'); 
         } 
         else { 
            $this->load->view('formsuccess'); 
         } 
      }
   }
?>

application / config / routes.php中添加以下行。

$route['validation'] = 'Form';

让我们通过在浏览器中访问以下URL来执行此示例。根据您的站点,此URL可能有所不同。

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

它将产生以下屏幕-

验证表格

我们已经在控制器中添加了验证-提交表单之前,名称为必填字段。因此,如果单击提交按钮时未在名称字段中输入任何内容,则将要求您在提交之前输入名称,如下面的屏幕所示。

未成功验证

成功输入名称后,您将被重定向到如下所示的屏幕。

成功验证

在上面的示例中,我们使用了必需的规则设置。 CodeIgniter中有许多可用规则,如下所述。

验证规则参考

以下是可使用的所有本机规则的列表-

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_list

Yes Returns FALSE if the form element is not within a predetermined list. in_list[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.

valid_url

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

valid_email

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

valid_emails

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

valid_ip

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

valid_base64

No Returns FALSE if the supplied string contains anything other than valid Base64 characters.