0

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.

Sergej Fomin
  • 1,822
  • 3
  • 24
  • 42

2 Answers2

1

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
}
Oleg
  • 655
  • 4
  • 10
  • Thank you, Oleg. In the controller i actually wrote: $request_array = request()->all(); $request_array['сity_from']= request()->input('city_from'); dd($request_array); – Sergej Fomin Jul 26 '17 at 21:10
  • Actually, the best way to pass the request to a method is Dependency Injection. I'll edit post and show you how the best practices suggest us to work with requests :) – Oleg Jul 26 '17 at 21:12
  • yeap, please edit . I wrote 'request()->input('city_from) because it's in my FormRequest Validator. ("class tourRequest extends FormRequest") and when I use $request it doesn't work... Should I add "class tourRequest extends FormRequest (Request $request) "? – Sergej Fomin Jul 26 '17 at 21:14
  • Thanks for editing the post, Oleg, but I think it all made it a little bit too complicated. My main question was how to get the value of 'city_from" equal 'null' when nothing is chosen. You answered it in your first post. Other people questioning the same question would find it easier to read your original post, than edited one. I would suggest you rolling it back:) – Sergej Fomin Jul 26 '17 at 21:36
0

remove

disabled="disabled"

and add

value="null"

hope it'll solve the problem.

Rafik Farhad
  • 1,182
  • 2
  • 12
  • 21
  • I also hope so:) But how to do it in Laravel Collective?:) – Sergej Fomin Jul 26 '17 at 20:47
  • {!! Form::select('сity_from', [ 'null' => 'Please select', 'l' => 'London', 't' => 'Tokyo', 'm' => 'Moscow'], 'null' ) !!} I didn't check it, but it should work. Let me know if it has any problem. – Rafik Farhad Jul 26 '17 at 20:55