Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 扩展CI库表单验证时出现的问题_Php_Codeigniter_Validation_Twitter Bootstrap 3_Continuous Integration - Fatal编程技术网

Php 扩展CI库表单验证时出现的问题

Php 扩展CI库表单验证时出现的问题,php,codeigniter,validation,twitter-bootstrap-3,continuous-integration,Php,Codeigniter,Validation,Twitter Bootstrap 3,Continuous Integration,我尝试从Codeigniter扩展Form_验证库,但没有成功 我希望在这个扩展类中有两个函数,但在运行验证时,我会在日志中看到以下消息: 调试-06-07-2016 11:20:33-->找不到验证规则:checkAccountnameForUpdate 调试-06-07-2016 11:20:33-->找不到验证规则:checkEmailForUpdate 这是我的扩展类,放在 应用程序/库 我在谷歌上搜索了很多不同的东西。我不知道为什么它不起作用 编辑1: 我已经试着换了 $this-&g

我尝试从Codeigniter扩展Form_验证库,但没有成功

我希望在这个扩展类中有两个函数,但在运行验证时,我会在日志中看到以下消息:

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

这是我的扩展类,放在

应用程序/库

我在谷歌上搜索了很多不同的东西。我不知道为什么它不起作用

编辑1:

我已经试着换了

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


没有更改。

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

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_格式
验证程序功能

关于如何使用
form\u validation->set\u rules()

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

第二。注意,我正在传递第四个参数。第四个论点没有很好的记录。本节中显示了它的用途,但本文档的本节中未对其进行说明。参数接受格式为['rule_name'=>'这是错误消息']的错误消息数组

我还习惯使用简短的语法来创建数组,例如
$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'];
    }
  }

}
类Testcase扩展CI_控制器
{
受保护的数据;
函数_u构造()
{
父项::_构造();
$this->data=[];
$this->load->library('form_validation');
}
公共功能测试和验证()
{
$this->form_validation->set_规则('phone','phone'
,['trim','required','phone_format']
, [
“电话号码格式”=>“不是有效的电话号码。”,
'必需'=>'{field}必需。'
]
);
如果($this->form\u validation->run()==FALSE)
{
$this->load->view('test\u form\u v',$this->data);
}
其他的
{
echo“通过验证
”; 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();

您使用的是CodeIgniter 3吗?您希望在函数中传递两个变量,但是
set_rules
仅为每个规则发送一个变量。在本例中,
$this->input->post('editAccountName')
,两个函数都缺少
$this->form\u validation->set\u message()
。@Aniruddhakaraborty-Yes CI3.@Jordy-我有set\u message()。它已被列入主要职位。但是我读到我可以这样传递两个变量。我使用了你的精确方法,包括传递两个参数,它工作得非常好。我不明白你为什么会有问题。没有其他错误消息?感谢您在这里提供的详细帮助,但它仍然不起作用。我甚至将Form_验证库设置为自动加载。您是否如我所介绍的那样创建了一个简单的“testcase”控制器?这样做可以帮助您缩小问题的范围。我发现它甚至不调用扩展库中的函数。当我将函数设置为只返回TRUE时,也会出现验证错误。
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;
  }
}
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'];
    }
  }

}
<?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();