我试图弄清楚如何使用这个作为返回的 JSON 数据运行 if 语句。当出现错误时,我希望它找出它的用户名或密码输入是否有错误,并附加一个带有错误类别的标签,即实际错误。所以下面的 if 语句需要判断它的数据错误是否是用户名,然后放入错误而不是错误。
{"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>');
}
}
编辑:
我不确定发生了什么,但现在我收到通知,表格由于某种原因没有提交。
$('#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;
}