Validácia formulára – form sa neodošle bez validationScope(false)

Aris
Člen | 48
+
0
-

Ahoj, neviem si rady s validáciou formulára. Pri akcii create sa jednoducho nezvaliduje ani nevyskočí žiaden dialóg chybne vyplneného poľa. Kde môže byť chyba? Ak nastavím setValidationScope(false) formulár bez problémov prejde, potrebujem ho však validovať.

Pustiť ho prípadne bez validácie, validovať zvlásť javascriptom a pred vkladaním do modelu zvalidovať hodnoty"manuálne"?

$form = new Form();
        $form->addProtection();
		$form->addText('id_rec')
		->setAttribute('type', "hidden");
		$form->addText('image_url')
		->setAttribute('type', "hidden");
		$form->addText('slug')
		->setAttribute('class', 'form-control m-b-5')
         ->setRequired()
            ->addRule(Form::FILLED,'Zadajte slug receptu');

		$form->addText('title')
		->setAttribute('class', 'form-control m-b-5 d-inline-block')
        ->setRequired()
            ->addRule(Form::FILLED,'Zadajte názov receptu');
		$form->addText('fb_url')
		->setAttribute('class', 'form-control d-inline-block social-link-input m-l-5');
		$form->addText('ig_url')
		->setAttribute('class', 'form-control d-inline-block social-link-input');

		$form->addText('pinterest_url')
		->setAttribute('class', 'form-control d-inline-block social-link-input');

		$form->addText('preparation')
        		->setAttribute('class','form-control recipe-number-input d-inline-block')
                ->setDefaultValue(1)
                ->setRequired()
                ->addRule(Form::FLOAT,'Pole dĺžka prípravy musí byť číselné')
                ->addRule(Form::FILLED,'Zadajte dĺžku prípravy receptu');
		$form->addText('cooking')
		->setAttribute('class','form-control recipe-number-input d-inline-block')
                ->setDefaultValue(1)
                ->setRequired()

                ->addRule(Form::FLOAT,'Pole dĺžka varenia musí byť číselné')
                ->addRule(Form::FILLED,'Zadajte dĺžku varenia receptu');
		$form->addText('portion_count')
		->setAttribute('class','portion-amount form-control form-control-line')
                ->setDefaultValue(1)
                ->setRequired()
                ->addRule(Form::FLOAT,'Pole počet porcií číselné')
                ->addRule(Form::FILLED,'Zadajte počet porcií receptu');

        $form->addTextArea('metatext')
            ->setAttribute('class', "form-control m-b-5")
            ->setAttribute('rows', '5')
            ->setAttribute('maxlength', "165")
            ->setRequired(false)
            ->addRule(Form::MIN_LENGTH, 'Meta text musí obsahovať aspoň %d znakov', 60)
            ->addRule(Form::FILLED,'Zadajte meta popis receptu');
        $form->addTextArea('textReceptu')
            ->setAttribute('class', "form-control m-b-5 ck-text")
            ->setAttribute('id', 'textReceptu')
            ->setAttribute('rows', '8')
            ->setAttribute('cols', '80')
            ->setRequired(false)
            ->addRule(Form::MIN_LENGTH, 'Titulný text receptu musí obsahovať aspoň %d znakov', 60);

        $form->addTextArea('textAfterRecipe')
            ->setAttribute('class', "form-control m-b-5 ck-text")
            ->setAttribute('rows', '5')
            ->setAttribute('id', 'textAfterRecipe')
            ->setRequired(false)
            ->addRule(Form::MIN_LENGTH, 'Záverečný text musí obsahovať aspoň %d znakov', 60);

        $form->addText('metatitle')
            ->setAttribute('class', 'form-control m-b-5')
            ->setRequired(false)
            ->addRule(Form::MIN_LENGTH, 'Meta titulok musí obsahovať aspoň %d znakov', 10)
            ->addRule(Form::FILLED,'Zadajte meta titulok receptu ');

        $form->addMultiSelect('categories', 'Kategórie', $this->categories)
            ->setAttribute('class', 'select2-container select2-container-multi select2 m-b-10 multi-select tags')
            ->setAttribute('multiple','multiple')
            ->setRequired(false)
            ->addRule(Form::FILLED,'Vyberte kategóriu receptu ');

        $form->addSelect('cat_edit_select','',$this->categoryModel->selectCategories())
            ->setAttribute('multiple','multiple');

        $form->addText('new_cat_name')
        	->setAttribute('class','form-control');
        $form->addText('new_cat_slug')
            ->setAttribute('class','form-control');
        $form->addTextArea('new_cat_desc')
            ->setAttribute('id', 'new_cat_desc')
        	->setAttribute('class','form-control ck-text ck-ajax');

        $form->addText('new_tag_name')
        	->setAttribute('class','form-control')
            ->setRequired(false)
            ->addRule(Form::MIN_LENGTH, 'Názov tagu musí obsahovať aspoň %d znakov', 5);
        $form->addText('new_tag_slug')
            ->setAttribute('class','form-control')
            ->setRequired(false)
            ->addRule(Form::MIN_LENGTH, 'Slug tagu musí obsahovať aspoň %d znakov', 5);
        $form->addTextArea('new_tag_desc')
            ->setAttribute('id', 'new_tag_desc')
        	->setAttribute('class','form-control ck-text ck-ajax');

        $form->addMultiSelect('tags', 'Tagy', $this->tags)
            ->setAttribute('class','select2-container select2-container-multi select2 m-b-10 multi-select')
            ->setAttribute('multiple','multiple');
        $form->addSelect('tags_edit_select','',$this->categoryModel->selectTags())
            ->setAttribute('multiple','multiple');

        $time = date("H:i");
        $date = date("d/m/Y");

        $form->addText('publish_time','Čas')
        	->setAttribute('class','form-control')
        	->setAttribute('placeholder',$time)
            ->setRequired()
            ->addRule(Form::FILLED, 'Zadajte čas publikovania');

        $form->addText('publish_date','Dátum')
        	->setAttribute('class','form-control')
        	->setAttribute('placeholder',$date)
            ->setRequired()
            ->addRule(Form::FILLED, 'Zadajte dátum publikovania');

        $form->addText('search', "Hľadať...")
                ->setAttribute('class', 'typeahead form-control');


        	$copies = 1;
        	$maxCopies = 20;
        	$multiplier = $form->addMultiplier('multiplier',
        		function (Nette\Forms\Container $container, Nette\Forms\Form $form)
        		{
        			$container->addText('krok');
        		}, $copies, $maxCopies);
        	$multiplier->addCreateButton('Pridať')
        	->addClass('ajax btn btn-primary pull-right m-r-15');
        	$multiplier->addRemoveButton('Odstrániť')
        	->addClass('ajax btn btn-danger pull-right m-t-5');

            $copies_2=1;
            $maxCopies_2=20;
            $multiplier_2= $form->addMultiplier('testMultiplier',
                function (Nette\Forms\Container $container, Nette\Forms\Form $form)
                {
                    $container->addText('skusobnyInput');
                }, $copies_2, $maxCopies_2);
            $multiplier_2->addRemoveButton('x')
            ->addClass('ajax btn btn-danger pull-right m-t-5');
                        $multiplier_2->addCreateButton('Pridať')
            ->addClass('ajax btn btn-primary pull-right m-r-15');


        if($this->presenter->action=='edit')
        {


            $recept = $this->recept;
            if($recept == NULL)
            {
            $httpRequest = $this->getHttpRequest();
            $id_receptu = $httpRequest->getUrl()->getQueryParameters()['id_receptu'];
            $recept = $this->receptyModel->selectRecipe($id_receptu);
            }

            $dateTime = DateTime::from($recept->publish_on);
            $time = date("H:i:s",strtotime($dateTime));
            $date = date("d.m.Y",strtotime($dateTime));


            //bdump($this->categoryModel->selectAttachedCategories($recept->categories));
            $form->setDefaults([
                    'preparation' => $recept->preparation,
                    'cooking' => $recept->cooking,
                    'portion_count' => $recept->portion_count,
                    'title' => $recept->title,
                    'slug' => $recept->slug,
                    'fb_url' => $recept->fb_url,
                    'ig_url' => $recept->ig_url,
                    'pinterest_url' => $recept->pinterest_url,
                    'categories' => $this->categoryModel->selectAttachedCategories($recept->categories),
                    'tags' => $this->categoryModel->selectAttachedCategories($recept->tags),
                    'publish_time' => $time,
                    'publish_date' => $date
            ]);


        }
            $this->redrawControl('dir');
            $this->redrawControl('directions');

        if($this->presenter->action=='create')
        {
            $form->addSubmit('save', 'Uložiť')
                ->setAttribute('class','btn btn-default pull-right m-r-20')

                ->onClick[] = [$this, 'receptCreateSucceeded'];
        }
        else
        {
            $form->addSubmit('save', 'Uložiť')
                ->setAttribute('class','btn btn-default pull-right m-r-20')
                ->setValidationScope(FALSE)
                ->onClick[] = [$this, 'receptUpdateSucceeded'];
        }

        $form->addSubmit('delete', 'Zmazať')
                ->setAttribute('class','btn btn-danger pull-right m-r-20')
                ->setValidationScope(FALSE)
                ->onClick[] = [$this, 'resetForm'];
        $form->onSubmit[]= function(Form $form){
            //bdump($form->values);
                   // $this->redrawControl();

        }            ;
		return $form;
	}
