Clean way to inject old input

Published 1 year ago by BrianDillingham

This, to me atleast, feels like a clean way to have edit & create views share fields

Shared Input

<input type="text" name="first_name" value="{{ old('first_name', $contact->first_name) }}">

Create & Edit Method - Because $contact isn't defined in create it throws an exception.

Any opinions / thoughts on adding an empty model, making the default value of old() = empty.

public function create()
{
        return view('contact.create')->withContact(new Contact);
}

public function edit($id)
{
        $contact = Contact::find($id);

        return view('contact.edit', compact('contact'));
}
veve286
veve286
1 year ago (37,655 XP)

<?php $firstName=isset($contact) ? $contact->first_name : '' ?> <input type="text" value="{{ old('first_name',$firstName )}}" >

It will solve throw errors.

gamxindev

try this ....for create pass a false value for $contact

$contact=false;

from the controller to the view and in the view do the following mods

<input type="text" name="first_name" @if($contact)value="{{ old('first_name', $contact->first_name) }}"@endif>
Devmaurice

I use laravel form collection is much easier.

@if(isset($contact))
{!! Form::model($contact, ['route' => ['contact.update', $contact->id], 'class' => 'form-horizontal']) !!}
@else
{!! Form::open(['method' => 'POST', 'url' => '/contact', 'class' => 'form-horizontal']) !!}
@endif

//include form fields here

{!! Form::close() !!}
Snapey
Snapey
11 months ago (409,585 XP)

I pass a new model to the edit view. Works fine.

I also test it to include different versions of the form tag

eg;

@if($supplier->exists)
    <form class="form-horizontal" role="form" method="POST" action="/admin/supplier/{{ $supplier->id }}">
    {{ method_field('PATCH') }}
@else
    <form class="form-horizontal" role="form" method="POST" action="/admin/supplier">
@endif
leeovery

Personally I do this...

<input type="hidden"
          value="{{ isset($listing)
                ? $listing->location()->latitude()
                : old('location.lat') }}"
>
kzorluoglu

I use so;

 <input type="text" name="slug" value="{{ old('slug',  isset($category->slug) ? $category->slug : null) }}" class="form-control">
dharav

can anyone provide code in contact.edit file so that it renders createforam

Snapey
Snapey
6 months ago (409,585 XP)

@dharav not quite sure what you are asking, but I'll give it a shot.

Suppose your edit method in the controller is like this;

public function edit(Request $request, Speaker $speaker)
{
    return view('speaker.edit')->with(compact('speaker'));
}

Then the model being edited is passed to the view.

In the create method of the controller, you can do this;

public function create()
{
    $speaker = new Speaker;
    return view('speaker.edit')->with(compact('speaker'));
}

Both methods use the same view...

Then in the view;

// the form tag needs to be different between create and edit

                @if($speaker->exists)
                        <form method="post" action="/speaker/{{ $speaker->id }}">
                @else
                        <form method="post" action="/speaker/store">
                @endif 

//the input fields can use the old() method to show the model or the previous submission

        <input name="subject" value="{{ old('subject', $speaker->subject) }}" />

So, a single form can edit or create

mazinoukah

how do i handle the file input field when editing ?

Snapey
Snapey
4 months ago (409,585 XP)

@mazinoukah

how do i handle the file input field when editing?

You cannot. You have to ask for the file again.

willvincent

Not entirely true.. you can't use an 'old' value to populate file input field, that's true.

However, since @mazinoukah's question seems to be about editing, not fixing validation errors and such, you could display the filename and maybe a thumbnail if it's an image, with a 'remove' option (you'd have to implement that functionality), on an edit form.. or skip the remove option and provide a file field so that if a new file is uploaded the previous one is replaced with the new upload... depends how you want it to work really.

mazinoukah

@Snapey Thanks i discovered it was not possible so i implemented what @willvincent suggested

Sign In or create a forum account to participate in this discussion.