Extra whitespaces in blade statements. Laravel 8 - laravel

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.

Related

Thymeleaf table

I have problem table when i want set value with for each i.index get error java.lang.NumberFormatException: For input string: "${i.index}". In Array i need numer so ${i.index} is int. I dont know what i do wrong.
<div class="form-group row" th:each="attribute, i: ${attributeList}">
<label class="col-sm-3 col-form-label" th:text="${attribute.name}"></label>
<div class="col-sm-9">
<input type="text" th:field="*{technicalAttributes[${i.index}].name}" class="form-
control" placeholder="Nazwa">
</div>
</div>
You can't nest expressions (*{...${...}...}) like you're doing without using preprocessing. Your code should look like this:
<div class="form-group row" th:each="attribute, i: ${attributeList}">
<label class="col-sm-3 col-form-label" th:text="${attribute.name}"></label>
<div class="col-sm-9">
<input type="text" th:field="*{technicalAttributes[__${i.index}__].name}" class="form-control" placeholder="Nazwa">
</div>
</div>
(If you weren't using the th:field attribute, the expression *{technicalAttributes[i.index].name} would also be appropriate. But since you are using a th:field you have to use preprocessing.)

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>

Laravel Edit Post Content using CKeditor

I am trying to edit existing post using CKeditor. Content isn't loading.
<div class="form-group">
<label>Content</label>
<textarea id="editor1" name="content" class="form-control" rows="3" placeholder="Enter ...">
{{ Request::old('content') }}
</textarea>
</div>
Also im having trouble getting date from the database too.
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" name="published_at" class="form-control" value="{{ $post->published_at->format('M jS Y') }}">
</div>
For the textarea, check for old input, but fall back to defaulting to using the model's content attribute (the post's content)
<textarea ....>{!! Request::old('content', $post->content) !!}</textarea>
For your date issue, I don't know what problem you are having.

Laravel: Input old for date field

I want to have my date field remember my old input
Is there any way i can do this?
Here's my code
<div class="form-group">
<label for="date1">Date From: </label>
<input id="date1" name="date1" type="date" value="<?php echo date('Y-m-d'); ?>" class="form-control">
</div>
<div class="form-group">
<label for="date2">Date To: </label>
<input id="date2" name="date2" type="date" value="<?php echo date('Y-m-d'); ?>" class="form-control">
</div>
Please Help. Thank You!
If you want to have a default but also remember the user provided input in case of any error you could do something like this:
<input type="date" name="date2" value="{{ old('date2', date('Y-m-d')) }}">
Then in your controller when you redirect back make sure to call the ->withInput()method.
First you need to redirect from your controller "With inputs" something like this:
return redirect('some/form/route')->withInput();
Then in your template:
Request::old('date_field')
Or in a blade template:
{{ old('date_field') }}
More detail in the "Old Input" section in L5 docs

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

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>

Resources