扩展CI库Form_validation的问题

时间:2016-07-06 09:36:30

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

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

我在这个扩展类中有两个函数但是在运行验证时我会在日志中获取这些消息:

  

DEBUG - 06-07-2016 11:20:33 - >无法找到验证规则:checkAccountnameForUpdate   DEBUG - 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.

我已经用Google搜索并厌倦了一些不同的东西。我不知道它为什么不起作用。

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

没有变化。

1 个答案:

答案 0 :(得分:0)

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

APPPATH /库/ 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->set_rules()的几点注意事项。

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

二。请注意我传递了第四个参数。第四个论点没有很好的记录。其用法显示在Setting Error Messages部分中,但未在文档的Class Reference部分中进行说明。该参数接受['rule_name'=&gt;形式的错误消息数组。 '这是错误信息']。

我也习惯使用短语法来创建数组,例如。 $this->data = [];代替$this->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

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