So I have made a form with CodeIgniter and it does uses CodeIgniter's form validation class to validate input fields. I have a total of 6 input fields which all are required fields.
I would like to show the same error of "This field is required" even if there are multiple empty fields, but right now it will show the error message for each of the fields. So if I leave every field empty, it will echo the same message 6 times.
I won't paste any code here since I know how to use the actual validation class, just don't know how to implement it in a way as said above.
Thanks in advance!
E: Now comes another problem, it doesn't show any errors, in fact the whole method will fail right after it's checking if the "add_user" button is submitted. I'm 100% sure that the name of my submit button is "add_user".
Sorry about bad indenting, I'll never learn how to use StackOverflow's indenting.
Here's my controller for adding a user:
<?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('[email protected]', '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')));
}
}
?>
And the 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;
});
});