Validating Arrays of radio buttons in laravel - laravel

I am using Laravel 5.1 and homestead.
My issue is when validation fails, the radio buttons do not repopulate with data from the original user's input.
My validation rules from PostRequest.php
public function rules()
{
//validating input text field --> this one works
$rules = ['owner' => 'required|max:15',];
//validating array of input text fields --> this too works
foreach($this->request->get('item') as $a => $z)
{
$rules['item.'.$a] = 'required';
}
//validating array of radio buttons --> does not return with data
foreach($this->request->get('radio') as $b => $y)
{
$rules['radio.'.$b] = 'required';
}
return $rules;
}
A segment of my view(blade.php)
<div class="row">
<div class="tcell col-xs-6">
<label for="item[0]" class="sr-only"></label>
<input class="form-control" id="item[0]" name="item[0]" type="text" placeholder="enter item" value="{ { old('item.0') } }"/>
</div>
<div class="tcell col-xs-6">
<div class="radio">
<input type="radio" id="s15" name="radio[0]" value="5" /><label for="s15" title="5">5</label>
<input type="radio" id="s14" name="radio[0]" value="4" /><label for="s14" title="4">4</label>
<input type="radio" id="s13" name="radio[0]" value="3" /><label for="s13" title="3">3</label>
<input type="radio" id="s12" name="radio[0]" value="2" /><label for="s12" title="2">2</label>
<input type="radio" id="s11" name="radio[0]" value="1" /><label for="s11" title="1">1</label>
</div>
</div>
</div>
...
<div class="tcell col-xs-6">
<div class="radio">
<input type="radio" id="s20" name="radio[1]" value="5" /><label for="s20" title="5">5</label>
<input type="radio" id="s19" name="radio[1]" value="4" /><label for="s19" title="4">4</label>
<input type="radio" id="s18" name="radio[1]" value="3" /><label for="s18" title="3">3</label>
<input type="radio" id="s17" name="radio[1]" value="2" /><label for="s17" title="2">2</label>
<input type="radio" id="s16" name="radio[1]" value="1" /><label for="s16" title="1">1</label>
</div>
</div>
The old() function worked for both input(text) and array of input(text). but I do not know how to apply it with radio buttons.
Thanks

Here's the solution
<input type="radio" id="s15" name="radio[1]" value="5" { { old('radio.1')=="5" ? 'checked='.'"'.'checked'.'"' : '' } } /><label for="s15" title="5">5</label>
<input type="radio" id="s14" name="radio[1]" value="5" { { old('radio.1')=="4" ? 'checked='.'"'.'checked'.'"' : '' } } /><label for="s14" title="4">4</label>
...
This will only help redirect back with the inputed data, however, validation will still fail if nothing is selected since nothing gets submitted. The fix is to use for loop instead of foreach loop. You must get the count of the radio buttons to iterate.

try to amend as following
blade template:
<div class="radio">
<input type="radio" id="s15" name="radio" value="5" /><label for="s15" title="5">5</label>
<input type="radio" id="s14" name="radio" value="4" /><label for="s14" title="4">4</label>
<input type="radio" id="s13" name="radio" value="3" /><label for="s13" title="3">3</label>
<input type="radio" id="s12" name="radio" value="2" /><label for="s12" title="2">2</label>
<input type="radio" id="s11" name="radio" value="1" /><label for="s11" title="1">1</label>
</div>
Form request
public function rules()
{
//validating input text field --> this one works
$rules = ['owner' => 'required|max:15', 'radio' => 'required'];
//validating array of input text fields --> this too works
foreach($this->request->get('item') as $a => $z)
{
$rules['item.'.$a] = 'required';
}
return $rules;
}

Related

Compact Two Functions in One View In Laravel

In my Controller
public function startQ(Request $request){
$users = question::limit(2)->get();
return view('answerDesk',compact('users'));
}
public function answerQuestions(Request $request){
$answers =$request->all();
return view('answerDesk',compact('answers'));
}
in My View
<form action="getAnswer" method="post" role="search"> #csrf #foreach($users as $answer) <input type="hidden" name="question[]" value="{{$answer->id}}">
<label class="form-check-label" for="inlineRadio1">
<h4 style=""><i class="fas fa-circle"></i>
{{$answer->id}}:{{$answer->question}}
</h4>
</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->a}}">(A) : <label class="form-check-label" for="inlineRadio1">{{$answer->a}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->b}}">(B) : <label class="form-check-label" for="inlineRadio1">{{$answer->b}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->c}}">(C) : <label class="form-check-label" for="inlineRadio1">{{$answer->c}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->d}}">(D) : <label class="form-check-label" for="inlineRadio1"> {{$answer->d}}</label><br>
<input type="hidden" name="ans[{{$answer->question}}]" value="{{$answer->ans}}"> #endforeach <input type="submit" value="submit">
</form>
I don't know how to use the getAnswer function
Can I Compact Two Functions in Controller To One View In Laravel?
If Yes How can I Do That?

