【发布时间】:2014-04-16 23:23:55
【问题描述】:
所以我用 CodeIgniter 制作了一个表单,它确实使用了 CodeIgniter 的表单验证 类来验证输入字段。我总共有 6 个输入字段,它们都是 必填字段。
我什至想显示“此字段为必填项”的相同错误 如果有多个空字段,但现在它会为每个字段显示错误消息 的领域。因此,如果我将每个字段都留空,它将重复相同的消息 6 次。
我不会在这里粘贴任何代码,因为我知道如何使用实际的验证类,只是 不知道如何以上述方式实现它。
提前致谢!
E: 现在又来了一个问题,它没有显示任何错误,实际上整个方法在检查“add_user”按钮是否提交后就会失败。我 100% 确定我的提交按钮的名称是“add_user”。
抱歉缩进不好,我永远也学不会如何使用 StackOverflow 的缩进。
这是我添加用户的控制器:
<?php
class Users extends CI_Controller {
public function add_user()
{
if($this->input->post('add_user'))
{
$this->load->model('subject');
$data['subjects'] = $this->subject->get_all_subjects();
$data['schools'] = $this->subject->get_all_schools();
$this->load->library('form_validation');
$this->form_validation->set_rules('f_name', 'Etunimi', 'required');
$this->form_validation->set_rules('l_name', 'Sukunimi', 'min_length[3]');
$this->form_validation->set_rules('email', 'Sähköpostiosoite', 'matches[email_confirm]|is_unique[users.email]|valid_email');
$this->form_validation->set_rules('email_confirm', 'Sähköpostiosoite', 'valid_email');
$this->form_validation->set_rules('phone_number', 'Puhelinnumero', 'min_length[7]|max_length[10]');
if($this->input->post('user_type') == "moderator")
{
$this->load->library('form_validation');
$this->form_validation->set_rules('school', 'Koulutalo', 'required');
$this->form_validation->set_rules('subject', 'Laji', 'required');
}
$this->form_validation->set_message('required', 'Täyttämättömiä kenttiä');
$this->form_validation->set_message('min_length', '%s - kentässä on liian vähän merkkejä');
$this->form_validation->set_message('matches', 'Sähköpostit eivät täsmää');
$this->form_validation->set_message('is_unique', '%s on jo rekisteröity');
if($this->form_validation->run() == FALSE)
{
$this->output->set_content_type('application_json');
$this->output->set_output(json_encode(array('result' => 0, 'error' => $this->form_validation->error_array() )));
return false;
}
$first_part_username = substr($this->input->post('f_name'), 0, 2);
$second_part_username = substr($this->input->post('l_name'), 0, 3);
$first_part_username = strtolower($first_part_username);
$second_part_username = strtolower($second_part_username);
$this->load->helper('string');
$random_number = random_string('numeric', 4);
$username = $first_part_username . $second_part_username .$random_number;
$password = random_string('alnum', 8);
$this->load->model('user');
$name = ucfirst($this->input->post('f_name')). " " .ucfirst($this->input->post('l_name'));
$data = array (
'name' => $name,
'email' => $this->input->post('email'),
'username' => $username,
'password' => $this->phpass->hash($password),
'user_type' => $this->input->post('user_type'),
'phone_number' => $this->input->post('phone_number'),
'subject_id' => !($this->input->post('subject')) ? null : $this->input->post('subject'),
'school_id' => !($this->input->post('school')) ? null : $this->input->post('school')
);
$this->user->add_user($data);
$this->load->library('email');
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from('your@example.com', 'Your Name');
$this->email->to($this->input->post('email'));
$this->email->subject('test test');
$this->email->message('Test');
if($this->email->send()) {
$this->output->set_content_type('application_json');
$this->output->set_output(json_encode(array('result'=>1, 'msg' => 'Uusi käyttäjä lisätty')));
}
}
/* It will echo out this one which is viewable from developer tab right after the form is submitted */
$this->output->set_content_type('application_json');
$this->output->set_output(json_encode(array('result'=>1, 'msg' => 'Form not submitted')));
}
}
?>
还有 javascript:
$(document).ready(function() {
$("#add_user_form").on('submit', function(e) {
e.preventDefault();
$("#loading_spinner").show();
var from = $(this);
$.ajax({
url: from.attr('action'),
type: from.attr('method'),
data: $(from).serialize(),
}).done(function(data) {
if(data.result == 0) {
$("#forgot-pass-success").hide();
$("#forgot-pass-error").show();
$("#forgot-pass-error").fadeIn(1000).html("<p>" + data.error + "</p>");
}
if(data.result == 1) {
$("#forgot-pass-error").hide();
$("#forgot-pass-success").show();
$("#forgot-pass-success").fadeIn(1000).html("<p>" + data.msg + "</p>");
}
$("#loading_spinner").hide();
}, 'json');
return false;
});
});
【问题讨论】:
-
您必须在您的 javascript 中循环从 PHP 发送的数组以显示每个错误 btw。我还在看你的代码。
-
$this->load->library('form_validation');只需在该函数中加载一次即可为整个函数工作。所以你可以删除重复项。
-
完成了这些。现在它每次都会将“表单未提交”作为 JSON 回显。所以基本上 AJAX 部分在获取这些 JSON 时是有效的。
-
是的,现在改变这个 $this->output->set_output(json_encode(array('result' => 0, 'error' => $this->form_validation->error_array() ))) ; TO $this->output->set_output(json_encode(array('result'=>1, 'msg' => $this->form_validation->error_array())));所以它符合你的方案
-
您对 JSON 的看法是正确的,添加的 JSON 需要更改为 msg,以便我们可以在您的输出中看到它。
标签: php forms codeigniter validation