我在学校有一个项目,我是codeigniter框架的初学者。现在我在我的codeigniter中使用表单验证库。我怎么能这样做:(我的表单中的PER INPUT TYPE)输入类型及以下是来自codeigniter表单验证的错误消息。现在这里是我的代码,它复制所有错误消息,并将发布每个输入类型。
例如,如果我将中间名和名字输入类型留空,我的代码会将每个输入类型的错误消息放在输入类型下面。
这是我的代码: VIEW:
<div class="modal fade large" id="admin_create_acct" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header btn-success">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title">Create Account</h3>
</div>
<div class="modal-body">
<form class="form-horizontal" id="frm_create_user">
<div class="form-group">
<label for="userFName" class="control-label col-sm-4">First Name:</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="userFName" id="userFName" placeholder="First name">
<div class="msg"></div><!-- form-group -->
</div><!-- col-sm-6 -->
</div><!-- form-group -->
<div class="form-group">
<label for="userMName" class="control-label col-sm-4">Middle Name:</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="userMName" id="userMName" placeholder="Middle name">
<div class="msg"></div><!-- form-group -->
</div><!-- col-sm-6 -->
</div><!-- form-group -->
</form><!-- form-horizontal -->
</div><!-- modal-body -->
<div class="modal-footer">
<button type="button" id="btn_reg" onclick="create_User()" class="btn btn-primary">Create Account</button>
这是我的控制器,我按照输入类型设置规则:
public function reg_user(){
$this->form_validation->set_rules('userFName', 'userFName', 'trim|required',
array('required' => 'Error Message on First name ')
);
$this->form_validation->set_rules('userMName', 'userMName', 'trim|required',
array('required' => 'Error Message on Middle Name')
);
if ($this->form_validation->run() == FALSE) {
$result['type'] = 'error';
$result['message'] = validation_errors();
}else {
$result['type'] = 'success';
$result['message'] = 'Whooos..! Your Succes Message';
}
echo json_encode($result);
}
我的ajax代码:
function create_User() {
$.ajax({
url: siteurl+"admin_controls/reg_user",
type: "POST",
data: $("#frm_create_user").serialize(),
dataType: "JSON",
success: function(data) {
if (data.type == "error") {
$('.msg').html(data.message);
}else{
// You can Sow this or Alert this
$('.msg').html(data.message);
alert('succcess fullyInserted')
}
$("#frm_create_user")[0].reset();
}
});
}
答案 0 :(得分:1)
根据CI手册,我提出以下建议。
您需要单独显示每条错误消息,这就是$result['message']
中您需要某些结构的原因:
$result['message'] = [
// fieldName => error text
'userFName' => 'error text',
'userMName' => 'another error text',
];
我想使用form_error
功能。因此,如果有错误,您需要检查每个字段:
if ($this->form_validation->run() == FALSE) {
$result['type'] = 'error';
// init message with empty array
$result['message'] = [];
if (!empty(form_error('userFName'))) {
$result['message']['userFName'] = form_error('userFName');
}
if (!empty(form_error('userMName'))) {
$result['message']['userMName'] = form_error('userMName');
}
// sure you can simplify this somehow
} else {
$result['type'] = 'success';
$result['message'] = 'Whooos..! Your Succes Message';
}
接下来,我们检查客户端的响应:
if (data.type == "error") {
// here we iterate over `data.message`
$.each(data.message, function( key, value ) {
// just for test
console.log(key, value);
// now we need to find element where to put error text
// according to your current markup -
// error field is next to input with a certain id:
$("#" + key).next().text( value );
// As we have `key` as `userFName` then
// input with id `userFName` will be found
// and error text will be put into a div next to it
});
} else { /* do other stuff */ }
当然,还有其他方法,我找到了这个。