How do I retrieve answers of the multichoice radio button in laravel?

I am developing a multi choice questionaire. I want to get the answered question of the student. How do I go about it?
My Code
<div class="car card-body" style="border: #d3d3d3 1px solid; padding: 2rem">
1. Don't do tath <br />
<div style="padding-top: 1rem;">
<input type="hidden" name="question_id" value=2TpZL1kJjUj7XNysyqSk">
<input type="radio" name="option[1]" id="" style="display:inline"
value="a">
1. Computer is an electronic device
<br />
<input type="hidden" name="question_id" value="2TpZL1kJjUj7XNysyqSk">
<input type="radio" name="option[1]" id="" style="display:inline"
value="b">Computer is human
<br />
<input type="hidden" name="question_id" value="2TpZL1kJjUj7XNysyqSk">
<input type="radio" name="option[1]" id="" style="display:inline"
value="c">in hime i came
<br />
</div>
</div>
<br />
<div class="car card-body" style="border: #d3d3d3 1px solid; padding: 2rem">
2. What is Web Development <br />
<div style="padding-top: 1rem;">
<input type="hidden" name="question_id" value="8qbpQJ2ob4RvlcEJzszY">
<input type="radio" name="option[2]" id="" style="display:inline" value="a">development involved database management
<br />
<input type="hidden" name="question_id" value="8qbpQJ2ob4RvlcEJzszY">
<input type="radio" name="option[2]" id="" style="display:inline"
value="b">
web development involves the design of the law
<br />
<input type="hidden" name="question_id" value="8qbpQJ2ob4RvlcEJzszY">
<input type="radio" name="option[2]" id="" style="display:inline"
value="c">Web store is the absolute beginer
<br />
</div>
</div>
<div class="form-group mt-3">
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>
Controller
public function submitExam(Request $request){
$myOption = $request->get('option');
$optioncount = count($myOption);
for($i=1; $i<$optioncount + 1; $i++){
echo $request->get("question_id");
echo $myOption[$i]. "<br />";
$option = Options::where('question_id', $question)->value('answer');
if($myOption[$i] == $option){
echo "Good";
}
}
}
I want to select from option where question id is the same and compare it the value of the selected radio button but I only get the question id of the first question. Can anyone help me? Thanks
I have finally solved it.
<div class="car card-body" style="border: #d3d3d3 1px solid; padding: 2rem">
<div style="padding-top: 1rem;">
<input type="hidden" name="question_id" value=2TpZL1kJjUj7XNysyqSk">
<input type="radio" name="option[1]" id="" style="display:inline" value="a">
1. Computer is an electronic device
<br />
<input type="hidden" name="question_id" value="2TpZL1kJjUj7XNysyqSk">
a. <input type="radio" name="option[1]" id="" style="display:inline" value="b">Computer is human
<br />
<input type="hidden" name="question_id" value="2TpZL1kJjUj7XNysyqSk">
<br />
b. <input type="radio" name="option[1]" id="" style="display:inline" value="c">computer is
</div>
</div>
Controller
$score = 0;
foreach ($request->input('question_id', []) as $key => $question) {
$answer = $request->input('option.'.$question);
$correctAnswer = Questions::where('question_id', $question)->value('answer');
if ($correctAnswer == $answer) {
$score++;
}
}
source
https://github.com/imran1234567/Online-Examination-System/blob/master/resources/views/tests/create.blade.php
https://github.com/imran1234567/Online-Examination-System/blob/master/app/Http/Controllers/TestsController.php

Submit checkbox values to Controller

I am having the following form, which I would like to submit to my backend.
<form id="revisionFilter" action="{{ route('revision.filter') }}" method='POST'>
<input class="form-check-input" type="checkbox" value="" id="checkbox1">
<label class="form-check-label" for="checkbox1">
1
</label>
<input class="form-check-input" type="checkbox" value="" id="checkbox0">
<label class="form-check-label" for="checkbox0">
0
</label>
<label class="form-check-label" for="checkboxNull">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="checkboxNull">
Null
</label>
<input type="hidden" name='_method' value='POST'>
<input id="revisionFilterSubmit" type="submit" class='btn btn-danger btn-sm' value='Filter'>
</form>
My backend looks like the following:
routes:
Route::post('/revision/filter', 'RevisionController#filter')->name('revision.filter');
RevisionController:
public function filter(Request $request)
{
Log::info("Request: ");
Log::info($request);
return redirect()->route('revision.rindex');
}
My problem is that when I press the button I am getting redirected to:
The page has expired due to inactivity.
Please refresh and try again.
Instead I would like to see the request with the values of the checkboxes to implement the db saving functionality.
I appreciate your replies!
since I've already made a comment. I will put my answer in the answer box lol.
To answer you, you're not getting any checkbox value since you're checkboxes don't have any name attribute.
You need to put a name attribute to them. E.g.,
<input class="form-check-input" type="checkbox" value="" name="checkbox0" id="checkbox0">
From there, you should now be able to see the value of the checkbox via its name.
Just put same name to your checkbox.For example
<input class="form-check-input" type="checkbox" value="" id="checkbox1" name="revisionFilter">
<input class="form-check-input" type="checkbox" value="" id="checkbox2" name="revisionFilter">

