Drop-down list with required value Laravel 5.5

0

I'm working on laravel 5.5

I have these lists:

{!! Form::mySelect('id_uso', 
                   'Uso', 
                   [0 => 'Escoge una opción'] + App\Usos::pluck('nombre', 'id')->toArray(), 
                   null, 
                   ['required','id' => 'listaUsos', 'class'=>'chosen']) !!}

{!! Form::mySelect('id_padre', 
                   'Servidor padre: Escojer unicamente los que esten marcador con - 1', [0 => 'Escoge una opción'] +
                   App\Servidores::select(DB::raw("CONCAT(hostname, ' - ', id_uso) AS hostname_version"), "id")-> pluck('hostname_version', 'id')->toArray(),       
                   null, 
                   ['required','class'=>'requerido-con-virtual', 'style' => 'width: 100%;']) !!}

The problem is that when saving, take this value [0 = > 'Choose an option'], and I need you to be required to choose a value from the list, instead of taking 'Choose an option', I put the required, but it does not work.

Could someone help me?

Thanks for reading, happy day.

    
asked by Omar Noa 16.08.2018 в 19:00
source

2 answers

0

Another colleague from the community helped me solve the problem in a comment on another question.

To this drop-down list:

{!! Form::mySelect('id_padre', 
                   'Servidor padre: Escojer unicamente los que esten marcador con - 1', [0 => 'Escoge una opción'] +
                   App\Servidores::select(DB::raw("CONCAT(hostname, ' - ', id_uso) AS hostname_version"), "id")-> pluck('hostname_version', 'id')->toArray(),       
                   null, 
                   ['required','class'=>'requerido-con-virtual', 'style' => 'width: 100%;']) !!}

I removed this part so that I would not take it as a first option:

[0 => 'Escoge una opción'] +

Y Le adicione

['required','class'=>'requerido-con-virtual', 'style' => 'width: 100%;', 'placeholder' => 'Escoge una opción']) !!} <-----

For me to take the required, and the placeholder to send the message that I should choose an object from the list, without that text being taken as a value.

I thank everyone for your attention, Thank you very much.

    
answered by 16.08.2018 / 19:19
source
1

You could have your validation, within your FormRequest or where you validate the data. apart from the rule required the rule not_in:0 to avoid the first element of select

'id_padre' =>'required|not_in:0',
'id_uso' =>'required|not_in:0'
    
answered by 16.08.2018 в 19:04