How to keep data already filled in textarea and select option after submitting a form? [duplicate]

0

I'm using the PHP language and the Laravel Framework 5. In the form validation, if it contains some blank field or with not accepted character sizes, when clicking save the system shows the validation message, however the textarea fields and the select option that were filled in are blank. How do I keep these types of data in the form after clicking save when some field is incorrect?

Textarea code:

    <textarea class="form-control" name="descricao" id="descricao"></textarea>

My select has only one option because it takes the values directly from the database. Select option code:

    <select class="form-control" name="categoria" id="categoria">           
        <option value="null"> Selecione uma categoria </option>
            @foreach($categorias as $row)
                <option value="{{ $row->id }}"
                    {{ $row->nome }}
                </option>
            @endforeach
    </select>
    
asked by anonymous 30.05.2016 / 17:29

1 answer

3

You should use the old function. It is responsible for bringing in the previous submission data saved in a Session Flash.

So you can just do it like this:

 <input type="text" name="nome" value="{{ old('name') }} />
 <textarea name="texto">{{ old('texto') }}</textarea>

However, I suggest that you install the Laravel Collective HTMLBuilder library. This will make it easier to create inputs, textareas or selects.

Example:

{!! 
    Form::select('item_id', $itens, old('item_id'), ['class' => 'form-control']) 
!!}

{!!
   Form::textarea('texto', old('texto'))
!!}

Instructions for installing Laravel Collective - HTML and Form Builder

    
30.05.2016 / 17:39