elliotk
128
4
Laravel

Form for Create and Update - Duplication

Posted 1 year ago by elliotk

Hello,

I have a form for Create and a form for Update/Edit. It seems to me, that most of the logic is the same. Yet I have a lot of repetition of HTML.

The only differences I see are

  1. The Form Action - Create and Update have different URLs
  2. The Value on Form Inputs are populated differently.

Create <input type="text" class="form-control {{ $errors->has('title') ? ' is-invalid' : '' }}" name="title" id="title" value="{{ old('title') }}" placeholder="Title" required>

Update <input type="text" class="form-control {{ $errors->has('title') ? ' is-invalid' : '' }}" name="title" id="title" value="{{ $post->title }}" placeholder="Title" required>

The rest is pretty much the same. Right now, I have 2 files, with 90% the same code, I'd really like to have one file, so it's more maintainable.

I'd be really interested to hear how others handle this.

Thanks

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.