Laravel Blade 请求方式
Laravel Blade Request method
最近我开始学习 Laravel,方法是遵循 Laravel From Scratch cast(https://laracasts.com/series/laravel-5-from-scratch)。
现在我正在尝试向注册表中添加其他功能(从错误反馈开始),但我已经 运行 遇到了问题。
是否有(本地)方法来检查表单是否已提交?
我尝试使用:
{{ Request::method() }}
但即使在 运行 命令 php artisan make:auth
生成的默认脚手架上按下 Register 之后,它 returns GET 而操作表单是 POST 并且它触发具有 POST Request 类型的路由。
所有这一切的原因是我想根据以下要求向元素添加CSS class。
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
有人知道解决办法吗?
里克,
你可以验证你应该看看Laravel中的验证函数。您可以验证您发送的输入,并在出现错误时将它们发送回您的视图。
查看示例 url:https://laravel.com/docs/5.1/validation
希望对您有所帮助。
我想你想实现这个:
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
}
}
在输入字段中是这样的:
<input name="name" class="validate{{ old('name') ? ( $errors->first('name') ? ' invalid' : ' valid') : '' }}" type="text" value="{{ old('name') }}">
最近我开始学习 Laravel,方法是遵循 Laravel From Scratch cast(https://laracasts.com/series/laravel-5-from-scratch)。
现在我正在尝试向注册表中添加其他功能(从错误反馈开始),但我已经 运行 遇到了问题。
是否有(本地)方法来检查表单是否已提交? 我尝试使用:
{{ Request::method() }}
但即使在 运行 命令 php artisan make:auth
生成的默认脚手架上按下 Register 之后,它 returns GET 而操作表单是 POST 并且它触发具有 POST Request 类型的路由。
所有这一切的原因是我想根据以下要求向元素添加CSS class。
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
有人知道解决办法吗?
里克,
你可以验证你应该看看Laravel中的验证函数。您可以验证您发送的输入,并在出现错误时将它们发送回您的视图。
查看示例 url:https://laravel.com/docs/5.1/validation
希望对您有所帮助。
我想你想实现这个:
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
}
}
在输入字段中是这样的:
<input name="name" class="validate{{ old('name') ? ( $errors->first('name') ? ' invalid' : ' valid') : '' }}" type="text" value="{{ old('name') }}">