我目前正在休假申请系统上。我有一个下拉字段,我要验证如果api返回false,则用户无法提交应用程序。
例如,如果没有年假(getLeaveAvailability return false)应返回错误消息。
表格:
<div class="form-group">
<label class="control-label col-md-3">Leave Type</label>
<div class="col-md-9">
<select type="text" id="leave_type" name="leave_type" required="required" class="leave_type form-control col-md-7 col-xs-12">
<option value="">Select Leave Type</option>
<option value="1">Annual Leave</option>
<option value="2">Medical Leave</option>
</select>
</div>
</div>
jQuery Ajax验证:
var validator = form.validate({
debug: true,
rules: {
leave_type: {
required: true,
remote: {
url: base_url + "/getLeaveAvailability",
type: 'post',
dataType: 'json',
data: {
leave_id: function() {
return $('[name="leave_type"]').val();
}
}
},
// leave_type: true
},
},
messages: {
leave_type: {
remote: "Leave not available!"
}
},
API响应:
public function getLeaveAvailability(Request $request){
return 'false';
}
答案 0 :(得分:0)
我认为您可能会通过以下代码获得帮助,
leave_type: {
required: true,
remote: {
url: base_url + "/getLeaveAvailability",
type: 'post',
dataType: 'json',
data: {
leave_id: function() {
return $('[name="leave_type"]').val();
}
},
success: function(response) {
if (response.responseText == "true") { //here you have to check the return response from server side
//Write statement if success
} else {
//Write statement if failed
}
}
}
}