如何验证laravel 5.4单选按钮? [英] how to validate laravel 5.4 radio button?

查看:70
本文介绍了如何验证laravel 5.4单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的单选按钮代码

<div class="radio">
  <label><input type="radio" name="gander" value="male">male </label>
  <label><input type="radio" name="gander" value="female">female</label>
</div>

我的控制器代码是

   //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');
}

当我提交此表格时,它会显示此错误 SQLSTATE [23000]:违反完整性约束:1048列'male'不能为空(SQL:插入到posts(titlebodystatusmalefemaleupdated_atcreated_at)值(sdf,sdfsfsd,0,,,2017-03-01 13:09:54,2017-03-01 13:09:54))

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))

这意味着两个单选按钮数据都需要数据库字段.在我的表格中有以下字段:标题",身体",状态",男性",女性","created_at"和'updated_at'

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'

我的问题是如何验证单选按钮&如何插入带有单选按钮值的数据?谢谢.

my problem is how to validate radio button & how to insert data with radio button value? Thanks.

推荐答案

如果必须填写性别字段,则还应将所需的验证信息传递给gander属性,如下所示

If gender is required field then you should also pass required validation to gander property like below

'gander'=> 'required|in:male,female' 

如果这不是必填字段,则使数据库表中的公列为空.

if it is not a require field then make the male column nullables in your database table.

在表设计中也存在问题,而不是在名称为性别并键入bollean的列上将男性价值转换为男性价值,将女性价值转换为女性价值,然后将0设置为男性,将1设置为女性.

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.

如果您不想使用布尔值,则对性别使用enum数据类型.enum是您情况下的最佳选择.

if u don't want to use boolean then use enum datatype for gender.enum is the best option in your case.

这篇关于如何验证laravel 5.4单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