Kdyby/Replicator pridávanie a mazanie formulárových prvkov ( AJAX Nette )

KristianSubweb
Člen | 144
+
0
-

Ahojte mám problém s ktorým nie a nie pohnúť. Tak mám formulár kde určitu skupinu inputov chcem replikovať a to tak že akcia je naviazaná na button a odosielaná je cez nette ajax. Lenže celé to padá do nasledovného erroru.

Error message:
ErrorException: end() expects parameter 1 to be array, null given in /Users/Documents/www/core/temp/cache/latte/templates-In-addIn.latte–8f09c67d34.php:405

V latte to vyzerá nasledovne:

{form addInForm}

	{control renderFormInput $form['number']}
    {control renderFormInput $form['name']}
    {control renderFormInput $form['vs']}

	{snippet dynamicInformation}
		 {foreach $form['inItems']->getContainers() as $componentId => $component}
			<td>
                                <div class="form-group-sm">
                                    {input inItems-$componentId-tax class => "form-control tax"}
                                </div>
                            </td>
                            <td>
                                <div class="form-group-sm">
                                    {input inItems-$componentId-price class => "form-control 	price"}
                                </div>
                            </td>

			{if $iterator->isLast()}
				<button n:name="inItems-add" class="btn btn-sm btn-default ajax">+</button>
            {/if}
		 {/foreach}
	{/snippet}

{/form}

FormFactory:

class InFormFactory extends DynamicFormFactory {
	public function getForm() {

        $currencies = InDefaultValues::getCurrencies();
        $ks = InDefaultValues::getConstantSymbols();
        $paymentForm = InDefaultValues::getPaymentForms();
        $contact = $this->user->getEntity()->getContact();

        $form = $this->translatedFormFactory->create();
        $form->addHidden("cleanNumber");
        $form->addText("number", "in.number");
        $form->addText("name", "in.name");
        $form->addText("vs", "in.vs");

        return $form;
    }
	private function addDynamicItems(Form $form, $tax, $defaultCount = 0) {

		$items = $form->addDynamic("inItems", function(Container $item) use($tax, $unitTypes) {
            // item things
            $item->addText("price")->setAttribute("value", 0.00);
            $item->addText("tax")->setAttribute("value", $ta

            // delete
            $item->addSubmit("delete", "")
                    ->setValidationScope(FALSE)
                    ->onClick[] = [$this, "deleteComponent"];
        }, $defaultCount);
        $items->addSubmit('add', '')
            ->setValidationScope(FALSE)
            ->onClick[] = [$this, 'addComponent'];
	}

A Extendujem DynamicFormFactory:

<?php

namespace DefaultModule\Factories;

use Nette\Forms\Controls;
use Nette\SmartObject;

abstract class DynamicFormFactory {

    use SmartObject;

    /**
     * Adds next container into specified component
     * @param Controls\SubmitButton $button
     */
    public function addComponent(Controls\SubmitButton $button) {
        $button->getParent()->createOne();
    }

    /**
     * Deletes container if it has more than 1 container in component
     * @param Controls\SubmitButton $button
     * @param int $numberOfComponents 99% it has container and add component so default value is 2
     */
    public function deleteComponent(Controls\SubmitButton $button, $numberOfComponents = 2)
    {

        // first parent is container
        // second parent is it's replicator
        $replicator = $button->getParent()->getParent();
        if (count($replicator->getComponents()) > $numberOfComponents) {
            $replicator->remove($button->getParent(), TRUE);
        } else {
            $replicator->createOne();
            $replicator->remove($button->getParent(), TRUE);
        }
    }

}

Presenter:

	public function createComponentAddInForm() {
    $form = $this->inFormFactory->createAddForm($this, $this->getParameter("insid"), $this->getParameter("id"));

    $form->onSuccess[] = function() use($form) {
        if ($this->isAjax()) {
            $this->redrawControl("dynamicInformation");
        }

        if ($form["submit"]->isSubmittedBy()) {
            if ($this->inFormFactory->resultOfForm) {
                $this->flashMessage("in.sucAdded", "success");
                $this->redirect("In:default");
            }
        }
    };
    return $form;
}

Hádam ste už niekto niečo podobné riešil.Vopred Vám ďakujem.

Editoval KristianSubweb (7. 8. 2018 11:58)