How can I color input fields border red when I get validation error laravel 5.3

My Validation Controller:
public function store(Request $request)
{
// dd($request->all());
/////Validations////
$this->Validate($request, [
'name' => 'required|min:3|',
'email' => 'required|unique:customers' ,
'favorite' => 'required',
'Password' => 'required|min:6',
'Confirm_Password' => 'required|same:Password',
]);
$user = new customer;
$user ->name=input::get("name");
$user ->email=input::get("email");
$user ->country=input::get("country");
$user ->gender=input::get("gender");
$user ->favorite=implode(" , " , input::get("favorite"));
if(input::hasfile("image")){
$files = Input::file('image');
$name = time()."_". $files->getClientOriginalName();
$image = $files->move(public_path().'/image' , $name);
$user->image=$name;
}
$user ->psw=input::get("psw");
$user->save();
return redirect("showall");
}
Insert.blade.php
<form action="store" method="post" enctype="multipart/form-data">
<ul><span style="color:red;">{!! $errors->first('name') !!}</span></ul>
<label for="name">Name</label>
<input type="text" name="name" value="{{old('name')}}" id="name" autofill="off">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<ul><span style="color:red;">{!! $errors->first('email') !!}</span></ul>
<label for="email">Email</label>
<input type="email" name="email" value="{{old('email')}}" id="email" autofill="off">
<br>
<br>
Country <select name="country" id="country">
<option value="USA">USA </option>
<option value="ENGLAND">ENGLAND </option>
<option value="JAPAN">JAPAN </option>
<option value="ITALY">ITALY</option>
</select>
<br>
<br>
Gender <input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
<br>
<br>
<ul><span style="color:red;">{!! $errors->first('favorite') !!}</span></ul>
<input type="checkbox" name="favorite[]" value="sout">South
<input type="checkbox" name="favorite[]" value="north">North
<input type="checkbox" name="favorite[]" value="east">East
<input type="checkbox" name="favorite[]" value="west">West
<br>
<br>
<input type="file" name="image">
<br>
<br>
<ul><span style="color:red;">{!! $errors->first('Confirm_Password') !!}</span></ul>
<label for="email">Password</label>
<input type="password" name="Password" value="" id="Password">
<ul><span style="color:red;">{!! $errors->first('Confirm_Password') !!}</span></ul>
<label for="email">Confirm Password</label>
<input type="Password" name="Confirm_Password" value="" id="cpsw">
<br>
<br>
<label for="submit"></label>
<input type="submit" name="submit" value="submit" id="submit">
</form>
Form where I want to change fields color:
I want to change input fields border color to change red when I get a validation error.
I try my best but I don't know how to rid off this problem.
Add this CSS code first:
.form-error {
border: 2px solid #e74c3c;
}
And HTML Code is ::
<input type="text" class="form-control{{($errors->first('name') ? " form-error" : "")}}" name="name" placeholder="Name">
if you need error message below the input field then ::
<input class="form-control" required="required" name="name" type="text" id="name">
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}

CasperJs, how to fill a form that only have a class, not a name and not an id?

I want to fill this form, but i dont know how to do it since it only have a classname.
All the examples i saw have an id or a name, to fill the form and submit it, please help.
<form class="header_form" method="post" action="">
<div class="lgn-items">
<div class="login_item">
<label>Email</label>
<input type="text" name="email" tabindex="1" class="inputtext" id="email" />
</div>
<div class="login_item" >
<label>Password</label>
<input type="password" name="password" tabindex="2" class="inputtext" id="pass" />
</div>
<div class="lgn-add">
Registration <span>|</span>
Forgot your password ?
<div class="rembo">
<input type="checkbox" name="remember" value="1" /> Remember me
</div>
</div>
</div>
<div class="login_item lgn-btn" >
<input type="submit" name="login_button" value="Login" tabindex="3" class="login" />
</div>
</form>
You can access to your form using any selector. In your case you to it like this.
casper.then(function () {
casper.fill('form.header_form', {
/** Your parameters here **/
}, true);
});

Resources