Question

I am having trouble getting a dynamic form to properly load. I have a form that a user can click a button and add input fields to their hearts content. I have been able to json_encode and store the information but when I try to edit the form Laravel only creates and fills in the default inputs, leaving out the input fields and data created by the user. How can I get Laravel's form model-binding to display the additional fields and information? Or am I just form model-binding incorrectly? The user can push "Add Another Fence" and another copy of the form is added to the bottom. So, in this case (I didn't include the entire form because it is very long), they could press the "Add Another Fence" button and another input with class "gateNumber" and a newly created id of "gateNumber_2" would appear at the bottom. If anyone knows how to get the dynamic data to bind and display or point out where I am going wrong it will be greatly appreciated! Thank you so much!

Here is my Routes for the edit:

/*Edit Orders (GET)*/
Route::get('orders/{orders}/edit', array(
    'as'    => 'order-edit',
    'uses'  => 'OrderController@getEdit'
));
/*Edit Order (POST)*/
    Route::post('/orders/{orders}/edit', array(
    'as'    => 'order-edit-post',
        'uses'  => 'OrderController@postEdit',
    ));

View (or at least a snippet, the form is very long):

@extends('layout.main')
@section('content')
{{ Form::model($order, array('route'=>array('order-edit-post', $order->id), 'name', '=', 'orderForm', 'id', '=', 'orderForm')) }}
 <fieldset id="customerInfo">
     {{ Form::label('order_name', 'Order Name:')}}
     {{ Form::text('order_name') }}
        @if($errors->has('order_name'))
    {{ $errors->first('order_name') }}
    @endif
<legend><strong>Gate Information</strong></legend>
    {{ Form::select('gateNumber_1',
        array(
            'select'    => 'Gates Needed',
            '0'         => '0',
            '1'         => '1',
            '2'         => '2',
            '3'         => '3',
    ), null,
    array('class'       => 'gateNumber')) }}
<div>
    <input type="button" id="btnAddFence" value="Add Another Fence" />
    <input type="button" id="btnDelFence" value="Remove Fence" />
</div>  

And Order Controller:

public function getEdit($id){
        $order = Order::where('id', '=', $id);

        if($order->count()) {
            $order = $order->first();
            $order->order_serialized = json_decode($order->order_serialized);

            foreach($order->order_serialized as $key => $value){
                $order->$key = $value;
            }
            return View::make('orders.edit')
                    ->with('order', $order);
        }   else {
            return App::abort(404);
        }
    }

    public function postEdit($id){
        $validator = Validator::make(Input::all(),
            array(
                'order_name'            =>'required',
                'phone'                 =>'required|min:10|numeric',
                'email'                 =>'required',
                'st_address'            =>'required',
                'post_date'             =>'required',
            )           
        );

        if($validator->fails()){
            return Redirect::route('order-edit', $id)
                    ->withErrors($validator)
                    ->withInput()
                    ->with('global', 'You done messed up.');
        }   else {
            /*Edit Order*/
            $order                      = Order::find($id);

            $order->order_name          = Input::get('order_name');
            $order->phone               = Input::get('phone');
            $order->email               = Input::get('email');
            $order->st_address          = Input::get('st_address');
            $order->post_date           = Input::get('post_date');
            $order->order_serialized    = json_encode(Input::except('_token', 'submit'));

            if($order->save()){
                return Redirect::route('home')
                        ->with('global', 'Your order has been edited.');
            }
        }

        return Redirect::route('home')
                ->with('global', 'Your order could not be edited.');

    }
Was it helpful?

Solution

Laravel binds data to your form when you use:

{{ Form::model($order, ...) }}

and

{{ Form::text('order_name') }}

But inside your javascript, you're not using the latter to create those new fields.

Anyway, this is something you could not do via Javascript, you have to execute it in PHP (Laravel). But you can, via ajax, gather this information from your database.

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