我在PHP和Laravel框架中都很新,我有以下疑问。
我正在按照本教程将reCAPTCHA插入表单(但我的疑问与表单验证比 reCAPTCHA 更相关):http ://tutsnare.com/how-to-use-captcha-in-laravel-5/
因此,要声明一个表单,它在视图中使用以下语法:
{!! Form::open(array('url'=>'contact','method'=>'POST', 'id'=>'myform')) !!}
我认为这种语法与laravelcollective/html命名空间有关,是吗?
所以我安装了它来执行语句:
composer require "laravelcollective/html":"^5.4"
在控制器方法中定义了处理表单 sumbit 操作的index()方法:
<?php namespace App\Http\Controllers;
use Input;
use Validator;
use Redirect;
use Session;
class EnquiryController extends Controller {
public function index()
{
$data = Input::all();
$rules = array(
'name' => 'required',
'email' => 'required|email',
'subject' => 'required',
'g-recaptcha-response' => 'required|captcha',
'msg' => 'required',
);
$validator = Validator::make($data, $rules);
if ($validator->fails()){
return Redirect::to('/contact')->withInput()->withErrors($validator);
}
else{
// Do your stuff.
}
}
}
因此,正如您在前面的代码片段中所见,此方法使用$rules数组提供输入验证,如果验证失败,则会重定向到显示包含验证错误的同一页面:
return Redirect::to('/contact')->withInput()->withErrors($validator);
这将通过这部分代码在视图中打印:
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br /><br />
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
我的疑问是:我是否可以使用纯 HTML 表单而不是laravelcollective/html命名空间提供的相同方式验证表单输入并返回潜在错误?