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

Asked

Viewed 1,057 times

0

I am using the PHP language and the Laravel Framework 5. In the form validation, if it contains any blank field or with unaccepted character sizes, clicking save shows the validation message, but the fields of the textarea and select option that were filled are empty. How to 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 only has 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>
  • Graziane welcome to Stack Overflow, please do not duplicate questions unnecessarily. It seems to me that João’s answer solves both problems with inputs, as selects and textareas, I recommend. Now if the problem is another could try to explain?

  • A understood what happened, asked a question about input then asked a question about textarea and select, however in this how John answered about the 3 situations there mark both as duplicate [and just to join the content, is not a penalty =)

1 answer

3

You must use the function old. She is responsible for bringing back data from the previous disappearance stored 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 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 the Laravel Collective - HTML and Form Builder

  • has still in the controller of redirect()->back()->withInput(), or is not necessary?

  • @Miguel depends. If she is using Request created by the command php artisan make:request NomeDoRequest, no need. Laravel already makes automatic

  • Haa didn’t know, obgado @Wallace

  • The @Miguel remark is very pertinent. It is not always necessary to create a Formrequest for Resource.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.