Laravel Collective select to output null when submitted - laravel

I am using Laravel Collective for creating my webform.
{!! Form::select('сity_from', ['London', 'Tokyo', 'Moscow'], null, ['placeholder' => 'Choose city'] ) !!}
which produces the following html:
<select id="сity_from" name="сity_from">
<option selected="selected" disabled="disabled" hidden="hidden" value>Choose city</option>
<option value="London">London</option>
<option value="Tokyo">Tokyo</option>
<option value="Moscow">Moscow</option>
when I choose no city and submit form, and then dd($request->all());in Controller
i can see nothing, I mean, there is no $request->all()['city_from'];
I would like to get ['city_from' = null] in this case.
I suppose I have to change 'value' in
<option selected="selected" disabled="disabled" hidden="hidden" value>Choose city</option>
to value="null"?
Or something else?
I would like to be using Laravel Collective when solving this problem.

I suggest you to not bother with the presence of 'city_from' in your request.
You might use $cityForm = $request->input('city_from');
And you will have $cityForm set to the actual value, or to null
public function store(Request $request)
{
$cityForm = $request->input('city_from'); //will always be actual value or null
}

remove
disabled="disabled"
and add
value="null"
hope it'll solve the problem.

Related

Laravel Livewire: Input select, default option selected

I am trying to fetch country codes from my database and trying to get the default value via IP address. It works just as I want for a second but then I don't know what happens but it refreshes itself and scrolls to the first option instead of the selected option.
Livewire Controller Component
use App\Models\CountryCodes;
use Livewire\Component;
use Location;
class TestCountry extends Component
{
public $iso;
public $country_codes;
public $country_code;
public function mount()
{
$iso=Location::get('ip');
$this->iso=$iso->countryCode;
}
public function render()
{
return view('livewire.test-country',[
$this->country_codes = CountryCodes::select('nicename','iso','phonecode')->get()->toArray()
]);
}
}
Livewire Blade Component
<select wire:model.lazy="country_code" name="country_code" id="country_code" class="form-control" required>
#foreach($country_codes as $country_code)
<option value="{!! $country_code['iso'] !!}"
wire:key="{{$country_code['iso']}}"
{{ $country_code['iso'] == $iso ? 'selected' : ''}}>
{!! $country_code['iso'] !!} +{!! $country_code['phonecode'] !!}
</option>
#endforeach
</select>
This code does select my default option but it changes and moves to the first option automatically. Am I doing something wrong here?
I believe what is happening is, $iso is set correctly, but the select is bound to the $country_code property, not $iso, so briefly, the check you have works, but because $country_code doesn't have a value, the selected option is lost when the state is updated by Livewire.
TLDR: Livewire is checking whatever is in the wire:model attribute, so the class property must be set for it to work.
I would try something like this:
public function mount()
{
$iso = Location::get('ip');
$this->iso = $iso->countryCode;
// set $country_code to $iso
$this->country_code = $this->iso;
}
I believe Livewire will intelligently select the state, so I think the selected check can be removed:
<select wire:model.lazy="country_code" name="country_code" id="country_code" class="form-control" required>
#foreach($country_codes as $country_code)
<option
value="{!! $country_code['iso'] !!}"
wire:key="{{$country_code['iso']}}"
>
{!! $country_code['iso'] !!} +{!! $country_code['phonecode'] !!}
</option>
#endforeach
</select>
Also, any reason for using {!! !!} tags in the view rather than just {{ }}?

Laravel Livewire: Passing option value onChange of Select input

I am trying to pass a value from the <option> of my <select> input to my livewire controller component and echo the value.
Livewire Blade View Component:
{!! Form::select('goals',$goals_plucked,null,[
'placeholder' => trans('classes.backend.forms.select_goals'),
'class' => 'custom-select',
'id' => 'goals',
'wire:change' => "goals(this.val)",
]) !!}
This get's me an output of null in my Livewire Controller Component
Livewire Controller Component
public $goals;
public function goals($goals)
{
dd($goals);
}
After watching some tutorials. I also tried using 'wire:change' => "goals($event.target.value)", which gave me an error of Undefined variable $event, obviously because it was not defined in main controller. I am not sure what am I doing wrong here.
What I am trying to do: Trying to create a flow just like select country then select state and then select city. via livewire. Before selecting a country the inputs of the select state and city won't be visible
I tried below code and it worked for me well. Just had to make sure I use normal html form inputs and dynamically add options to it by foreach loop. Also used mount() function for getting getting values and id's for select dropdowns.
Livewire Blade View Component:
<select wire:model="goal" name="goal" class="form-control" required>
<option value="">
{!! trans('classes.backend.forms.select_goals') !!}
</option>
#foreach ($goals as $goal)
<option value="{{ $goal->id }}">{{ $goal->goals }}</option>
#endforeach
</select>
Livewire controller component
public $goals;
public $goal;
public function mount()
{
$this->goals = Goals::all()->isActive();
}
public function updatedGoal($value)
{
dd($value);
}
just give wire:model="variable_name" to select in front end.
and in livewire controller there should be a public variable with same name. it will automatically get the value on select value change.
below is the example of same
<select class="custom-select border-0 shadow-none" id="enquiry_for" wire:model="enquiry_for">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>

