如何验证无线电 button/checkbox 并且必须 select 成为 laravel 中的一个
How to validate radio button/checkbox and must to select one in laravel
我正在尝试验证 Laravel 中的单选按钮。这是我的代码,但它不起作用。
在我的例子中,我有一个 动态 表格,其中包含许多不同类型的问题,例如:收音机、支票簿、单一输入、数字输入……所以我有为每种类型的问题使用数组名称。例如:姓名="radio['.$k.']"。
在我的控制器中,我进行了验证,$key 是初始形式的 $k 值。
public function rules()
{
$rules = [];
if (Input::has('radio')) {
foreach (Input::get('radio') as $key => $val) {
$rules['radio.' . $key] = 'required';
}
}
if (Input::has('singleinput')) {
foreach (Input::get('singleinput') as $key => $val) {
$rules['singleinput.'.$key] = 'required|max:10';
}
}
}
public function messages()
{
$messages = [];
if (Input::has('radio')) {
// some code here;
}
}
public function answer_store($survey_id, $patient_id)
{
$rule = $this->rules();
$message = $this->messages();
$validator = Validator::make(Input::all(), $rule, $message);
}
在视图中:
<input type="radio" name="radio['.$k.']" value="'.$str1.'">'.$answer->answer_body
我的代码适用于文本输入类型,但不适用于单选框和复选框。
谁能帮帮我?
您的输入标签以 </label>
结尾。
不确定这是否有帮助。
好的,没有回复,如果您使用 Laravel 5,这就是我的回答。
Laravel 5 在您提交表单时使用请求。您可以在该数据执行您的控制器之前对该数据执行验证。
首先使用终端运行一个artisan命令
php artisan make:request MyRequest
这将在 App\Http\Requests 中创建一个文件。
将其放入新的请求文件中
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class MyRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'myRadios' => 'required'
];
}
}
在您看来,收音机如下所示,确保您想要的收音机组都具有相同的名称。
<input type="radio" name="myRadios" value="1"> Number 1
<input type="radio" name="myRadios" value="2"> Number 2
<input type="radio" name="myRadios" value="3"> Number 3
在您的控制器中,您需要引用请求文件并使用依赖注入将其放入您的函数中。
当你想使用被选中的radio的值时,你使用$request数组
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\MyRequest;
class MyController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function myFormFunction(MyRequest $request)
{
//Get the value of the radio selected
$myVariable = $request['myRadios'];
}
}
?>
一天后,我找到了问题的解决方案。
Using if (Input::has('radio')) { } in the rule code. Because if i
don't select anything, don't have any information related radio to
validate.
我使用的广播问题数组:
<input type="radio" name="radio['.$count_radio.']" value="'.$str1.'">
我的问题是我不知道每张表格有多少单选题。我有很多不同的形式。我将计算 View 文件中单选表格的数量并传递给 Controll ($count_radio).
在控制器中,我制定了这样的规则:
for ($key = 1; $key <= $count_radio; $key++) {
$rules['radio.' . $key] = 'required';
}
我还在视图中添加了此代码的错误:
if($errors->has('radio.'.$count_radio.'')) {
echo $errors->first('radio.'.$count_radio.'');
}
就这些了。
我正在尝试验证 Laravel 中的单选按钮。这是我的代码,但它不起作用。
在我的例子中,我有一个 动态 表格,其中包含许多不同类型的问题,例如:收音机、支票簿、单一输入、数字输入……所以我有为每种类型的问题使用数组名称。例如:姓名="radio['.$k.']"。
在我的控制器中,我进行了验证,$key 是初始形式的 $k 值。
public function rules()
{
$rules = [];
if (Input::has('radio')) {
foreach (Input::get('radio') as $key => $val) {
$rules['radio.' . $key] = 'required';
}
}
if (Input::has('singleinput')) {
foreach (Input::get('singleinput') as $key => $val) {
$rules['singleinput.'.$key] = 'required|max:10';
}
}
}
public function messages()
{
$messages = [];
if (Input::has('radio')) {
// some code here;
}
}
public function answer_store($survey_id, $patient_id)
{
$rule = $this->rules();
$message = $this->messages();
$validator = Validator::make(Input::all(), $rule, $message);
}
在视图中:
<input type="radio" name="radio['.$k.']" value="'.$str1.'">'.$answer->answer_body
我的代码适用于文本输入类型,但不适用于单选框和复选框。 谁能帮帮我?
您的输入标签以 </label>
结尾。
不确定这是否有帮助。
好的,没有回复,如果您使用 Laravel 5,这就是我的回答。
Laravel 5 在您提交表单时使用请求。您可以在该数据执行您的控制器之前对该数据执行验证。
首先使用终端运行一个artisan命令
php artisan make:request MyRequest
这将在 App\Http\Requests 中创建一个文件。
将其放入新的请求文件中
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class MyRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'myRadios' => 'required'
];
}
}
在您看来,收音机如下所示,确保您想要的收音机组都具有相同的名称。
<input type="radio" name="myRadios" value="1"> Number 1
<input type="radio" name="myRadios" value="2"> Number 2
<input type="radio" name="myRadios" value="3"> Number 3
在您的控制器中,您需要引用请求文件并使用依赖注入将其放入您的函数中。
当你想使用被选中的radio的值时,你使用$request数组
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\MyRequest;
class MyController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function myFormFunction(MyRequest $request)
{
//Get the value of the radio selected
$myVariable = $request['myRadios'];
}
}
?>
一天后,我找到了问题的解决方案。
Using if (Input::has('radio')) { } in the rule code. Because if i don't select anything, don't have any information related radio to validate.
我使用的广播问题数组:
<input type="radio" name="radio['.$count_radio.']" value="'.$str1.'">
我的问题是我不知道每张表格有多少单选题。我有很多不同的形式。我将计算 View 文件中单选表格的数量并传递给 Controll ($count_radio).
在控制器中,我制定了这样的规则:
for ($key = 1; $key <= $count_radio; $key++) {
$rules['radio.' . $key] = 'required';
}
我还在视图中添加了此代码的错误:
if($errors->has('radio.'.$count_radio.'')) {
echo $errors->first('radio.'.$count_radio.'');
}
就这些了。