Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: textarea does not refill when using Input::old

Tags:

laravel

I am experiencing that only the Input Textfields respond as expected when I write the code to repopulate a form (when errors were found for example) or when from a table row I click in the Edit button and I go to an editable form. The field for a textarea is not repopulated so it comes up empty, therefore, if I save it, I would delete the content of the textarea. (I know I am asking a succession of questions lately, the reason is that I have basically finished my application and I left for the end the minor things I could not solve, so my apologies for that).

here is an example of what I am saying:

This WORKS for input textfield:

WORKS

<div class="col-md-4">
<label for="relato">Charges</label>
<input type="text" name="expenses" maxlength ="30" class="form-control" 
value = "{{ $user->expenses }}"/>
</div>

That is, the $user->expenses fills the textfield of the form that comes up when clicking the Edit button of a table row.

However, that does not work for a textarea field:

<div class="row">

              <label for="relato">Description</label>
              <textarea name ="message" id="message" rows="5" cols="100" value = "{{ $user->message }} class="form-control" 
      </textarea>
              </div>

See? that part $user->message will not pass the content to the textarea of a form.

Similarly: with Input::old

Works for an Input textfield

WORKS

 Email: <input type="text" class="form-control" name="email" {{ (Input::old('email')) ?' value ="' . e(Input::old('email')). '"' : ''  }}>

DOES NOT WORK FOR TEXTAREA

<div class="form-group">
  <label for="relato">Une petite description</label>
  <textarea id="message" name = "content" rows="10" cols="50" onKeyPress class="form-control"  {{ (Input::old('content')) ?' value ="' . e(Input::old('content')). '"' : ''  }}
  ">
  </textarea>{{ $errors->first('content')}}
        </div>

And the controller is also trying to refill the form by sending ->withInput

 if($validator->fails()){

           return Redirect::route('usersgetformtopostads')

                        ->withErrors($validator)

                        ->withInput();                           

                        } 

but, as I said, it only works for textfields. Does not repopulate me a select list or a textrarea

By the way, I have looked a related question here where the author says he could not repopulate a File field and he was told that "you cant" and he gave that as a correct answer, however, I have been able to repopulate Files uploaded, not having any problem with that.

like image 846
Soviet Patriot CCCP Avatar asked Apr 27 '14 19:04

Soviet Patriot CCCP


4 Answers

textarea does not have a value attribute. Values in textarea should be inside <textarea></textarea>, so in your case:

<textarea id="message" name = "content" rows="10" cols="50" onKeyPress class="form-control">
{{{ Input::old('content') }}}
</textarea>
like image 143
Marwelln Avatar answered Nov 01 '22 15:11

Marwelln


Just figured it out, put the old value in between the brackets as below

<textarea name="message">{{ old('message') }}</textarea>
like image 29
Ngaiza Avatar answered Nov 01 '22 14:11

Ngaiza


This is another way to do the same but with the Forms component from laravel:

{{ Form::textarea('content',Input::old('content'),array('id' => 'message')) }}
like image 4
felipebla Avatar answered Nov 01 '22 14:11

felipebla


I'd like to add one thing, if you use Form Class for the form and elements then you don't need to explicitly right the Input::old('element-name') to retrieve the value of the previous form submission.
Just use
{{ Form::text('name', null, array('id'=>'name', 'class' => 'class-to-apply')) }}
And you're good to go.

Here, null value for the text field will be null for the first time, and if you redirect back this page with input then this will automatically grab the value.

like image 1
ssi-anik Avatar answered Nov 01 '22 15:11

ssi-anik