laravel 如何验证laravel 5.4单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42532823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to validate laravel 5.4 radio button?
提问by Masum
this is my radio button code
这是我的单选按钮代码
<div class="radio">
<label><input type="radio" name="gander" value="male">male </label>
<label><input type="radio" name="gander" value="female">female</label>
</div>
and my controller code is
我的控制器代码是
//validate this form
$this->validate(request(),[
'title' => 'required',
'body' => 'required',
'gander'=> 'in:male,female'
]);
$post = new Post;
$post->title = $request['title'];
$post->body = $request['body'];
$post->status= $request['status'];
$post->male = $request['male'];
$post->female= $request['female'];
$post->save();
//And then redirect to the home
return redirect('blog');
}
when i submit this form it show this error
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'male' cannot be null (SQL: insert into posts
(title
, body
, status
, male
, female
, updated_at
, created_at
) values (sdf, sdfsfsd, 0, , , 2017-03-01 13:09:54, 2017-03-01 13:09:54))
当我提交此表单时,它显示此错误 SQLSTATE[23000]: Integrity constraint conflict: 1048 Column 'male' cannot be null (SQL: insert into posts
( title
, body
, status
, male
, female
, updated_at
, created_at
) values (sdf, sdfsfsd, 0, , , 2017 -03-01 13:09:54, 2017-03-01 13:09:54))
that means both of radio button data need for database field. and into my table have these field 'title', 'body', 'status', 'male', 'female' 'created_at' & 'updated_at'
这意味着两个单选按钮数据都需要数据库字段。并在我的表中有这些字段“title”、“body”、“status”、“male”、“female”、“created_at”和“updated_at”
my problem is how to validate radio button & how to insert data with radio button value? Thanks.
我的问题是如何验证单选按钮以及如何使用单选按钮值插入数据?谢谢。
回答by Shahid Ahmad
If gender is required field then you should also pass required validation to gander property like below
如果性别是必填字段,那么您还应该将所需的验证传递给如下所示的 gander 属性
'gander'=> 'required|in:male,female'
if it is not a require field then make the male column nullables in your database table.
如果它不是必需字段,则使数据库表中的男性列可以为空。
and there is also problem in your table design instead of putting male value to male and female value to female make on column with name gender and type bollean then set 0 for male and 1 for female.
并且您的表设计中也存在问题,而不是将男性值设为男性,将女性值设为女性,在名称为性别和类型 bollean 的列上制作,然后为男性设置 0,为女性设置 1。
if u don't want to use boolean then use enum datatype for gender.enum is the best option in your case.
如果您不想使用布尔值,则使用 enum 数据类型作为性别。enum 是您的最佳选择。
回答by jakub wrona
The error message has nothing to do with the validation. The validation is passed and you do it properly. But your model seems weird, because instead of same "gender" column you have the male and female ones.
错误消息与验证无关。验证通过,您正确执行。但是你的模型看起来很奇怪,因为你有男性和女性的列,而不是相同的“性别”列。
回答by CHARITRA SHRESTHA
You can validate in the client-side just by adding the required attribute
您只需添加所需的属性即可在客户端进行验证
<!-- Gender Field -->
<div class="form-group col-sm-6">
{!! Form::label('gender', 'Gender: *') !!}
<br>
<label class="radio-inline">
{!! Form::radio('gender', "Male", null,['required']) !!} Male
</label>
<label class="radio-inline">
{!! Form::radio('gender', "Female", null) !!} Female
</label>
</div>
回答by Kamlesh
Radio required featured works with laravel:
广播所需的 laravel 特色作品:
@foreach($status_list as $status_key => $status)
{!! Form::radio('status', $status_key, false, array('id'=>'status_'.$status_key, 'required'=>'required' )); !!}
{!! Form::label('status_'.$status_key, $status ) !!}
@endforeach