扩展Codeigniter中的表单验证 [英] Extending form validation in Codeigniter

查看:137
本文介绍了扩展Codeigniter中的表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把这个名为'My_Form_validation.php'的类文件放到'application / core'中,我也试过把它放在'application / libraries'中。

在我的控制器中,我正在使用

$ $ $ this-> form_validation-> set_rules('user_postcode','Postcode',' valid_postcode |修剪|所需| xss_clean');

这是My_Form_validation.php中的内容 - 实际的逻辑在这里没有问题,因为我有一对的选项来实际验证邮政编码。我需要帮助的是理解为什么它没有加载或调用!



我的CI版本是
define('CI_VERSION','2.0.2' );

 <?php if(!defined('BASEPATH'))exit('No direct script access allowed') ; 
$ b $ / **
*表格验证英国邮政编码
*
*检查其有效的邮政编码
* @author James Mills< james @ koodoocreative.co.uk>
* @version 1.0
* @package FriendsSavingMoney
* /

class MY_Form_validation extends CI_Form_validation
{

function __construct( )
{
parent :: __ construct();
log_message('debug','*** Hello from MY_Form_validation ***');
}

函数valid_postcode($ postcode)
{

/ **
*
* UK邮政编码验证表达式来自Wikipedia
* http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
*
*注意:在插入数据库之前记住strtoupper()您的邮编!
*
* /

$ pattern =/ ^(GIR 0AA)|(((A [BL] | B [ABDHLNRSTX]?| C [ABFHMORTVW] | D [ADEGHLNTY] | E [HNX] | F [KY] | G [LUY] | H [ADGPRSUX] | I [GMPV] | JE | K [ATWY] | L [ADELNSU] | M [EKL] | N [EGNPRW] | 2 O [LX] | P [AEHLOR] | R [GHM] | S [AEGKLMNOPRSTY] | T [ADFNQRSW] | UB | W [ADFNRSV] | YO | ZE)[1-9] [? 0-9] |((E | N | NW | SE | SW | W)1 | EC [1-4] | WC [12])[A-HJKMNPR-Y] |(SW | W)([2- 9] | [1-9] [0-9])| EC [1-9] [0-9])[0-9] [ABD-HJLNP-UW-Z] {2})$ /;


if(preg_match($ pattern,strtoupper($ postcode)))
{
return TRUE;
}
else
{
$ this-> set_message('valid_postcode','请输入有效的邮政编码');
返回FALSE;




解决方案

因为你扩展了一个CodeIgniter库而不是一个核心组件,所以你希望把它放在 application / libraries (不是 application /核心)。



当然,不要忘记加载 Form_validation 在您的控制器代码中的库。



$ $ $ this-> load-> library('form_validation');

其他要检查的内容:


  • 文件名区分大小写( MY_Form_validation.php 加载,而 My_Form_validation.php 不会) li>
  • 类名区分大小写(类 MY_Form_validation 扩展 CI_Form_validation



  • 参考资料:


    I have placed this class file called 'My_Form_validation.php' into 'application/core' and I have also tried placing it in 'application/libraries'.

    In my controller I am using

    $this->form_validation->set_rules('user_postcode', 'Postcode', 'valid_postcode|trim|required|xss_clean');
    

    This is whats in My_Form_validation.php - the actual logic is not in question here because I have a couple of options to actually validate the postcode. What I need help with is understanding why it is not loading or getting called!

    My CI version is define('CI_VERSION', '2.0.2');

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    /**
     * Form validation for UK Postcodes
     * 
     * Check that its a valid postcode
     * @author James Mills <james@koodoocreative.co.uk>
     * @version 1.0
     * @package FriendsSavingMoney
     */
    
    class MY_Form_validation extends CI_Form_validation
    {
    
        function __construct()
        {
            parent::__construct();  
            log_message('debug', '*** Hello from MY_Form_validation ***');
        }
    
        function valid_postcode($postcode)
        {
    
            /**
             *
             * UK Postcode validation expression from Wikipedia
             * http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
             *
             * Note: Remember to strtoupper() your postcode before inserting into database!
             *
             */
    
            $pattern = "/^(GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9]) [0-9][ABD-HJLNP-UW-Z]{2})$/";
    
    
            if (preg_match($pattern, strtoupper($postcode)))
        {
                return TRUE;
            } 
            else
            {
                $this->set_message('valid_postcode', 'Please enter a valid postcode');
                return FALSE;
            }
        }
    }
    

    解决方案

    Because you're extending a CodeIgniter library and not a core component, you want to place that in application/libraries (not application/core).

    And of course, don't forget to load the Form_validation library within your controller code.

    $this->load->library('form_validation');
    

    Other things to check:

    • Filename case sensitivity (MY_Form_validation.php loads while My_Form_validation.php won't)
    • Class name case sensitivity (class MY_Form_validation extends CI_Form_validation)

    Reference material:

    这篇关于扩展Codeigniter中的表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