Search code examples
phplaravel-5.1laravel-bladelaravel-form

Form model data not running correctly on @yield Laravel 5


all I want is to define the form model on template and then put a yield as the content of form data. But it cannot assign the model data correctly into each field that has been defined. This is my code:

template.detail.blade.php

@extends('admin.template.lte.layout.basic')

@section('content-page')
    {!! Form::model($model, ['url' => $formAction]) !!}
        @yield('data-form')
    {!! Form::close() !!}
    @if ($errors->any())
@stop

partial.edit.blade.php

@extends('template.detail')
@section('data-form')
    <div class="form-group">
        {!! Form::label('Dal_Name', 'Alternative Name', ['class' => 'required']) !!}
        {!! Form::text('Dal_Name', null, ['required', 'class' => 'form-control', 'placeholder' => 'Enter Alternative Name']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('Dal_DssID', 'DSS Period', ['class' => 'required']) !!}
        {!! Form::select('Dal_DssID', $dssOptions, null, ['class' => 'form-control']) !!}
    </div>

    <div class="checkbox">
        <label for="Dal_Active">
            {!! Form::hidden('Dal_Active', 'N') !!}
            {!! Form::checkbox('Dal_Active', 'Y') !!}
            Active
        </label>
    </div>
@stop 

My Controller part:

     /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $this->data['model'] = DssAlternative::find($id);
        $this->data['formAction'] = \Request::current();
        $this->data['dssOptions'] = Dss::lists('Dss_Name', 'Dss_ID');
        return view('partial.edit', $this->data);
    }

But the model data does not propagate to form correctly. Sorry for my bad english.


Solution

  • It won't work since you are passing the $model object to partial/edit.blade.php file, and hoping to use it within template/detail.blade.php

    exactly at this line {!! Form::model($model, ['url' => $formAction]) !!}

    Solution: in template/detail.blade.php Take the form model line inside like this:

    @extends('admin.template.lte.layout.basic')
    
    @section('content-page')
        @yield('data-form')
        @if ($errors->any())
    @stop
    

    So partial/edit.blade.php would be like below:

    @extends('template.detail')
    @section('data-form')
    {!! Form::model($model, ['url' => $formAction]) !!}        
        <div class="form-group">
            {!! Form::label('Dal_Name', 'Alternative Name', ['class' => 'required']) !!}
            {!! Form::text('Dal_Name', null, ['required', 'class' => 'form-control', 'placeholder' => 'Enter Alternative Name']) !!}
        </div>
    
        <div class="form-group">
            {!! Form::label('Dal_DssID', 'DSS Period', ['class' => 'required']) !!}
            {!! Form::select('Dal_DssID', $dssOptions, null, ['class' => 'form-control']) !!}
        </div>
    
        <div class="checkbox">
            <label for="Dal_Active">
                {!! Form::hidden('Dal_Active', 'N') !!}
                {!! Form::checkbox('Dal_Active', 'Y') !!}
                Active
            </label>
        </div>
    {!! Form::close() !!}
    @stop