Old input for array? - laravel

I have foreach and inside foreach i have input like this:
<input type="text" id="text-title" name="article_title[{{$language->code}}]" value="{{ old('article_title[$language->code]') }}" class="form_input" />
Im trying to add old input like this but its not working. Any suggestion how can i use old for array?
Also i tried this but also not working:
<input type="text" id="text-title" name="article_title[{{$language->code}}]" value="{{ old('article_title.0') }}" class="form_input" />
I have validation only for first iteration if that means anything for you.

You should be able to access the index off of your language code:
value="{{ old('article_title.'.$language_code) }}"

you can use the old to return old value as an array
# old('article_title') => array
value="{{ old('article_title')[$language_code] ?? "" }}"

Related

How to avoid livewire request for simple inputs calculations?

How would I avoid doing calculation for 3 inputs using Livewire and use JS instead, but still can bind the inputs and their new values with the component.
Example:
<input id="cash-price"
type="text"
wire:model="total_cache_price"
class="amount-credit">
<input id="deposit"
type="text"
wire:model="deposit"
class="amount-credit">
<input id="trade-in"
type="text"
wire:model="trade_in"
class="amount-credit">
I can easily do a simple calculation using JS, but the properties in Livewire component would still be empty or null after submitting the form. I am trying to avoid livewire requests for every input change.
Note: I understand the deferred updating in livewire, the problem is with the property values not changing.
I will show you an example in alpine js + livewire way.
Here I want to set value in two inputs from a selected option in select.
Please note that x-ref is used in alpine to directly access DOM elements without using getElementById.
<div x-data="{
from_date : #entangle('from_date').defer,
to_date : #entangle('to_date').defer,
dateChange(){
select = this.$refs.years;
this.from_date = select.options[select.selectedIndex].getAttribute('data-from');
this.to_date = select.options[select.selectedIndex].getAttribute('data-to');
},
resetFilters(){
this.net_balances = false;
}}">
<select x-ref="years" #change="dateChange()">
<option value="">Year</option>
#foreach ($years as $key => $year)
<option wire:key="{{ 'years'.$key }}" data-from="{{ $year->from_date }}" data-to="{{ $year->to_date }}" value="{{ $year->id }}">{{ $year->name }} </option>
#endforeach
</select>
<div>
<text type="date" x-model="from_date" />
</div>
<div >
<text type="date" x-model="to_date" />
</div>

Laravel validator on a form and send post

I used laravel validator.
I have a form wih 2 fields
If i fill one field "NAME" and i leave the other "SURNAME" empty pressing SAVE button of the form appears required alert message on SURNAME but the name inserted in the NAME field became empty! And i think form send data anyway.why?I aspect that form doesn't send datas
In the value attribute of each input you need to echo old("the-input-name") so it can retain the previous input before the error
<input name="name" value="{{old('name')}}" class="" />
You should use old('NAME') to get the value of the field after the validation fails, blade example:
<input type="text" value="{{ old('NAME') }}" />
An example In case of update forms you should display the original value from the database unless there's an error:
<input type="text" value="{{ old('NAME') ?? $your_entry->value }}" />
Take a look at the documentation for more details

Edit Form (Laravel 5.3)

I am using same form for Create and Edit i have a Folder field and i want to set its value Inbox by default however if a user want to replace it with own folder he /she can replace
<input type="text" name="folder" value="INBOX {{isset($mbl->folder) ? $mbl->folder : '' }}" class="form-control" />
when i go on edit it shown **INBOX INBOX
I want that Default value should not be show in Edit form it just show the name from Folder remember that i can't remove value="INBOX" because i need it i am using it as default value and i am using same form for Create and Edit
Thanks For Help
You Need to pass default value as
<input type="text" name="folder" value="{{isset($mbl->folder) ? $mbl->folder : 'INBOX' }}" class="form-control" />
Try This hope it will work
Simply use it as follows:
<input type="text" name="folder" value="{{$mbl->folder or 'INBOX' }}" class="form-control" />
More info here.

How to populate edit form fields in laravel 5.3

