Wednesday, March 1, 2017

how to validate laravel 5.4 radio button?

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

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.




via Masum

Advertisement