Rosh
Rosh

Reputation: 491

How to maintain the old input value in checkbox after validating by an update request using laravel?

when i open the edit page it should have the value from the database and when i change the value and if in case validaton fails, i need the {{old('input')}} value to persist. This is the code :

<input type="checkbox" id="show_in_website" name="show_in_website"  @if($data->show_in_website=='1') checked @endif>Show in Website
       

Upvotes: 0

Views: 1061

Answers (1)

OMi Shah
OMi Shah

Reputation: 6186

You can do using the old() helper method. The old() helper method supports retrieving any old form value on validation failure. Also, it allows passing a default value on empty/null which means we can pass a value from our database as default when the edit page is initially called.

<input type="checkbox" id="show_in_website" name="show_in_website" {{ old('show_in_website', $data->show_in_website) ? 'checked' : '' }}> Show in Website

also, make sure you are redirecting back to the form with withInput() as:

if($validator->fails()) {
    return back()->withErrors($validator)->withInput();
} 

Upvotes: 3

Related Questions