I have a problem in populating edit form fields in my first Laravel 5.3 application. In examples I have found online, they have used Form facade to do it. However, I prefer to use plain html.
This is my controller action
public function edit()
{
$profile = Profile::find(Auth::user()->id);
return view('profile.edit', ['profile' => $profile]);
}
In my view file I have a form like this
<form method="POST" action="{{ url('profile/update') }}">
{{ csrf_field() }}
<input id="name" type="text" name="name" value="{{ old('name') }}">
...
</form>
Problem is, above input field is not populated with the value of the $profile->name.
I can get it to work, setting the value attribute like
{{ (old('name')) ? old('name') : $profile->name }}
Since I'm new to Laravel framework I want to know if there is a better way to do that.
Use old($key, $default). It will return $default if $key is not found.

What is the best practice to show old value when editing a form in Laravel? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
I am writing the logic for an edit form and have some complications when displaying data in the inputs.
When I initially show the form, I show the records values like:
value="{{$dog->title}}"
Then when the form does not pass the validation I need to show the old input, so that user does not loose what he has already input. So I need to have a way to display old data like:
value="{{old('title')}}"
Because I need to input old data in case it exists, I ended up with this code:
value="{{$dog->title or old('title')}}"
And in controller I check if Request has old input, I assign the $dog var a null value.
I wanted to ask if that is considered an OK practice or is there a better and 'correct' way to do it?
Function old have default parameter if no old data found in session.
function old($key = null, $default = null)
You can replace expression in template with
value="{{old('title', $dog->title)}}"
I know this has already been answered but I thought I would leave a little snippet here for others in the future.
Setting old value on input as #ikurcubic posted can be used the same way on radio button or select:
<input type="text" name="name" value="{{ old('name', $DB->default-value) }}" />
Select option:
<option value="Jeff" {{ old('name', $DB->default-value) == 'Jeff' ? 'selected' : '' }}>Jeff</option>
Radio Button:
<input type="radio" name="gender" value="M" {{ old('name', $DB->default-value)== "M" ? 'checked' : '' }} />
Another way of doing it; write a small if statement to determine which value should be evaluated:
#php
if(old('name') !== null){
$option = old('name');
}
else{ $option = $database->value; }
#endphp
<select name="name">
<option value="Bob" {{ $option == 'Bob' ? 'selected' : '' }}>Bob</option>
<option value="Jeff" {{ $option == 'Jeff' ? 'selected' : '' }}>Jeff</option>
</select>
<input type="radio" name="gender" value="M" {{ $option == "M" ? 'checked' : '' }} />
<input type="radio" name="gender" value="F" {{ $option == "F" ? 'checked' : '' }} />
Setting old value of input with an array name e.g name="name[]":
<input type="text" name="name[]" value="{{ old('name.0) }}" />
This will give you the old value of the input with an index of 0.
I have tested this and it works.
Another way to do that is get the data from the dog class, like this:
value="{{old('title') ?? $dog->title }}"
Why? Because old() is for validation; when you fail validation, the input will remain available in the field. In the case where validation hasn't fired yet, value will be filled with $dog->title.
I solved that issue in my application
if you want to get old previous inserted value in input you should try this one
I did like this.
function SaveData(Request $request)
{
$sValidationRules = [
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required',
'phone' => 'required',
];
$validator = Validator::make($request->all(), $sValidationRules);
if ($validator->fails()) // on validator found any error
{
// pass validator object in withErrors method & also withInput it should be null by default
return redirect('Your Route Hare')->withErrors($validator)->withInput();
}
Employee::create($request->all());
return redirect(' your route hare ');
}
& after then get old data in input field value like this using {{ Request::old('firstname') }}
Happy Coding. :)
<input type="text" name="firstname" id="firstname" value="{{ Request::old('firstname') }}" class="form-control" placeholder="First Name">
Old Data: input type text | phone | number | ...
<input type="text" name="my_text" value="{{old('my_text')}}">
Old Data: input type checkbox | radio
<input type="checkbox" {{ old('my_checkbox') == 'on' ? 'checked' : '' }} name="my_checkbox">
There is nothing wrong with the way you are doing things as Laravel gives multiple ways to handle the situation you're describing.
What I would suggest is using the Laravel Collective Form and HTML packages to build your form. This package will automatically handle binding old request values to your form if validation fails
https://laravelcollective.com/docs/5.2/html

Resources