Question

I have a number of tables I want to product forms for. For the sake of convenience, I want them to share as much code as possible, so I plan on using the following blade structure:

/views/forms/template.blade.php

/views/forms/table1/edit.blade.php
/views/forms/table1/create.blade.php
/views/forms/table1/partials/_fields.blade.php

/views/forms/table2/edit.blade.php
/views/forms/table2/create.blade.php
/views/forms/table2/partials/_fields.blade.php

The controller which uses these views, does the following:

return View::make('forms.'.$viewForm.'.edit')->with('data', $data)
                                         ->with('viewForm', $viewForm) 

$viewForm will either contain 'table1' or 'table2' in our example. $data will be populated with the current record or an empty record.

If I create the following file:

/views/forms/table1/edit.blade.php
{{ dd($data); }}

Then I get the correct output, ie a rather messy representation of the data.

If I replace that with the following code:

@extends('forms.template')
@section('formcontent')

<div class='formfield'>
{{ Form::label('table1_field1','Code') }}
{{ Form::input('text','table1_field1') }}
{{ $errors->first('table1_field1') }}
</div>

@stop

And create the template file as so:

/views/forms/template.blade.php

<h1>Edit</h1>
{{ Form::model($data,['method'=>'PATCH', 'url'=>route('table').'/'.$viewForm]) }}

@yield("formcontent")

<div class='formfield'>
{{ Form::submit('Edit',['class'=>'button']) }}
</div>
{{ Form::close() }}

Then I get an empty field displayed, but no data. However, if I move the following

<div class='formfield'>
{{ Form::label('table1_field1','Code') }}
{{ Form::input('text','table1_field1') }}
{{ $errors->first('table1_field1') }}
</div>

from /table1/edit.blade.php to template.blade.php, then it contains data.

I can get this working by moving the {{ form }} to the edit.blade.php, but that is precisely what I am trying to avoid. Is there any way I can create the form in template.blade.php as I want to?

Was it helpful?

Solution

If I replace edit.blade.php with the following code:

<div class='formfield'>
{{ Form::label('table1_field1','Code') }}
{{ Form::input('text','table1_field1') }}
{{ $errors->first('table1_field1') }}
</div>

And change the template file to:

/views/forms/template.blade.php

<h1>Edit</h1>
{{ Form::model($data,['method'=>'PATCH', 'url'=>route('table').'/'.$viewForm]) }}

@include("/views/forms/".$viewForm."/edit")

<div class='formfield'>
{{ Form::submit('Edit',['class'=>'button']) }}
</div>
{{ Form::close() }}

Finally, change the route to:

return View::make('forms.template')->with('data', $data)
                                   ->with('viewForm', $viewForm) 

Then it works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top