回调表单验证在 HMVC 中不起作用
Callback form validation is not working in HMVC
我在使用回调进行表单验证时遇到问题。
在创建新的费用类型之前,验证应该检查 tbl_fees_type 是否存在现有的 fees_type_name,如果它已经存在,它将显示错误费用类型已经存在。
我知道所需的表单验证有效,因为它表明它是必需的,但是当涉及到回调验证时,如果它存在,则使用回调检查数据库中的信息,但它不起作用。
这是我的代码:
所以我有一个这样的 Feestype 控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Feestype extends MX_Controller {
public function __construct() {
parent::__construct();
// loading the fees type model
$this->load->model('model_feestype');
// loading the form validation library
$this->load->library('form_validation');
}
public function index() {
$this->load->view('feestype');
}
public function create()
{
$validator = array('success' => false, 'messages' => array());
$validate_data = array(
array(
'field' => 'fees_type_name',
'label' => 'Fees Type Name',
'rules' => 'trim|required|callback_validate_feestypename'
//this is the callback
),
array(
'field' => 'fees_type_role',
'label' => 'Fees Type Role',
'rules' => 'trim|required|'
)
);
$this->form_validation->set_rules($validate_data);
$this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');
if($this->form_validation->run() === true) {
$create = $this->model_feestype->create();
if($create === true) {
$validator['success'] = true;
$validator['messages'] = "Successfully added";
}
else {
$validator['success'] = false;
$validator['messages'] = "Error while inserting the information into the database";
}
}
else {
$validator['success'] = false;
foreach ($_POST as $key => $value) {
$validator['messages'][$key] = form_error($key);
}
} // /else
echo json_encode($validator);
}
// call back validation function to do
public function validate_feestypename()
{
$validate = $this->model_feestype->validate_feestypename();
if($validate === true) {
$this->form_validation->set_message('validate_feestypename', 'The {field} already exists');
return false;
}
else {
return true;
}
}
}
?>
这是 model_feestype.php 型号
<?php if (!defined ('BASEPATH')) exit ('No direct script access allowed');
class Model_Feestype extends CI_Model {
public function __construct() {
parent:: __construct();
}
public function create()
{
$insert_data = array(
'fees_type_name' => $this->input->post('fees_type_name'),
'fees_type_role' => $this->input->post('fees_type_role')
);
$status = $this->db->insert('tbl_fees_type', $insert_data);
return ($status === true ? true : false);
}
public function validate_feestypename()
{
$feestypeName = $this->input->post('fees_type_name');
$sql = "SELECT * FROM tbl_fees_type WHERE fees_type_name = ?";
$query = $this->db->query($sql, array($feestypeName));
return ($query->num_rows() == 1 ? true : false);
}
}
?>
这是我的模态表单视图 php 文件。
<div class="modal fade" tabindex="-1" role="dialog" id="addFeetype">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Add Fees Type</h4>
</div>
<form method="post" action="feestype/create" id="createFeetypeForm">
<div class="modal-body">
<div class="form-group">
<label for="fees_type_name">Fees Type Name</label>
<input type="text" class="form-control" id="fees_type_name" name="fees_type_name" placeholder="Fees Type Name">
</div>
<div class="form-group">
<label for="fees_type_name">Fees Type Role</label>
<select class="form-control" name="fees_type_role" id="fees_type_role">
<option></option>
<option>School Fees</option>
<option>Personal Fees</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
需要验证的表单正在运行,如下所示。
Form validation required working
这是我想要实现的示例表单,它具有相同的源代码,但它在 codeigniter(不是 HMVC)中运行,但它在我的工作(HMVC)中不起作用。
Woking callback validation should look like this
在文件夹库中创建 class MY_Form_validation
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation{
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
// call back validation function to do
public function validate_feestypename()
{
$this->CI =& get_instance();
$this->CI->load->model('model_feestype');
$validate = $this->CI->model_feestype->validate_feestypename();
if($validate === true) {
$this->CI->form_validation->set_message->set_message('validate_feestypename', 'The {field} already exists');
return false;
}
else {
return true;
}
}
}
要使回调在 HMVC 中正常工作,您需要做的就是
MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation {
function run($module = '', $group = '') {
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
然后在运行部分添加$this
在run($this)
$this->form_validation->run($this)
我在使用回调进行表单验证时遇到问题。 在创建新的费用类型之前,验证应该检查 tbl_fees_type 是否存在现有的 fees_type_name,如果它已经存在,它将显示错误费用类型已经存在。
我知道所需的表单验证有效,因为它表明它是必需的,但是当涉及到回调验证时,如果它存在,则使用回调检查数据库中的信息,但它不起作用。
这是我的代码:
所以我有一个这样的 Feestype 控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Feestype extends MX_Controller {
public function __construct() {
parent::__construct();
// loading the fees type model
$this->load->model('model_feestype');
// loading the form validation library
$this->load->library('form_validation');
}
public function index() {
$this->load->view('feestype');
}
public function create()
{
$validator = array('success' => false, 'messages' => array());
$validate_data = array(
array(
'field' => 'fees_type_name',
'label' => 'Fees Type Name',
'rules' => 'trim|required|callback_validate_feestypename'
//this is the callback
),
array(
'field' => 'fees_type_role',
'label' => 'Fees Type Role',
'rules' => 'trim|required|'
)
);
$this->form_validation->set_rules($validate_data);
$this->form_validation->set_error_delimiters('<p class="text-danger">','</p>');
if($this->form_validation->run() === true) {
$create = $this->model_feestype->create();
if($create === true) {
$validator['success'] = true;
$validator['messages'] = "Successfully added";
}
else {
$validator['success'] = false;
$validator['messages'] = "Error while inserting the information into the database";
}
}
else {
$validator['success'] = false;
foreach ($_POST as $key => $value) {
$validator['messages'][$key] = form_error($key);
}
} // /else
echo json_encode($validator);
}
// call back validation function to do
public function validate_feestypename()
{
$validate = $this->model_feestype->validate_feestypename();
if($validate === true) {
$this->form_validation->set_message('validate_feestypename', 'The {field} already exists');
return false;
}
else {
return true;
}
}
}
?>
这是 model_feestype.php 型号
<?php if (!defined ('BASEPATH')) exit ('No direct script access allowed');
class Model_Feestype extends CI_Model {
public function __construct() {
parent:: __construct();
}
public function create()
{
$insert_data = array(
'fees_type_name' => $this->input->post('fees_type_name'),
'fees_type_role' => $this->input->post('fees_type_role')
);
$status = $this->db->insert('tbl_fees_type', $insert_data);
return ($status === true ? true : false);
}
public function validate_feestypename()
{
$feestypeName = $this->input->post('fees_type_name');
$sql = "SELECT * FROM tbl_fees_type WHERE fees_type_name = ?";
$query = $this->db->query($sql, array($feestypeName));
return ($query->num_rows() == 1 ? true : false);
}
}
?>
这是我的模态表单视图 php 文件。
<div class="modal fade" tabindex="-1" role="dialog" id="addFeetype">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Add Fees Type</h4>
</div>
<form method="post" action="feestype/create" id="createFeetypeForm">
<div class="modal-body">
<div class="form-group">
<label for="fees_type_name">Fees Type Name</label>
<input type="text" class="form-control" id="fees_type_name" name="fees_type_name" placeholder="Fees Type Name">
</div>
<div class="form-group">
<label for="fees_type_name">Fees Type Role</label>
<select class="form-control" name="fees_type_role" id="fees_type_role">
<option></option>
<option>School Fees</option>
<option>Personal Fees</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
需要验证的表单正在运行,如下所示。 Form validation required working
这是我想要实现的示例表单,它具有相同的源代码,但它在 codeigniter(不是 HMVC)中运行,但它在我的工作(HMVC)中不起作用。
Woking callback validation should look like this
在文件夹库中创建 class MY_Form_validation
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation{
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
// call back validation function to do
public function validate_feestypename()
{
$this->CI =& get_instance();
$this->CI->load->model('model_feestype');
$validate = $this->CI->model_feestype->validate_feestypename();
if($validate === true) {
$this->CI->form_validation->set_message->set_message('validate_feestypename', 'The {field} already exists');
return false;
}
else {
return true;
}
}
}
要使回调在 HMVC 中正常工作,您需要做的就是
MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation {
function run($module = '', $group = '') {
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
然后在运行部分添加$this
在run($this)
$this->form_validation->run($this)