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

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

问题描述

我已将名为My_Form_validation.php"的类文件放入应用程序/核心"中,并且我也尝试将其放入应用程序/库"中.

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');

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

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!

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

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;
        }
    }
}

推荐答案

因为您要扩展 CodeIgniter 库而不是核心组件,所以您希望将其放置在 application/libraries(而不是 <代码>应用程序/核心).

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

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

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

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

其他需要检查的事项:

  • 文件名区分大小写(MY_Form_validation.php 加载而My_Form_validation.php 不会)
  • 类名区分大小写(class MY_Form_validation extends CI_Form_validation)
  • 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)

参考资料:

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

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