kristijanhusak / laravel-form-builder

Laravel Form builder for version 5+!
https://packagist.org/packages/kris/laravel-form-builder
MIT License
1.71k stars 296 forks source link

Remove field from Collection #365

Open uovidiu opened 7 years ago

uovidiu commented 7 years ago

How can I remove a field from AddressForm?

Here is my structure

class ClientForm extends Form
{
    public function buildForm()
    {
        // Add fields here...

        $this

            ->add('company_name', 'text', [
                'rules' => 'required|min:5',
            ])

            ->add('first_name', 'text', [
                'rules' => 'required',
            ])
            ->add('last_name', 'text', [
                'rules' => 'required'
            ])
            ->add('service_charge_rate', 'number', [
                'rules' => 'required'
            ])
            ->add('late_fee_amount', 'number',[
                'rules' => 'required'
            ])
            ->add('minimum_balance', 'number',[
//              'rules' => 'required'
            ])

            ->add('addresses', 'collection', [
                'type' => 'form',
                'property' => 'id',
                'prototype' => true,
                'wrapper' => ['class' => 'form-group grid-3_xs-1'],
                'options' => [    // these are options for a single type
                    'class' => AddressForm::class,
                    'label' => false,
                    'wrapper' => array('class'=> 'col'),
                ],
                'label_show' => false,
            ])
        ;

        $this->add('submit', 'submit', [
            'wrapper' => [ 'class' => 'clearfix'],
            'attr' => [
                'class' => 'btn btn-primary btn-block'
            ]
        ]);

    }
}

AddressForm.php

use Kris\LaravelFormBuilder\Form;
use Webpatser\Countries\Countries;
use Lecturize\Addresses\Models\Address;

class AddressForm extends Form
{
    public function buildForm()
    {
        // Add fields here...

        $this
            ->add('street', 'text', [
                'rules' => 'required',
            ])

            ->add('city', 'text', [
                'rules' => 'required',
            ])

            ->add('state', 'text', [
                'rules' => 'required',
            ])

            ->add('post_code', 'text', [
                'rules' => 'required',
            ])

        ;
    }
}

I've tried $form->addresses->removeChildren('post_code') but doesn't work.

Is it possible? Because on a child element of type 'form' works without a problem.

kristijanhusak commented 7 years ago

@uovidiu since collection is array of forms, you need to provide the index of the field that you want to remove. So if you need to remove 2nd entry, something like this should work:

$form->addresses->removeChild(1);

Also note that i used removeChild, since removeChildren does not exist.

uovidiu commented 7 years ago

Still doesn't work. I get that I need to pass the index. $form->addresses->removeChild(0); result in an empty form.

Everything else than 0 doesn't do anything.