我正在为使用laravel的学生制作一个小型投票门户。我打算做的是,我希望他们能够从我获得了api的其他应用程序中输入ID。现在,我希望能够在发布之前输入这些ID时对其进行验证。因此,如果该ID不存在,则应返回false。
下面是我的代码 -这是我的控制器
public function postVote(Request $request)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://3.16.139.101:8080/enshika/api/validate/enshikaId.json",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
print_r(json_decode($response));
}
$this->validate($request,[
'enshika_id' => 'required|unique:votes|'],
['enshika_id.required'=>'You need to enter your Enshika ID',
'enshika_id.unique'=>"The ID $request->enshika_id already exist!"]
);
if($request->ajax())
{
return response()->json(Vote::create($request->all()));
}else return back()->with(['error'=>'']);
}
-路线
Route::post('/post-vote', ['as' => 'postVote', 'uses' => 'VoteController@postVote']);
-用于发布数据的Ajax
$('.btn-vote').on('click', function (e) {
e.preventDefault();
var data = $('#frm-create-vote').serialize();
$.post("{{route('postVote')}}", data, function (data) {
$('#vote-show').modal('hide');
swal('Great!',
'You have voted successfully',
'success');
$(this).trigger('reset');
}).error(function (data,status,error) {
console.log(data);
var response = $.parseJSON(data.responseText)
$.each(response.errors, function(key, value){
swal({
title: "ooops!",
text: value,
icon: "error",
color: "#FEFAE3",
button: "OK",
});
});
});
});
-表格
<form id="frm-create-vote" class="form-horizontal" action="">
<input type="hidden" name="cont_id" id="cont_id_edit">
<input type="hidden" name="fullname" id="fullname_edit">
<div class="modal-body">
<div class="form-group">
<div class="col-lg-2">
<label for="class_name">Enshika ID</label>
</div>
<div class="col-lg-8">
<input type="text" class="form-control" name="enshika_id">
</div>
</div>
<div class="form-group">
<div class="col-lg-12">
<label for="class_name" style="color: #ff2121">Dont have Enshika ID. Click on the links to download and sign up now for your ENSHIKA ID! </label><br>
<a href="https://play.google.com/store/apps/details?id=com.enshika.passenger" target="_blank" class="logo"><img src="assets/img/playstore.png"
height="120px" width="250px"></a><br>
<a href="https://itunes.apple.com/gh/app/enshika/id1276549309?mt=8" target="_blank" class="logo"><img src="assets/img/appstore.png"
height="80px" width="230px"></a>
</div>
</div>
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
<button class="btn btn-success btn-vote" type="button">Submit</button>
</div>
</form>
我想验证enshika_id字段。要检查输入是否存在于提供给我的外部api中。 请帮助我