Skip to content

Commit

Permalink
Merge pull request #27 from roed/master
Browse files Browse the repository at this point in the history
fix for resetting rules and messages for multiple forms
  • Loading branch information
bramstroker committed Sep 17, 2014
2 parents f008558 + 9b2742d commit 69a5333
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/StrokerForm/Renderer/JqueryValidate/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public function preRenderForm($formAlias, View $view, FormInterface $form = null
$inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/jquery.validate.bootstrap.js');
}
}

$this->reset();
}

/**
* Resets previously set rules and messages, if you have multiple forms on one request
*/
protected function reset()
{
$this->rules = array();
$this->messages = array();
}

/**
Expand Down
58 changes: 56 additions & 2 deletions tests/StrokerFormTest/Renderer/JqueryValidate/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ function ($string) {
* Get a test form with fields setup
*
* @param string $alias
* @param string $emailFieldName
* @return \Zend\Form\FormInterface
*/
protected function createForm($alias)
protected function createForm($alias, $emailFieldName = 'email')
{
$factory = new Factory();
$form = $factory->createForm(
array(
'hydrator' => 'Zend\Stdlib\Hydrator\ArraySerializable',
'name' => 'test',
'name' => $alias,
'elements' => array(
array(
'spec' => array(
Expand Down Expand Up @@ -469,6 +470,59 @@ public function testIfSetOptionsAddsRecursively()

$this->assertEquals($expectedOptions['validate_options'], $this->renderer->getOptions()->getValidateOptions());
}

public function testIfRulesAreResetWithMultipleForms()
{
$form1 = $this->createForm('test', 'customEmailName');

$inputFilter1 = new InputFilter();
$inputFilter1->add(array(
'name' => 'email',
'required' => true,
'validators' => array(
array(
'name' => 'emailAddress'
)
)
));
$inputFilter1->add(array(
'name' => 'name',
'required' => true
));

$form1->setInputFilter($inputFilter1);

$this->renderer->preRenderForm('test', $this->view);


$form2 = $this->createForm('test2', 'email');

$inputFilter2 = new InputFilter();
$inputFilter2->add(array(
'name' => 'otherfieldname',
'required' => true,
'validators' => array(
array(
'name' => 'emailAddress'
)
)
));
$inputFilter2->add(array(
'name' => 'name',
'required' => true
));

$form2->setInputFilter($inputFilter2);

$this->renderer->preRenderForm('test2', $this->view);

$inlineScript = $this->view->plugin('inlineScript');
$inlineString = preg_replace('/(\r\n|\r|\n|\t)+/', '', $inlineScript->toString());
$explodedString = explode('form[name="test2"]', $inlineString);
$lastPart = end($explodedString);

$this->assertNotContains('customEmailName', $lastPart);
}

/**
* Get rules and messages as matches from the inlineScript string
Expand Down

0 comments on commit 69a5333

Please sign in to comment.