bootforms icon indicating copy to clipboard operation
bootforms copied to clipboard

Feature suggestion: Allow form binding from multiple models

Open GregPeden opened this issue 8 years ago • 0 comments

I have some forms where the user is entering data destined to two separate Model objects. In particular, an Address model is used throughout my code, being bound to various other parent Models via Laravel's polymorphic relationship. Since the code repeats everywhere, I have a Blade template for the Address portion of the form, so that form partial can't make reference to any parent model by name, so I feed it the Address model directly. I using a small trick to allow binding, by calling BootForm::bind() twice, once before each form partial.

Here is an example from one update form for a model (note in this case providing an Address is optional and might not exist, hence the conditional check)

    <div class="container">
        {!! BootForm::open()->put()->action(route('clients.update', $client)) !!}
        {!! BootForm::bind($client) !!}
        @include('clients.partials.form')
        @if(isset($address))
        {!! BootForm::bind($address) !!}
        @endif
        <strong>Address</strong>
        <p class="help-block">Provide corporate headquarters address when known, otherwise leave blank</p>
        <div class="section-border">
            @include('places.addresses.partials.form')
        </div>
        {!! BootForm::submit('Submit')->class('btn btn-primary') !!}
        {!! BootForm::close() !!}
    </div>

Here's a similar example from another form (here Address always exists):

    <div class="container">
        {!! BootForm::open()->put()->action(route('offices.update', $office)) !!}
        {!! BootForm::bind($office) !!}
        @include('offices.partials.form')
        {!! BootForm::bind($address) !!}
        <strong>Address</strong>
        <p class="help-block">Required</p>
        <div class="section-border">
            @include('places.addresses.partials.form')
        </div>
        {!! BootForm::submit('Submit')->class('btn btn-primary') !!}
        {!! BootForm::close() !!}
    </div>

Doing this, it just occurs to me that it would be swell and simpler if one could pass multiple models to the bind() method. I am aware that any attributes with the same name would intersect, and I think only the latter would be picked up. Still, it could be useful! In particular, the above solution obligates me to keep the two models in separated segregated sections in my form, whereas someone might want to mix child model fields in the middle of their form inside another model.

GregPeden avatar Jun 09 '16 20:06 GregPeden