【问题标题】:Problems extending CI library Form_validation扩展 CI 库 Form_validation 的问题
【发布时间】:2016-11-08 07:32:56
【问题描述】:

我尝试从 Codeigniter 扩展 Form_validation 库,但没有成功。

我在这个扩展类中有两个我想要的功能,但是当验证运行时,我会在日志中收到这些消息:

DEBUG - 06-07-2016 11:20:33 --> 找不到验证规则:checkAccountnameForUpdate 调试 - 06-07-2016 11:20:33 --> 找不到验证规则:checkEmailForUpdate

这是我的扩展类,放在

应用程序/库

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation 
{
    public function __construct($rules = array())
    {
        // Pass the $rules to the parent constructor.
        parent::__construct($rules);
        // $this->CI is assigned in the parent constructor, no need to do it here.
    }

    public function checkAccountnameForUpdate($accountname, $id)
    {
        return 1;
    }

    public function checkEmailForUpdate($email, $id)
    {
        return 1;
    }
}

这是我的规则:

$this->form_validation->set_rules('editAccountname', 'Brugernavn', 'required|checkAccountnameForUpdate[hiddenField]');
$this->form_validation->set_rules('editEmail', 'Email', 'required|checkEmailForUpdate[hiddenField]');
$this->form_validation->set_message('checkAccountnameForUpdate', 'Test2');
$this->form_validation->set_message('checkEmailForUpdate', 'Test');
// hiddenField is the name of a hiddenField containing the users ID.

我用谷歌搜索了一些不同的东西。我不知道为什么它不起作用。

编辑 1:

我已经尝试替换

$this->form_validation->set_rules('editAccountname', 'Brugernavn', 'required|checkAccountnameForUpdate[hiddenField]');
$this->form_validation->set_rules('editEmail', 'Email', 'required|checkEmailForUpdate[hiddenField]');

$this->form_validation->set_rules('editAccountname', 'Brugernavn', 'required|checkAccountnameForUpdate');
$this->form_validation->set_rules('editEmail', 'Email', 'required|checkEmailForUpdate');

没有变化。

【问题讨论】:

  • 你在使用 CodeIgniter 3 吗?
  • 您期望在您的函数中传递 2 个变量,但 set_rules 每个规则只发送一个。在这种情况下$this-&gt;input-&gt;post('editAccountName'),您还缺少两个函数的$this-&gt;form_validation-&gt;set_message()
  • @AniruddhaChakraborty - 是 CI3。
  • @Jordy - 我有 set_message()。它已包含在主帖中。但我读到我可以通过这种方式传递两个变量。它只计算回调吗?
  • 我使用了您的确切方法,包括传递两个参数,并且效果很好。我不明白你为什么会遇到问题。没有其他错误消息?

标签: php codeigniter validation twitter-bootstrap-3 continuous-integration


【解决方案1】:

这是我对验证库的扩展。我省略了一些功能,因为它们是特定于站点的,但不影响此处的演示。

APPPATH/libraries/MY_Form_validation.php

class MY_Form_validation extends CI_Form_validation
{
  public function __construct($rules = array())
  {
    parent :: __construct($rules);
  }

  /*
   * reformats the input to ###-###-#### and returns formatted string, 
   * if it cannot accomplish the format (of a non-empty input) it returns FALSE.
   */
  public function phone_format($phone)
  {
    if(empty($phone))
    {
      //assume an empty field is OK. 
      //There needs to be a required rule to check a required field and that rule should
      //be before this one in the list of rules
      return TRUE;
    }
    $phone = preg_replace('/[^0-9]/', '', $phone);
    $newPhone = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
    if(preg_match('/((\d){3})?(\-){1}(\d){3}(\-){1}(\d){4}/', $newPhone))
    {
      //preg_replace was able to reformat it to the correct pattern - must be good
      return $newPhone;
    }
    return FALSE;
  }

  /**
   * field validation for zipcode
   */
  public function check_zipcode($zipcode)
  {
    $result = (bool) preg_match("/^([0-9]{5})(-[0-9]{4})?$/i", $zipcode);
    return $result;
  }
}

我们可以使用如下所示的简单控制器和视图来测试这个类。测试使用phone_format验证器函数。

关于我如何使用form_validation-&gt;set_rules() 的几点注意事项。

首先,我传递了一个规则数组,而不是通常演示的管道分隔字符串。例如。 “修剪|必需”。为什么?因为set_rules() 无论如何都会将字符串转换为数组,所以为什么要让它做额外的工作呢?

第二。请注意,我正在传递第四个参数。第四个论点没有很好的记录。它的使用在Setting Error Messages 部分中显示,但在文档的Class Reference 部分中没有描述。该参数接受格式为 ['rule_name' => 'This is the error message'] 的错误消息数组。

我也习惯于使用简短的语法来创建数组,例如。 $this-&gt;data = []; 而不是 $this-&gt;data = array(); 主要是因为它的打字更少。

这是测试控制器Testcase.php

class Testcase extends CI_Controller
{
  protected $data;

  function __construct()
  {
    parent::__construct();
    $this->data = [];
    $this->load->library('form_validation');
  }

  public function test_validation()
  {
    $this->form_validation->set_rules('phone', 'Phone'
      , ['trim', 'required', 'phone_format']
      , [
          'phone_format' => 'Not a valid phone number.', 
          'required' => '<em>{field}</em> required.'
        ]
    );

    if($this->form_validation->run() == FALSE)
    {
      $this->load->view('test_form_v', $this->data);
    }
    else
    {
      echo "Passed Validation<br>";
      echo $_POST['phone'];
    }
  }

}

这里是视图文件 test_form_v.php

<?php    
echo form_open('testcase/test_validation');
echo form_input('phone', set_value('phone'));
echo form_error('phone');
echo form_submit('Submit', 'Submit');
echo form_close();

在浏览器中输入此网址运行它your_doman.whatever/testcase/test_validation

在不输入任何输入的情况下提交,您会收到所需的错误。提交“123456789”,您会收到无效电话号码错误消息。提交“1234567890”即可获得

通过验证

123-456-7890

这表明扩展类及其新验证器有效。我希望这可以帮助您解决问题。

【讨论】:

  • 感谢您的详细帮助,但它仍然无法正常工作。我什至将 Form_validation 库设置为自动加载。
  • 您是否像我介绍的那样创建了一个简单的“测试用例”控制器?这样做可以帮助您缩小问题的可能范围。
  • 我发现它甚至没有调用扩展库中的函数。当我将函数设置为只返回 TRUE 时,它也会给我验证错误。
猜你喜欢
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-25
相关资源
最近更新 更多