我有这个上传表格,
这是视图
<div class="box-body">
<div class="form-group">
<label for="userfile" class="col-sm-2 control-label col-sm-offset-2">File<span>*</span></label>
<div class="col-sm-5">
<input type="file" class="form-control" id="userfile" name="userfile" >
</div>
</div>
<div class="form-group has-error col-sm-offset-7">
<label class="form-group has-error control-label" for="error"><?php echo form_error("userfile"); ?></label>
</div>
</div>
*other input type='text' divs
这是控制器
public function do_upload(){
$this->load->helper(array('form','url'));
$config['upload_path'] = './uploads/pdf';
$config['allowed_types'] = 'pdf';
$config['max_size'] = 0;
$this->load->library('upload', $config);
$this->form_validation->set_rules('userfile', 'Document' ,'callback_handle_upload');
$this->form_validation->set_rules('DOC_NAME', 'Document Name' ,'trim|required');
$this->form_validation->set_rules('DOC_TYPE', 'Document Type' ,'trim|required');
$this->form_validation->set_rules('DOC_DATE', 'Date' ,'trim|required');
$file_data = $this->upload->data();
$url = base_url().'uploads/pdf/'.$file_data['file_name'];
if($this->form_validation->run($this)){
if($this->upload->do_upload()){//upload function
$this->PDFuploads_Model->insert($url);
}
}else{
$this->add_view();
}
}
,回电是
public function handle_upload()
{
if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name']))
{
if ($this->upload->do_upload('userfile'))
{
// set a $_POST value for 'userfile' that we can use later
$upload_data = $this->upload->data();
$_POST['userfile'] = $upload_data['file_name'];
return true;
}
else
{
// possibly do some clean up ... then throw an error
$this->form_validation->set_message('handle_upload', $this->upload->display_errors());
return false;
}
}
else
{
// throw an error because nothing was uploaded
$this->form_validation->set_message('handle_upload', "You must upload a document!");
return false;
}
}
所有这些工作都非常精细。
现在,我有一个用于表单验证的ajax函数。但当我对此所有表单验证工作进行ajax验证并显示错误,除了上传表单验证 ..所以我假设表单验证或回调方法是问题,我已将此MY_Form_validation
库放在我的库中。
这是当我把它放在ajax验证时控制器的样子
public function do_upload(){
$this->load->helper(array('form','url'));
$config['upload_path'] = './uploads/pdf';
$config['allowed_types'] = 'pdf';
$config['max_size'] = 0;
$this->load->library('upload', $config);
$data = array('success' => false, 'messages' => array());
$this->form_validation->set_rules('userfile', 'Document' ,'callback_handle_upload');
$this->form_validation->set_rules('DOC_NAME', 'Document Name' ,'trim|required');
$this->form_validation->set_rules('DOC_TYPE', 'Document Type' ,'trim|required');
$this->form_validation->set_rules('DOC_DATE', 'Date' ,'trim|required');
$this->form_validation->set_error_delimiters('<p class="text-danger"','</p>');
$file_data = $this->upload->data();
$url = base_url().'uploads/pdf/'.$file_data['file_name'];
if($this->form_validation->run($this)){
if($this->upload->do_upload()){//upload function
$this->PDFuploads_Model->insert($url);
}
$data['success'] = true;
}else{
foreach ($_POST as $key => $value) {
# code...
$data['messages'][$key] = form_error($key);
}
}
echo json_encode($data);
}
这里也是ajax
$('#form-user').submit(function(e) {
e.preventDefault();
var me = $(this);
// perform ajax
$.ajax({
url: me.attr('action'),
type: 'post',
data: me.serialize(),
dataType: 'json',
success: function(response) {
if (response.success == true) {
// if success we would show message
// and also remove the error class
$('#the-message').append('<div class="alert alert-success">' +
'<span class="glyphicon glyphicon-ok"></span>' +
' Data has been saved' +
'</div>');
$('.form-group').removeClass('has-error')
.removeClass('has-success');
$('.text-danger').remove();
// reset the form
me[0].reset();
// close the message after seconds
$('.alert-success').delay(200).show(10, function() {
$(this).delay(200).hide(10, function() {
$(this).remove();
});
})
}
else {
$.each(response.messages, function(key, value) {
var element = $('#' + key);
element.closest('div.form-group')
.removeClass('has-error')
.addClass(value.length > 0 ? 'has-error' : '')
.find('.text-danger')
.remove();
element.after(value);
});
}
}
});
});
为什么仅上传文件的表单验证不会显示在ajax上