David Matějka
Moderator | 6445
+
0
-

Neodesle (tedy ani se neprovede http request) nebo se pouze nezavola onClick? nemas vlastni renderovani formulare? pokud ano, vykreslujes spravne validacni chyby? pokud se ani neodesle, tak co pouzivas na frontend validaci? netteForms.js?

Aris
Člen | 48
+
0
-

Http request sa vykoná avšak opäť na akciu create, parametre sú vyplnené podľa formulára. Problém je s volaním success metódy na strane servera tým pádom.

Rendrujem manuálne cez inputy a využívam netteForms.min.js . Pri nastavenej validácii a nevyplnení polí dostanem hlášku. Treba manuálne nastaviť vykresľovanie validácie chýb cez JS? Doteraz som nemal väčšie problémy keď som nastavil setRequired() a rules v PHP, netteForms vždy vyhodilo správne hlášky.

{form receptyForm}
...
<div class="col-md-4">
   <h3 class="box-title d-inline-block">Názov</h3>
   {input title}
   <span class=""><small><strong>Slug</strong></small></span>
    {input slug}
</div>
...

//odosielanie
   <div class="col-md-4 pull-right m-t-20 m-r-10">
        {input publish}
        {input save}
        {input delete}
   </div>
{/form}

Editoval Aris (20. 1. 2019 14:16)