I’m trying to figure out how to run an if statement with this as the returned JSON data. When there is an error I want it to find out if its the username or password input that has the error and append a label with a class of error the actual error. So the if statement below needs to find out if its the data error is a username or not and then place in the error instead of error.
{"output_status":"Error","output_title":"Form Not Validated","output_message":"The form did not validate successfully!","error_messages":{"username":"This is not have an accepted value!"}}
if (data.output_status == 'Error')
{
if (data.?)
{
$('#username').after('<label class="error">error</label>');
}
}
EDIT:
I”m not sure what is going on but now I’m getting a notice the the form didn’t get submitted for some reason.
$('#login_form :input:visible').each(function() {
var name = $(this).attr('name');
if (data.error_messages.name)
{
$(this).after('<label class="error">' + data.error_messages.name + '</label>');
}
});
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, 'error_messages' => $this->form_validation->error_array()));
}
public function check_username($str)
{
if (preg_match('#[a-z0-9]#', $str))
{
return TRUE;
}
$this->form_validation->set_message('check_username', 'This is not have an accepted value!');
return FALSE;
}
Try:
Now as a bonus, you can iterate over all your input fields and do the same: