contributte / forms-multiplier

:repeat: Form multiplier & replicator for Nette Framework
https://contributte.org/packages/contributte/forms-multiplier.html
MIT License
26 stars 21 forks source link

Manual rendering example #2

Closed foxycode closed 8 years ago

foxycode commented 8 years ago

Can you please add manual rendering example? I can't get remove button to work. I got this error: Component with name 'multiplier_remover' does not exist.

Form code:

$multiplier = $form->addMultiplier('metadata', [$this, 'addMetadata'], 0);
$multiplier->addCreateSubmit('Přidat');
$multiplier->addRemoveSubmit('Odebrat');

My template:

<table class="table table-bordered">
    <thead>
        <tr>
            <th>Název</th>
            <th>Hodnota</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr n:foreach="$form['metadata']->getContainers() as $id => $container">
            <td n:foreach="$container->components as $name => $component" n:class="$component->name === 'vps_metadata_id' ? hide">
                {input metadata-$id-$name class => 'form-control'}
                <span class="help-block" style="color:#a94442" n:foreach="$component->errors as $error">{$error}</span>
            </td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="3" class="text-right">
                {input $form['metadata']['multiplier_remover'] class => 'btn btn-primary'}
                {input $form['metadata']['multiplier_creator'] class => 'btn btn-primary'}
            </td>
        </tr>
    </tfoot>
</table>
MartkCz commented 8 years ago

Remove button shows only if user can delete some input (the same applies for second button, but the other way around).

Usage:

<td colspan="3" class="text-right">
    {foreach $form['metadata']->getButtons() as $button}
        {input $button class => 'btn btn-primary'}
    {/foreach}
</td>

or (current commit)

{if $btn = $form['metadata']->getRemoveButton()}
    {input $btn class => 'btn btn-primary'}
{/if}
{if $btn = $form['metadata']->getCreateButton()}
    {input $btn class => 'btn btn-primary'}
{/if}

off topic:

Input macro accepts also object, so better is:

<tr n:foreach="$form['metadata']->getContainers() as $id => $container">
            <td n:foreach="$container->components as $name => $component" n:class="$component->name === 'vps_metadata_id' ? hide">
                {input metadata-$id-$name class => 'form-control'}
                <span class="help-block" style="color:#a94442" n:foreach="$component->errors as $error">{$error}</span>
            </td>
        </tr>
foxycode commented 8 years ago

@MartkCz Thans for fast answer. I solved it with ofset right now, because I want different button classes after all:

<td colspan="3" class="text-right">
    {ifset $form['metadata']['multiplier_remover']}{input $form['metadata']['multiplier_remover'] class => 'btn btn-danger'}{/ifset}
    {input $form['metadata']['multiplier_creator'] class => 'btn btn-success'}
</td>

But there must be error. Delete button is missing if I have one containers even if I have copies set to 0. I need to be able remove last container too.

Nice feature would be if remove buttons were on every line so I could remove one line in middle of others.

I don't understand last example, it seems same as mine.

MartkCz commented 8 years ago

But there must be error. Delete button is missing if I have one containers even if I have copies set to 0. I need to be able remove last container too.

Fixed

$multiplier->setMinCopies(0);

Nice feature would be if remove buttons were on every line so I could remove one line in middle of others.

I will look at it

foxycode commented 8 years ago

@MartkCz With setMinCopies ti now works. Why I need to call it and this isn't enought?

$metadata = $form->addMultiplier('metadata', [$this, 'addMetadata'], 0, 10);

There is probably new bug introduced. When I don't set maxCopies there is no add button wven with zero items.

MartkCz commented 8 years ago

Can you try dev version? You don't have to set min copies. Remove buttons added to each container.

foxycode commented 8 years ago

@MartkCz Great. Almost everything works. I have last issue I hope.

I have some initial data:

/** @var \WebChemistry\Forms\Controls\Multiplier $metadata */
$metadata = $form->addMultiplier('metadata', [$this, 'addMetadata'], 0);
$metadata->addCreateButton('Přidat');
$metadata->addRemoveButton('Odebrat');

if (!$this->vps) {
    $metadataValues = [
        [
            'name'  => 'root_authorized_keys',
            'value' => 'test',
        ],
        [
            'name'  => 'user-script',
            'value' => 'test2',
        ],
    ];
    $metadata->setDefaults($metadataValues);
} else {
    $metadata->setDefaults(array_map('iterator_to_array',
        iterator_to_array($this->vps->related('vps_metadata'))));
}

Now when I remove both rows and click on add button, both previous rows will appear again (empty) and I get total 3 rows now.

MartkCz commented 8 years ago

Thank you for all reports. I fixed it in master and added feature.