简体   繁体   English

Laravel Blade请求方法

[英]Laravel Blade Request method

Recently I started learning Laravel by following the Laravel From Scratch cast( https://laracasts.com/series/laravel-5-from-scratch ). 最近,我通过关注Laravel From Scratch演员( https://laracasts.com/series/laravel-5-from-scratch )开始学习Laravel。

Right now I'm trying to add additional functionality to the registration form(starting with error feedback) and I already ran into a problem. 现在,我正在尝试向注册表单添加其他功能(从错误反馈开始),我已经遇到了问题。

Is there a (native) way to check wether the form has been submitted or not? 是否有(本机)检查表单是否已提交的方法? I tried to use: 我尝试使用:

{{ Request::method() }}

But even after pressing Register on the default scaffold generated by running the command php artisan make:auth it returns GET while the action of the form is POST and it triggers a route with a POST Request type. 但是,即使在通过运行命令php artisan make:auth生成的默认支架上按了Register之后,它也将在表单的操作为POST的情况下返回GET并以POST Request类型触发路由。

The reason for all of this is that I want to add a CSS class to a element based on the following requirements. 所有这些的原因是我想根据以下要求将CSS类添加到元素。

if form is submitted
   if $errors->has('name') //is there an error for name(example)
      add 'has-error' class
   else
      add 'has-success' class
   endif
endif

Does anybody know a solution for it? 有人知道解决方案吗?

I think you want to achieve this: 我认为您想实现以下目标:

if( old('name') ){ // name has been submitted
  if( $errors->first('name') ){ // there is at least an error on it
    // add error class
  }else{ // has been submitted without errors
    // add valid class
  }
}

That in a input field is something like this: 在输入字段中是这样的:

<input name="name" class="validate{{ old('name') ? ( $errors->first('name') ? ' invalid'  : ' valid') : '' }}" type="text" value="{{ old('name') }}">

Rick, 里克,

You can validate you should have a look at the validate function in Laravel. 您可以验证,您应该看看Laravel中的validate函数。 You can validate the input you sent in and when there are errors send them back into your view. 您可以验证您发送的输入,如果出现错误,则将其发送回视图。

For examples look at the url: https://laravel.com/docs/5.1/validation 有关示例,请查看网址: https : //laravel.com/docs/5.1/validation

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM