Laravel: textarea does not refill when using Input::old - 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.

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>

Just figured it out, put the old value in between the brackets as below
<textarea name="message">{{ old('message') }}</textarea>

This is another way to do the same but with the Forms component from laravel:
{{ Form::textarea('content',Input::old('content'),array('id' => 'message')) }}

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.

Sorry for late reply
{{Form::textarea('mobile_message', isset($notifications -> mobile_message) ? $notifications -> mobile_message : null, 'id'=> 'mob_id','class' => 'form-control'))}}

<div class="control-group hidden-phone">
<label class="control-label" for="textarea2">Category Description</label>
<div class="controls">
<textarea style="resize:none" name="category_description" required rows="6">
<div class="control-group hidden-phone">
<label class="control-label" for="textarea2">Category Description</label>
<div class="controls">
<textarea style="resize:none" name="category_description" required rows="6" >{{$category_info->category_description}}</textarea>
</div>
</div>
</textarea>
</div>

Normally, textarea does not have a value attribute, but there's a way you can add that with the current Laravel version (which is 8+)
<textarea>{{{ Request::old('content') }}}</textarea>

Related

Extra whitespaces in blade statements. Laravel 8

The problem is, it's not just one variable, but statements with #if, #isset and #foreach directives. I can't remove whitespaces without syntax error. And all these whitespaces are displaying in input fields.
In description field I did this:
Check, if old('description') exists.
If yes, display it.
If not, then check, if variable $post exists (I use this form for store and update methods both).
If yes, display it.
If not, the field remains empty.
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" rows="3">#if(old('description')){{ old('description') }}#else #isset ($post){{$post->description}}#endisset #endif</textarea>
<div class="form-group">
<label for="tags">Tags</label>
<input type="text" name="tags" id="tags"
value="#isset ($post, $tags)#foreach($post->tagged as $tagged){{$tagged->tag_name}},#endforeach #endisset">
I tried package hedronium/spaceless-blade, but it doesn't work with input values.
>#if and "#isset will be parsed as string because # will be parsed as syntax only if not join together with other character except space, new line or tab. You can do if condition without # inside {{}}.
I have a better solution using ternary operator and null coalescing operator.
change your long code
#if(old('description')){{ old('description') }}#else #isset ($post){{$post->description}}#endisset #endif
to
{{old('description') ?? isset($post)?$post->description:''}}
and change
#isset ($post, $tags)#foreach($post->tagged as $tagged){{$tagged->tag_name}},#endforeach #endisset
to
{{isset($post) ? implode(', ', $post->tagged->pluck('tag_name')->toArray() )):''}}
so the full code:
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" rows="3">{{old('description') ?? isset($post)?$post->description:''}}</textarea>
</div>
<div class="form-group">
<label for="tags">Tags</label>
<input type="text" name="tags" id="tags"
value="{{isset($post) ? implode(', ', $post->tagged->pluck('tag_name')->toArray() )):''}}">
</div>
I solved my problem, using directive #php.
<textarea id="description" name="description" rows="3">#php
if(old('description')) {
echo old('description');
} elseif (isset($post)){
echo $post->description;
}
#endphp</textarea>
No more extra whitespaces.

Display textarea value in Laravel

I have posts. When I click edit it takes me to the edit screen and display current post values. However for some reason the input is displayed while textarea is not. I can't understand why.
Works perfectly, displays value.
<input class = "form-control" type="text" name = 'body' value = "{{ old('body', $post->body ?? null) }}" />
It doesn't display value for body.
<textarea class="form-control" type="text" name = 'body' rows="3" value = "{{ old('body', $post->body ?? null) }}" ></textarea>
input is self closing tag that's why in input you can set value via value=
and in textarea there is closing tag that's why in textarea you can't set value via value attribute
<textarea class="form-control" type="text" name = 'body' rows="3"> {{ old('body', $post->body) }}</textarea>
The textarea tag has no value, but work fine with handlebars.it has placeholder attribute but here it not work like this https://html.spec.whatwg.org/multipage/forms.html#attr-textarea-placeholder
So just give the value before close tag
<textarea class="form-control" type="text" name = 'body' rows="3" >{{ old('body', $post->body ?? null) }}</textarea>
****Display textarea value in Laravel**
<div class="form-group col-md-6">
<label class="control-label">Vendor Description</label>
<textarea name="dec" class="form-control" rows="3" value="{{old('dec')}}"></textarea> {!! $errors->first('dec', '
<p class="text-danger errorBag">:message</p>') !!}
<label class="control-label">Last Name</label>
<input type="text" name="l_name" class="form-control
" value="{{old('l_name')}}"> {!! $errors->first('l_name', '
<p class="text-danger errorBag">:message</p>') !!}
</div>

How to get checkbox value in a Laravel controller?

I have a checkbox in my form and I want to save its value into the database. When I check it, its value will be 1 and when I uncheck it then it's value will be 0. However, in the controller, I am getting NULL all the time. But why? Is it possible to solve without jQuery?
Blade/View
<form action="{{ route('admin.categories.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group>
<label>
<input type="checkbox" name="category_is_menu"
value="{{ old('category_is_menu', isset($category) ? 'checked' : '') }}"/>
</label>
</div>
<input class="btn btn-success" type="submit" value="Submit">
</form>
Controller
public function store(Request $request)
{
$category = new Category();
$category->category_is_menu = $request->category_is_menu;
return $category;
}
Unfortunately, it's giving me NULL for category_is_menu.
Add a hidden input with the negative value just before the checkbox input (with the same value in the name attribute)
<div class="form-group">
<input type="hidden" name="category_is_menu" value="0"/>
<input type="checkbox" name="category_is_menu" value="1" {{ old('category_is_menu', isset($category) ? 'checked' : '') }}/>
</div>
The value in the checkbox input needs to be always 1.
A checkbox input in not sent in the request when it's unchecked, in that case the hidden input will be sent with the value 0.
When the Checkox is checked, it will overwrite the value to 1.
Either use the hidden input trick as mentioned in the other answers or just check if the input was passed at all. If it was passed at all it was checked, if its not there it was not checked:
$category->category_is_menu = $request->has('category_is_menu');
If it was checked you get true, 1, if it wasn't checked you get false, 0.

Make input field display form text

I want users to be able to read and edit their 'About me' text. So far, all works fine, the updated text populates correctly on their profile page. Awesome.
However, I can't get the text to remain in the textform field, which is annoying.
Here's an example of when it does work:
I enable users to update their 'Name' and the name remains viewable in the form.
label for="first_name" class="col-md-4 col-form-label text-md-right">{{ __('First Name') }}</label>
Now, I know that textareas work differently, and I also have a working example where I enable users to update their 'Group Description' and, again, the textarea remains viewable in the form.
<div class="form-group">
<label for="group_description">Group Description:</label>
<textarea class="form-control" rows="5" name="group_description">{{ $group->group_description }}</textarea>
</div>
However, I can't get the 'About me' text to display the textarea?
Here's what I have:
<textarea class="form-control" rows="5" name="about_me">{{ $user->about_me) }}</textarea>
#error('about_me')
I get an error on submission: syntax error, unexpected ')', expecting ',' or ';' (View: -->
The good news is, I can use the below code which does update the 'About me' text, but it will not show the text in the textarea as I need:
<textarea class="form-control #error('about_me') is-invalid #enderror" value="{{ $user->about_me }}" id="about_me" type="text" rows="5" name="about_me" autofocus></textarea>
Theres a wrong format to initialize textarea to display text on it! Here's the code!
<textarea class="form-control #error('about_me') is-invalid #enderror" id="about_me" rows="5" name="about_me" autofocus>{{ $user->about_me }}</textarea>

Custom File upload with other form fields using dropzone js and laravel

This is the first time I'm using dropzone js. I've following requirement.
My form has following fields
Name
Category
Description
Images
Here Name is input field, Category is selector, Description is Textfield and Images is multiple file upload field.
{{ csrf_field() }}
<div class="form-group">
<label for="name" class="col-form-label">Name</label>
<div>
<input type="text" name="name" class="form-control"
required placeholder="Enter Name of the Package" value="#if(isset($package)) {{$package->name}} #else {{old('name')}}#endif" />
</div>
</div>
<div class="form-group">
<label for="category_id" class="col-form-label">Select</label>
<select class="form-control" name="category_id" required>
<option value="">Select Category</option>
#foreach($categories as $category )
<option value="{{$category->id}}"
#if(isset($package))
#if($package->category_id==$category->id)
selected
#endif
#else
#if(old('category_id') == $category->id)
selected
#endif
#endif
>{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label>Description</label>
<textarea name="description" class="form-control" required placeholder="Type something">{{isset($package) ? $package->description:old('description')}}</textarea>
</div>
<div class="form-group">
//here I need multiple file upload field with dropzone js
</div>
My problem is files are not attached when form is submitted as normal post request. Is there any way to make such custom form in laravel.
I've found a lot of solution with form having image upload field only but i didn't find solution for my case.
If you need further information, feel free to ask.
Thanks,
You could try using the .getAcceptedFiles() or similar to get the files that are uploaded and then pass that info in an ajax post or get.

Resources