所以我用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;
});
});
答案 0 :(得分:0)
我可以尝试使用个别错误。
在您看来:
<?php
$form_errors = array(
form_error('field_1'),
form_error('field_2'),
form_error('field_3'),
form_error('field_4'),
form_error('field_5'),
form_error('field_6')
);
$error = FALSE;
foreach ($form_errors as $form_error)
{
if ( ! empty($form_error))
$error = TRUE;
}
if ($error)
echo "All fields are required".
?>
所以基本上你在这里做的是填充一系列单独的表单错误。如果根据验证规则正确填写字段,则form_error()
返回空字符串,否则将是错误消息。
要确认所有内容都已正确填充,您需要查找一个空字符串数组。如果未正确填写字段,则数组将包含一个包含错误消息的元素。
答案 1 :(得分:0)
这是一个返回JSON字符串中的错误以使用jQuery处理的示例,您必须加载表单验证帮助程序。
加载库:
$this->load->library('form_validation');
/* Outputto Json Format */
$this->output->set_content_type('application_json');
/* Validate Form Data */
$this->form_validation->set_rules('login', 'Username', 'required|min_length[4]|max_length[16]|is_unique[user.login]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password', 'Password', 'required|min_lenght[6]|max_length[16]|matches[confirm_password]');
/* Custom Messages */
$this->form_validation->set_message('required', 'You must enter a %s.');
$this->form_validation->set_message('is_unique', 'That %s is already in use, please try again.');
$this->form_validation->set_message('valid_email', 'You must enter a vaild email address.');
if($this->form_validation->run() == false)
{
$this->output->set_output(json_encode(['result' => 0, 'error' => $this->form_validation->error_array()]));
return false;
}
$login = $this->input->post('login');
$email = $this->input->post('email');
$password = $this->input->post('password');
$confirm_password = $this->input->post('comfirm_password');
$user_id = // DO YOUR QUERY HERE
if($user_id)
{
$this->session->set_userdata(['user_id' => $user_id]);
$this->output->set_output(json_encode(['result' => 1]));
return false;
}
$this->output->set_output(json_encode(['result' => 0, 'error' => 'User not created.']));
编辑: 抱歉,我正在使用自定义库来扩展form_validation,因此我可以创建要返回的数组。只需确保将其作为my_form_validation.php
保存在库文件中
class MY_Form_validation extends CI_Form_validation
{
// ------------------------------------------------------------------------
public function __construct($config = array())
{
parent::__construct($config);
}
// ------------------------------------------------------------------------
public function error_array()
{
if(count($this->_error_array > 0))
{
return $this->_error_array;
}
}
// ------------------------------------------------------------------------
}
编辑:在jQuery中处理此数据的示例
<!-- Javascript POST -->
<script type="text/javascript">
$(function() {
// You need to create a div called #register_form_error
$("#register_form_error").hide();
// Your register form would go here in place of #regiser_form
$("#register_form").submit(function(evt) {
evt.preventDefault();
var url = $(this).attr('action');
var postData = $(this).serialize();
$.post(url, postData, function(o) {
if (o.result == 1) {
// If the registration was successful where to go.
window.location.href = '<?=site_url('where to go')?>';
} else {
// This is where all of the form errors will be displayed
$("#register_form_error").show();
var output = '<ul>';
for (var key in o.error) {
var value = o.error[key];
output += '<li>' + value + '</li>';
}
output += '</ul>';
$("#register_form_error").html(output);
}
}, 'json');
});
});
</script>
最后:
if($this->input->post('add_user'))
Shoud be:
if($this->input->post('$_POST'))
这样你就得到了整个帖子阵列。