Fateh Alrabeai
Fateh Alrabeai

Reputation: 730

How to check if value is null and use old( ) function to keep data after validation page refresh?

I perform the insert and update functionality using the same inputs. I have the following files: create.blade.php, form_partial.blade.php and edit.blade.php. Where the file form_partial.blade.php contains the inputs and included in both create.blade.php and edit.blade.php.

My sample input to check if there's data or not and if there's no data after validation return old('title') :

<input type="text" value="{{old('title')}}{{isset($news) ? $news->title: ''}}" name="title"
                    class="form-control" id="title" placeholder="News Title">

My problem ?

The problem with my code (in the input value) is that when I attempt to edit a certain data and validation error runs and refreshes the page, the data in the inputs gets doubled with (data from the session and the retrieved data from the database). For example if the value retrieved was "Hello" after validation error it becomes: "hellohello".

Now how can I keep the data stored in the session in inputs after validation runs when storing new data and avoid the data getting doubled when validation refreshes the page during updating data ?

Upvotes: 1

Views: 487

Answers (3)

Fateh Alrabeai
Fateh Alrabeai

Reputation: 730

I found the answer and it was very easy :

{{old('title', $news->title)}}

Upvotes: 2

Md Azizur Rahman
Md Azizur Rahman

Reputation: 375

<input value="{{ old('name') }}" name="name" type="text" id="category_name" class="form-control">

Upvotes: 0

enbermudas
enbermudas

Reputation: 1615

I think that your value attribute should be modified like this:

{{ isset($news) ? $news->title : old('title') ? old('title') : '' }}

Either way you will always get that old value on a validation error.

Upvotes: 0

Related Questions