Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Validating all forms in manager at same time

I am trying to combine two forms on a page, then submit and validate and display the same error message (if any) example

The Controller:

    $this->forms->set('form1', new \Forms\Register\Part1());
    $this->forms->set('form2', new \Forms\Register\Part2());

The view File:

  {{ forms.get('form1').label('firstName') }}
  {{ forms.get('form1').label('lastName') }}
  {{ forms.get('form2').label('email') }}
  {{ forms.get('form2').label('password1') }}
  {{ forms.get('form2').label('password2') }}

So I have validation in both forms (presenceOF, and in Part2 I have idenitcal on password1 and password2)

I am wanting to do something like:

   if (!$this->forms->isValid($this->request->getPost()))
   {
     foreach($this->forms->getMessages() as $msg)
     {
        $this->view->errors($msg);
   }  
     }

My two options as I see currently is to do the above isValid method for both forms, or manually make one form that has all the fields in. However the problem I have is that i have another form that is using Part1a and Part2 thus reusing the form 'part2'. If i maunally create two forms then the data will have to be maintanted in both forms.

Any Ideas?

Create an unique form dynamically using your part1 and part2

edited Jan '19

how would one make a unique form dynamically - as this sounds perfect. You cant merge two objects into one, and Ive googled everywhere but I can not seem to find any such thing as dynamic forms?

The dynamic forms I see are only for dynamic fields in a single form, which again will increase duplication. Im trying to keep resuable small forms and combine when needed.

edited Jan '19

Don't merge the two in one like an array, you need to create a new form class, instanciate the 2 part, parse elements and add them into the new form.
Give me the code of your two forms then I'll show you.

edited Jan '19

Form 1

<?php

namespace Forms\Register;

use Phalcon\Forms\Form,
    Phalcon\Forms\Element\Text,
    Phalcon\Validation\Validator\PresenceOf;

class Form1 extends Form
{
    /**
     * Initialize the form
     */
    public function initialize()
    {
        $this->addElement1();
    }

    /**
     * Add the element 1 to input
     */
    private function addElement1()
    {
        $element = new Text('element1', array(
            'placeholder'  => 'Enter Element1 Value..',
            'name'         => 'element1',
            'autocomplete' => 'on',
            'tabindex' => 1
        ));
        $element->setLabel('Element One:');
        $element->addValidators(array(
            new PresenceOf(array(
                'message' => 'Your element one is required'
            ))
        ));
        $this->add($element);
    }
}

The second Form:

<?php

namespace Forms\Register;

use Phalcon\Forms\Form,
    Phalcon\Forms\Element\Text
    Phalcon\Forms\Element\Submit,
    Phalcon\Validation\Validator\PresenceOf;

class Form2 extends Form
{
    /**
     * Initialize the form
     */
    public function initialize()
    {
        $this->addElement2();
        $this->addSubmit();
    }

    /**
     * Add the element 2 to input
     */
    private function addElement2()
    {
        $element = new Text('element2', array(
            'placeholder'  => 'Enter Element2 Value..',
            'name'         => 'element2',
            'autocomplete' => 'off',
            'tabindex' => 2
        ));
        $element->setLabel('Element Two:');
        $element->addValidators(array(
            new PresenceOf(array(
                'message' => 'Your element two is required'
            ))
        ));
        $this->add($element);
    }

    /**
     * Add the submit Form
     */
    private function addSubmit()
    {
        $submit = new Submit('register', array(
            'name' => 'register',
            'value' => 'Create Your Account',
            'tabindex' => 3
        ));

        $this->add($submit);
    }
}

The Controller

        $this->forms->set('form1', new \Forms\Register\Form1());
        $this->forms->set('form2', new \Forms\Register\Form2());

So Currently I have a choice of doing something nasty like:

           if (!$this->forms->get('form1')->isValid($this->request->getPost())
             && !$this->forms->get('form2')->isValid($this->request->getPost())
               || !$this->forms->get('form1')->isValid($this->request->getPost())
                || !$this->forms->get('form2')->isValid($this->request->getPost())
                )
            {

                $messages = array();

                foreach($this->forms->get('tenant')->getMessages() as $msg)
                {
                    array_push($messages, $msg);
                }

                foreach($this->forms->get('user')->getMessages() as $msg)
                {
                    array_push($messages, $msg);
                }

                $output = null;

                foreach($messages as $message)
                {
                    $output = $output . ' ' . $message;
                }}

                $this-view->errorMsg = $output;

I want something cleaner that puts all form validation together.

The view


                <fieldset>
                    <dl>
                        <dt>{{ forms.get('form1').label('element1') }}</dt>
                        <dd>{{ forms.get('form1').render('element1', ['tabindex': '1']) }}</dd>
                    </dl>
                    <dl>
                        <dt>{{ forms.get('form2').label('element2') }}</dt>
                        <dd>{{ forms.get('form2').render('element2', ['tabindex': '2']) }}</dd>
                        <dt>{{ forms.get('form2').label('register') }}</dt>
                        <dd>{{ forms.get('form2').render('register', ['tabindex': '3']) }}</dd>
                    </dl>
                </fieldset>


11.1k
Accepted
answer

So create a new form with the two others :

<?php

namespace Forms\Register;

use Phalcon\Forms\Form;

class Form3 extends Form
{
    /**
     * Initialize the form
     */
    public function initialize()
    {
        foreach([
            new Form1(),
            new Form2()
        ] as $form){
            foreach($form->getElements() as $element){
                $this->add($element);
            }
        }
    }
}

controller

$this->forms->set('form', new \Forms\Register\Form3());

view

                <fieldset>
                    <dl>
                        <dt>{{ forms.get('form').label('element1') }}</dt>
                        <dd>{{ forms.get('form').render('element1', ['tabindex': '1']) }}</dd>
                    </dl>
                    <dl>
                        <dt>{{ forms.get('form').label('element2') }}</dt>
                        <dd>{{ forms.get('form').render('element2', ['tabindex': '2']) }}</dd>
                        <dt>{{ forms.get('form').label('register') }}</dt>
                        <dd>{{ forms.get('form').render('register', ['tabindex': '3']) }}</dd>
                    </dl>
                </fieldset>

result

    if (!$this->forms->get('form')->isValid($this->request->getPost()){
        ...
    }