I have a form with 4 input fields, all of them to which I applied an old() function so whenever users load the page after submitting the form, the data that they filled in would retain in the fields for better UX.
My issue is that out of these 4 fields, only 1 has the old() function working. The difference between this field and the others is that:
The name and id attributes for this field are the same, while other fields have different names and IDs respectively.
The input type for this field is type="number" while the other fields are type="text".
Based on these differences, I tried modifying the code to standardize them, and only one solution works which is that I change the input types to "number", however, not all my inputs are numbers, so it is not an ideal solution.
Form blade file:
<div class="form-group mb-3">
<input type="text" class="form-control" name="emp_name" id="company_name"
value="{{ old('emp_name' , (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')) ) }}">
#if ($errors->has('emp_name'))
<span class="text-danger">{{ $errors->first('emp_name') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="number" class="form-control" name="emp_length" id="emp_length"
value="{{ old('emp_length' , (isset($user->emp_detail->emp_length) ? $user->emp_detail->emp_length : old('emp_length')) ) }}">
#if ($errors->has('emp_length'))
<span class="text-danger">{{ $errors->first('emp_length') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="text" class="form-control money-prefix" name="emp_monthly_gross_salary" id="monthly_salary"
value="{{ old('emp_monthly_gross_salary' , (isset($user->emp_detail->emp_monthly_gross_salary) ? $user->emp_detail->emp_monthly_gross_salary : old('emp_monthly_gross_salary')) ) }}">
#if ($errors->has('emp_monthly_gross_salary'))
<span class="text-danger">{{ $errors->first('emp_monthly_gross_salary') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="text" class="form-control money-prefix" name="monthly_allowance" id="bonus"
value="{{ old('monthly_allowance' , (isset($user->emp_detail->emp_monthly_allowance) ? $user->emp_detail->emp_monthly_allowance : old('monthly_allowance')) ) }}">
#if ($errors->has('monthly_allowance'))
<span class="text-danger">{{ $errors->first('monthly_allowance') }}</span>
#endif
</div>
To be clear, I also have several other forms in which I applied the same old() function, with texts as well as numbers, and they work fine, so I am unsure why the old() function won't work on this form specifically. I also dd() the variables I wanted to output as the old function, so I am sure my controller is already passing the values, my blade just isn't showing it.
Can anyone advise me on this problem?
maybe the issue is the default value, may be set but empty
try to check it with old directly without the default value
ex:
replace
{{ old('emp_name' , (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')) ) }}
with
{{ old('emp_name') }}
if it works fine with you then you need to enhance the default value condition
ie: (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')
Related
I have a basic CRUD structure for a model that works well. Now I want to redirect to the create method and populate the form with a pre-existing model. It would work by a user selecting an id and then I would select that model and redirect to the create page and populate the form.
This is what I have tried so far
$order = Orders::find($id);
$inventory = Inventory::where(['id' => $order->inventory_id])->first()->toArray();
return redirect()->route('backend.inventory.create', $inventory['bag_id'])->withInput($inventory);
In the above example, it finds the order and selects the single inventory item related to it (I have confirmed that it's definitely selecting an inventory item as expected) and redirects to the inventory creation page. However, when using the ->withInput() method this doesn't seem to populate my form with the data as I expected.
How do I pass data to a form using a redirect?
Adding the form code as requested below. This is just one column of the form as its a huge block of code
<div class="form-group row" id="item-name" v-if="type != '3'">
<label for="name" class="col-md-2 col-form-label text-md-right">Item Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required>
#if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
You can use this :
return Redirect::back()->withInput(Input::all());
If you're using Form Request Validation, this is exactly how Laravel will redirect you back with errors and the given input.
Excerpt from \Illuminate\Foundation\Validation\ValidatesRequests:
return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());
try this
<div class="form-group row" id="item-name" v-if="type != '3'">
<label for="name" class="col-md-2 col-form-label text-md-right">Item Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') ?? $inventory['name'] }}" required>
#if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
{{ old('name') ?? ($inventory['name'] ?? '') }} if old data is present it will show old data otherwise $inventory->name which is coming from withInput with is also optional that's why i put ??
ref link https://laravel.com/docs/6.x/requests#old-input
You can use compact() function that creates array containing variables and their values.
$order = Orders::find($id);
$inventory = Inventory::where(['id' => $order->inventory_id])->first()->toArray();
return view('backend.inventory.create', compact('inventory'));
In your view page you can use it like,
<input id="name" type="text" class="form-control" name="name" value="{{ $inventory['name'] }}" required>
I'm using https://github.com/thomastkim/laravel-spam-prevention but I want to display validator messages and old content back to user but Idk if it's possible if names are randomized. What I'm currently doing:
Example of input:
<div class="form-group{{ $errors->has(Defender::get('firstname')) ? ' has-error' : ''}}">
<label for="{{ Defender::get('firstname') }}" class="control-label">First name:</label>
<input type="text" name="{{ Defender::get('firstname') }}" value="{{old(Defender::get('firstname'))}}" class="form-control" id="{{ Defender::get('firstname') }}">
<span class="text-danger"> {{ $errors->first(Defender::get('firstname'))}} </span>
</div>
and in my request there is rule:
Defender::get('firstname') => 'required',
and there is message:
Defender::get('firstname').'.required' => 'You must enter your first name',
But obviously when form gets submited {{ $errors->first(Defender::get('firstname'))}} this won't be same as before... So any idea what I should do here in order to display messages to user and get old input ?
I have created a create form create.blade.php and a edit form edit.blade.php - both form fields are identical.
It quite annoying when I add new fields in the create.blade.php, I also need to add fields in edit.blade.php.
How can I merge into 1 blade file it so it work for both way for creating and editing (view via db).
For example in create.blade.php I have this:
<label>Name</label>
<input type="text" name="name" value="{{ old('name') }}">
#if ($errors->has('name'))
<label class="error">{{ $errors->first('name') }}</label>
#endif
In edit.blade.php
<label>Name</label>
<input type="text" name="name" value="{{ old('name', $category->name) }}">
#if ($errors->has('name'))
<label class="error">{{ $errors->first('name') }}</label>
#endif
Just check if model exists:
{{ old('name', empty($category) ? '' : $category->name) }}
Also, you may want to check if it's create or update form and change form url accordingly:
{{ route(empty($category) ? 'article.create' : 'article.update' ) }}
<input type="text" name="name" value="{{ $category ? $category->name : old('name') }}">
you can use a partial (fancy name for a smaller template) for your form, and pass that partial the data you require.
Using the example of a booking system:
In your create blade file:
#include('bookings._forms', ['booking' => new \App\Booking])
and the edit file ($booking being retrieved on the controller):
#include('bookings._forms', ['booking' => $booking])
And then your form partial can look like this:
<label for="name" class="label">Name</label>
<input type="text"
class="input"
name="name" id="name"
placeholder="Guest name"
value="{{ old('name') ?? $booking->name }}">
The good thing with this method is that now you can have ONLY your form elements on the partial and only have to update 1 file when you add fields, AND the actual tag stays on your create and edit template, so no worries about that either.
Hope that's clear and it helps.
I am doing project in laravel. I want edit form with database table values.
In controller,
$event_note = DB::table('event_note')->where('event_id',$id)->first();
I have blade.php file for particular textarea as,
<div class="form-group clearfix{{ $errors->has('event_note') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Note</label>
<div class="col-md-6">
<textarea class="form-control" name="event_note" rows="4" cols="50">{{ $event_note->note }}</textarea>
#if ($errors->has('event_note'))
<span class="help-block">
<strong>{{ $errors->first('event_note') }}</strong>
</span>
#endif
</div>
</div>
but as event_note doesn't have any record it throws an error as,
Trying to get property of non-object (View:....
for variable $event_note->note.
I don't know how to edit this form if database table dont have any value it should show an empty textarea.
You can use or '' clause:
{{ $event_note->note or '' }}
It's a Blade shortcut for this solution:
{{ isset($event_note->note) ? $event_note->note : '' }}
"first()" will return "null" if nothing is found in database for this id.
Try this:
<textarea class="form-control" name="event_note" rows="4" cols="50">{{ is_null($event_note) ? null : $event_note->note }}</textarea>
I am developing a project where I have to store information of a job. For example job_start_date,job_end_date,payment_received_date. It will be nullable initially. As soon as the job starts admin will update job_start_date and job_end_date column. So I cannot set current date and time as default to those field. The problem is, while updating the job_start_date field, if laravel finds that the job_start_date column is null, it throws an error. Call to a member function format() on a non-object. Even though I have declared protected $dates = ['job_start_date','job_end_date','payment_received_date']; in my model. Any suggestion to overcome this problem?
example code:
<!-- job start date -->
<div class="form-group {{ $errors->has('job_start_date') ? 'has-error' : '' }}">
<label class="col-md-4 control-label" for="job_start_date">Job Start Date</label>
<div class="col-md-4">
<input id="job_start_date" name="job_start_date" type="date" value="{{ $job_account_detail->job_start_date->format('Y-m-d') }}" class="form-control input-md">
{!! $errors->first('job_start_date','<span class="help-block">:message</span>') !!}
</div>
</div>
You can check for null value in your view to avoid the error
<input id="job_start_date" name="job_start_date" type="date" value="{{ !is_null($job_account_detail->job_start_date) ? $job_account_detail->job_start_date->format('Y-m-d') : "" }}" class="form-control input-md">
I would add a method in the model to handle validation and formatting to make my life easier:
// in your model
public function getJobStartDateInDateStringFormat() {
return !is_null($this->job_start_date)
? $this->job_start_date->format("Y-m-d")
: "";
}
Then in your view you can just call the method:
<input id="job_start_date" name="job_start_date" type="date" value="{{ $job_account_detail->getJobStartDateInDateStringFormat() }}" class="form-control input-md">