I'm using Codeigniter framework to create my web application.
I have a login form for the users to input their username and password as well as a remember me checkbox. After the user submits the form it uses jQuery ajax to submit the form and send the post data to the login controller submit function for data checking and manipulation. After the submit function is ran a JSON array is returned if the user was logged in. If the user is successfully logged in he/she is sent to the dashboard and if they weren't logged in successfully then an error message is displayed and under each of the user inputs displays the corresponding message to the rule that didn't pass the CI form validation rules.
As of right now it displays a message saying it didn't pass validation but I'm having an issue with figuring out how to display the validation rule that didn't pass under the user input box.
<div style="display:none" id="message_container"><button type="button" class="close" data-dismiss="alert">×</button></div>
<div class="login_container">
<?php $attributes = array('id' => 'login_form', 'class' => 'form-horizontal'); ?>
<?php echo form_open('login/submit', $attributes); ?>
<div class="form-row row-fluid">
<div class="spa12">
<div class="row-fluid">
<?php $attributes = array('class' => 'form_label span12'); ?>
<?php echo form_label('Username<span class="icon16 icomoon-icon-user-3 right gray marginR10"></span>', 'username', $attributes); ?>
<?php $attributes = array('name' => 'username', 'id' => 'username', 'value' => '', 'class' => 'span12 text valid'); ?>
<?php echo form_input($attributes); ?>
</div>
</div>
</div>
<div class="form-row row-fluid">
<div class="span12">
<div class="row-fluid">
<?php $attributes = array('class' => 'form_label span12'); ?>
<?php echo form_label('Password<span class="icon16 icomoon-icon-locked right gray marginR10"></span><span class="forgot"><a href="#">Forgot your password?</a></span>', 'password', $attributes); ?>
<?php $attributes = array('name' => 'password', 'id' => 'password', 'value' => '', 'class' => 'span12 password'); ?>
<?php echo form_password($attributes); ?>
</div>
</div>
</div>
<div class="form-row row-fluid">
<div class="span12">
<div class="row-fluid">
<div class="form-actions">
<div class="span12 controls">
<input type="checkbox" id="keepLoged" value="Value" class="styled" name="logged" />Keep me logged in
<button type="submit" class="btn btn-info right" id="loginBtn">
<span class="icon16 icomoon-icon-enter white"></span>
Login
</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
public function submit()
{
$output_status = 'Notice';
$output_title = 'Not Processed';
$output_message = 'The request was unprocessed!';
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer');
if ($this->form_validation->run() == TRUE)
{
}
else
{
$output_status = 'Error';
$output_title = 'Form Not Validated';
$output_message = 'The form did not validate successfully!';
}
echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message));
}
public function check_username($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-z]#', $str))
{
return TRUE;
}
$this->form_validation->set_message('check_username', 'This is not have an accepted value!');
return FALSE;
}
EDIT:
"error_messages":{"username":"This is not have an accepted value!"}}
Since I'm submitting this form via jQuery ajax function I'm trying to figure out how I'm going to make my if statement. Take into consideration I'll have multiple inputs that will have to be placed under each text field unless there's a better idea.