How do I remove the null value in the selectbox array when nothing is selected

I am trying to validate an array of select option to check if it is empty as seen below. When value is not selected the default value of the array is null which passes the validation. How do I remove the null value from the select box when nothing is selected or if there is any other way you can suggest.
My code
<select name="test_id[]" class="form-control select-test-{{$idselect}}"
id="select-{{Illuminate\Support\Str::random(10)}}">
<option value="">Select Patient Test</option>
#foreach ($tests as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
#endforeach
</select>
Laravel validation code
$request->validate([
'test_id' => 'required:array'
], [
'test_id.required' => "Test is required"
]);
it goes through even when no selected
Output when not selected
When you give a value for an option it will be passed as a parameter, can you change this line:
<option value="">Select Patient Test</option>
As:
<option disabled selected>Select Patient Test</option>

Controller method does not exist. But actually exists. Not passing value with request neither

This is just driving me crazy. A lot of research, testing, double checking the rotes with artisan, and even a new clean laravel installation to make sure my routes are working fine, and i keep getting the error that my method does not exist and it does not matter if i change namespace, or the method name.
My route web.php
Route::get('/view1', function () {
return view('view1');
});
Route::post('/view1results', [
"uses" => 'MyController#MyMethod',
"as" => 'MySearch'
]);
Route::get('/', function () {
return view('welcome');
});
My view1:
<label class="myfilters">filters:</label>
<form action="{{ route('MySearch') }}" method="POST">
{{ csrf_field() }}
<select class="myfilters" id="filter1" name="filter1">
<option value=""> -- Select Something --</option>
<option value=""> option 1 </option>
<option value=""> option 2 </option>
</select>
<input type="submit"></input>
</form>
My Controller
<?php
use Input;
use app\SubSectorsBPIsData;
use App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class SubSectorsBPIsDataController extends Controller
{
public function MyMethod(Request $request)
{
$valueselected= $request->input('filter1');
return view('view1results', ['filter1' => $valueselected]);
}
}
Error is: BadMethodCallException
Method App\Http\Controllers\MyController::MyMethod does not exist.
And as you can see the method called: MyMethod is there.
It does not matter what i put on my view1results, i was just trying a simple {{echo $valueselected}};
Also tried to add another different view to pass data from my controller. In my web.php:
Route::get('/viewresults2', function () {
return view('viewresults2');
});
And then in my controller
return view('viewresults2', ['filter1' => $valueselected]);
It gives me a blank page.
Even if with this other approach it's recognizing the method, it's now returning any view nor value: If i just bypass the view and try to:
return $selectedTrend;
Still get a blank page.
It's not recognizing the method and it's not passing any value from the select.
Whats going on please? :(
Update
One more thing: my website is file structure is:
www.mydomain.net/software/public/view1
And after submiting the form redirects to:
www.mydomain.net/software/public/view1results
Just adding it up so you could help me better with the route, since i think that's where the problem is and i couldn't figure out what route to use on my action form.
Turns out it was a silly mistake (like always). In my view i had this code for the dropdown:
<select class="myfilters" id="filter1" name="filter1">
<option value=""> -- Select Something --</option>
<option value=""> option 1 </option>
<option value=""> option 2 </option>
</select>
Notice the <option value=""> does not have any value and controller needs to know which value to get. So do this instead:
<option value="option1"> option1 </option>
<option value="option2"> opion2 </option>
And controller will get the value. All the rest it's set properly.
Hope it helps another newbie in
Laravel 5.7

Laravel Rules Validation - Only another field has a value

<input type="checkbox" name="grievance_discipline" value="checked">
<select name="grievance_type">
<option value="_none" selected>- Select One</option>
<option value="suspension">Suspension</option>
</select>
<?php
$this->validate($request,[
'grievance_type' => 'required_if:grievance_discipline, checked|not_in:_none',
]);
?>
The trouble I am running in to, is telling Laravel to ONLY use a particular validation rule if another field has a value.
In the example, the discipline_type field (a select list), is required IF grievance_discipline is checked.
The default select option for the select list is - Select One - with the value of _none. In the validation rules, _none should be thrown out, but it should ONLY validate on it if the grievance_discipline checkbox has value 'checked'.
Could anybody help me with this, please?
How about setting the first option as disabled so the user are forced to pick a value or not submitting a value, hence gracefully fail your validation check. Also, use required_with instead of required_if to check if another value exist or not.
<input type="checkbox" name="grievance_discipline" value="checked">
<select name="grievance_type">
<option disabled selected value>- Select One</option>
<option value="suspension">Suspension</option>
</select>
Then in the PHP, do
$this->validate($request,[
'grievance_type' => 'required_with:grievance_discipline'
]);

Resources