CodeIgniter + hmvc form_validation 规则 "is_unique" 未应用
CodeIgniter + hmvc form_validation rule "is_unique" not applied
我似乎无法弄清楚我的问题希望有人能提供帮助。
我正在使用 codeigniter 3 +HMVC,在我的表单验证中我使用规则 is_unique 它工作完美,除了如果我想使用回调,我需要扩展 CI_form_validation 像这样:
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
"When using form validation with MX you will need to extend the CI_Form_validation class as shown below,"
<?php
/** application/libraries/MY_Form_validation **/
class MY_Form_validation extends CI_Form_validation
{
public $CI;
}
"before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly. (This has been discussed on the CI forums also)."
<?php
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
}
当我进行此修改时,规则 "is_unique" 停止工作。
有谁知道它是什么?这是一个错误吗?
这是我的代码:
class Prod_parent extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
function submit() {
$this->form_validation->set_error_delimiters('<p style="color:red;">', '</p>');
$this->form_validation->set_rules('grupo', 'nombre del grupo', 'trim|required|is_unique[prod_parent.product]');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('error', validation_errors());
$this->new();
die();
} else {
die('works great');
}
感谢大家的帮助....我需要休息一下!
你可以参考这段代码。
if($this->input->post('user_name') != $original_value) {
$is_unique = '|is_unique[users.user_name]'
} else {
$is_unique = ''
}
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);
system/libraries/Form_validation.php
在第 1127 行
将 isset() 更改为 is_object()
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
return is_object($this->CI->db)
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
在application/libraries/MY_Form_validation中只需使用下面的代码:
<?php
class MY_Form_validation extends CI_Form_validation {
public $CI;
/**
* Is Unique
*
* Check if the input value doesn't already exist
* in the specified database field.
*
* @param string $str
* @param string $field
* @return bool
*/
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
//return isset($this->CI->db)
return is_object($this->CI->db)
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
}
这将覆盖 form_validation class
的 is_unique 方法
我似乎无法弄清楚我的问题希望有人能提供帮助。 我正在使用 codeigniter 3 +HMVC,在我的表单验证中我使用规则 is_unique 它工作完美,除了如果我想使用回调,我需要扩展 CI_form_validation 像这样:
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
"When using form validation with MX you will need to extend the CI_Form_validation class as shown below,"
<?php
/** application/libraries/MY_Form_validation **/
class MY_Form_validation extends CI_Form_validation
{
public $CI;
}
"before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly. (This has been discussed on the CI forums also)."
<?php
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
}
当我进行此修改时,规则 "is_unique" 停止工作。 有谁知道它是什么?这是一个错误吗?
这是我的代码:
class Prod_parent extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
function submit() {
$this->form_validation->set_error_delimiters('<p style="color:red;">', '</p>');
$this->form_validation->set_rules('grupo', 'nombre del grupo', 'trim|required|is_unique[prod_parent.product]');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('error', validation_errors());
$this->new();
die();
} else {
die('works great');
}
感谢大家的帮助....我需要休息一下!
你可以参考这段代码。
if($this->input->post('user_name') != $original_value) {
$is_unique = '|is_unique[users.user_name]'
} else {
$is_unique = ''
}
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);
system/libraries/Form_validation.php 在第 1127 行
将 isset() 更改为 is_object()
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
return is_object($this->CI->db)
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
在application/libraries/MY_Form_validation中只需使用下面的代码:
<?php
class MY_Form_validation extends CI_Form_validation {
public $CI;
/**
* Is Unique
*
* Check if the input value doesn't already exist
* in the specified database field.
*
* @param string $str
* @param string $field
* @return bool
*/
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
//return isset($this->CI->db)
return is_object($this->CI->db)
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
}
这将覆盖 form_validation class
的 is_unique 方法