在laravel中验证bootstrap模式

时间:2018-04-09 05:10:11

标签: php laravel bootstrap-modal

我正在使用laravel而且我有自举模式我想成为必需的字段。我在输入标签和textarea中尝试了这个required并且它不起作用。我试图在控制器的字段中验证,但它不会工作,我知道在做什么有什么不对。我只是初学者在laravel。帮助我

这是我的模态

<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" 
aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
    <div class="modal-header info-color white-text">
        <h4 class="title">
            <i class="fa fa-pencil"></i> New Message</h4>
        <button type="button" class="close waves-effect waves-light" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
        </button>
    </div>
    <div class="modal-body">
      <label for="defaultFormEmailModalEx">Recipient</label>
      <input type="email" name="recipient" id="defaultFormEmailModalEx" class="form-control form-control-sm"  autocomplete="off" v-model="recipient">
      <br>
      <label for="defaultFormMessageModalEx">Your message</label>
      <textarea type="text" name="newmessage" id="defaultFormMessageModalEx" class="md-textarea form-control" v-model="newMessage"></textarea>
      <div class="text-center mt-4 mb-2">
          <button class="btn btn-info" @click="sendNewMessage">Send
              <i class="fa fa-send ml-2"></i>
          </button>
      </div>
    </div>
  </div>
</div>

这是@click="sendNewMessage"做什么

sendNewMessage(){
  axios.post('http://localhost/sendnewmsg', {
        recipient: this.recipient,
        newMessage: this.newMessage
      })
      .then( (response) => {
        console.log(response.data);
        /*if(response.status===200){
          app.messages = response.data;
          app.privateMsg = response.data;
        }*/

      })
      .catch(function (error) {
        console.log(error);
      });
  this.recipient='';
  this.newMessage='';
},

这是控制器,我认为这里必须进行验证

public function sendnewmessage(Request $request){
  $recipient = $request->recipient;
  $newmessage = $request->newMessage;

  $checkUser = DB::table('users')->where('email','=', $recipient)->get();
  if($checkUser->isEmpty()){
    echo 'data doesnt exist';
  }
  else{
    echo 'data exist';
  }
}

2 个答案:

答案 0 :(得分:0)

您可以在 sendnewmessage 功能中传递manual validators, 请检查以下代码。

public function sendnewmessage(Request $request){

$validator = \Validator::make($request->all(), [
                'recipient' => 'required',
                'newMessage' => 'required',
             ]);

if ($validator->fails())
{
    return response()->json(['errors'=>$validator->errors()->all()]);
}

$recipient = $request->recipient;
$newmessage = $request->newMessage;

$checkUser = DB::table('users')->where('email','=', $recipient)->get();
if($checkUser->isEmpty()){
    echo 'data doesnt exist';
}
else {
    echo 'data exist';
}
}

如果您想在模态中显示错误,可以在模态中写下

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

希望如果您有任何疑问,这将有助于您发表评论。

答案 1 :(得分:0)

这是你想要的......

$this->validate($request, [
    'recipient' => 'required',
    'newmessage' => 'required',
]);

Here is validate document for laravel

要在js中显示错误消息,您可以获取错误并将其存储在变量

let $errors = response.responseJSON;