withErr..."/>
您好,登錄后才能下訂單哦!
這篇文章主要介紹laravel Validator ajax返回錯(cuò)誤信息怎么辦,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
將return back()->withErrors($validator->errors())->withInput();替換為下面的代碼
$validator = \Validator::make($request->input(), $rules, $messages); if ($validator->fails()) { //將返回錯(cuò)誤循環(huán)組裝成字符串 $arr = []; foreach ($validator->getMessageBag()->toArray() as $k=>$error){ array_push($arr, "<li>".$error[0]."</li>"); } $str = implode(' ', $arr); return \Response::json([ 'success' => false, 'errors' => $str ]); }
然后直接在ajax輸出相應(yīng)的錯(cuò)誤就行了
<div class="alert alert-danger alert-dismissible fade in" id="yc"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> <ul class="margin-bottom-none padding-left-lg" id="li1"> </ul> </div>
$.ajax({ method: 'post', data: form, dataType:'json', url: $('#url').val(), success: function (data) { //console.log(data); if (data.code == 0){ $('#textarea').html(JSON.stringify(data.resp, null, "\t")).css("display","block"); } else if(data.code == 1) { layer.msg("失敗"+data.msg,{icon:2}) }else if (data.success == false) { if (data.errors){ $('#yc').css('display','block'); $('#li1').html(data.errors); } } } })
以上是“l(fā)aravel Validator ajax返回錯(cuò)誤信息怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。