Answer the question
In order to leave comments, you need to log in
Why is form_validation not working?
Hello. I use the codeigniter framework and its built-in form validation class. I also use the jqueryvalidation.org library on the frontend side. Without this library, validation on the codeginiter side works, but not with it.
Ajax send code example:
$('#button-l-p-auth').click(function() {
$.ajax({
url: '/auth/auth_user',
dataType: "json",
type: 'POST',
data: $('form-l-p-auth').serialize(),
success: function (response) {
if (response.status == 'error') {
$('#form-l-p-auth-response').empty();
$('#form-l-p-auth-response').append(response.message);
} else if(response.status == 'success') {
window.location.href = response.redirect;
}
}
});
});
$(document).ready(function () {
$("#form-l-p-auth").validate({
rules: {
email: {
required: true,
minlength: 4
},
password: {
required: true,
minlength: 6
},
},
messages: {
email: {
required: "This field can't be blank.",
minlength: "Please enter a valid email address."
},
password: {
required: "This field can't be blank.",
minlength: "Password has to be 6 symbols min."
},
}
});
});
public function auth_user()
{
if ($this->form_validation->run()) {
$email = $this->input->post('email');
$password = $this->input->post('password');
$data_user = $this->auth_model->get_data_user($email);
if ($data_user->num_rows() > 0) {
$data_user = $data_user->row();
$hash_pass_tmp = crypt($password, $data_user->hash_pass);
if ($data_user->hash_pass == $hash_pass_tmp) {
$auth_data = array(
'id' => $data_user->id,
'email' => $data_user->email,
'logged' => TRUE
);
$this->session->set_userdata($auth_data);
$data = array(
'status' => 'success',
'redirect' => base_url('apps')
);
print(json_encode($data));
} else {
$data = array(
'status' => 'error',
'message' => 'Wrong password'
);
print(json_encode($data));
}
} else {
$data = array(
'status' => 'error',
'message' => 'Unrecognised email'
);
print(json_encode($data));
}
} else {
$data = array(
'status' => 'error',
'message' => 'This field can\'t be blank.'
);
print(json_encode($data));
}
}
'auth/auth_user' => array(
array(
'field' => 'password',
'label' => 'Password user',
'rules' => 'trim|required|min_length[6]'
),
array(
'field' => 'email',
'label' => 'Email user',
'rules' => 'trim|required|valid_email'
)
),
<form class="form-horizontal" id="form-l-p-auth" method="post" accept-charset="utf-8">
<div class="form-group">
<div class="col-lg-12">
<input type="email" class="form-control" id="email" name="email"
placeholder="Email">
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<input type="password" class="form-control" id="password" name="password"
placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-lg-12 form-l-p-auth">
<span id="form-l-p-auth-response"></span>
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<button type="button" class="btn btn-success" id="button-l-p-auth">Submit</button>
</div>
</div>
</form>
Answer the question
In order to leave comments, you need to log in
Question removed. I forgot to add # to form id when sending ajax request.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question