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>