Skip to content

Commit 3bdeb99

Browse files
author
Yuri Maltsev
committed
Merge pull request #39 from formapro/JsFormValidatorBundle-37
JsFormValidatorBundle-37 Error mapping doesn't work
2 parents 0bc03f9 + 7b9c837 commit 3bdeb99

10 files changed

Lines changed: 233 additions & 9 deletions

File tree

Resources/public/js/FpJsFormValidator.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ function FpJsFormElement() {
2929
self.errors[sourceId] = FpJsFormValidator.validateElement(self);
3030

3131
var errorPath = FpJsFormValidator.getErrorPathElement(self);
32-
errorPath.showErrors.apply(errorPath.domNode, [self.errors[sourceId], sourceId]);
32+
var domNode = errorPath.domNode;
33+
if (!domNode) {
34+
for (var childName in errorPath.children) {
35+
var childDomNode = errorPath.children[childName].domNode;
36+
if (childDomNode) {
37+
domNode = childDomNode;
38+
break;
39+
}
40+
}
41+
}
42+
errorPath.showErrors.apply(domNode, [self.errors[sourceId], sourceId]);
3343

3444
return self.errors[sourceId].length == 0;
3545
};

Resources/public/js/fp_js_validator.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ function FpJsFormElement() {
2929
self.errors[sourceId] = FpJsFormValidator.validateElement(self);
3030

3131
var errorPath = FpJsFormValidator.getErrorPathElement(self);
32-
errorPath.showErrors.apply(errorPath.domNode, [self.errors[sourceId], sourceId]);
32+
var domNode = errorPath.domNode;
33+
if (!domNode) {
34+
for (var childName in errorPath.children) {
35+
var childDomNode = errorPath.children[childName].domNode;
36+
if (childDomNode) {
37+
domNode = childDomNode;
38+
break;
39+
}
40+
}
41+
}
42+
errorPath.showErrors.apply(domNode, [self.errors[sourceId], sourceId]);
3343

3444
return self.errors[sourceId].length == 0;
3545
};
@@ -302,10 +312,7 @@ function FpJsCustomizeMethods() {
302312
this.delPrototype = function(name) {
303313
//noinspection JSCheckFunctionSignatures
304314
FpJsFormValidator.each(this, function (item) {
305-
// console.log(name, item.jsFormValidator.children, item.jsFormValidator.children[name]);
306315
delete (item.jsFormValidator.children[name]);
307-
// console.log(item.jsFormValidator.children);
308-
// console.log('----------------');
309316
});
310317
};
311318
}
@@ -540,7 +547,6 @@ var FpJsFormValidator = new function () {
540547
value = element.transformers[i].reverseTransform(value, element);
541548
}
542549

543-
console.log(element, value);
544550
return value;
545551
};
546552

Tests/Functional/MainFunctionalTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,45 @@ public function testEmptyChoice()
347347
$fpErrors = $this->getAllErrorsOnPage('empty_choice/0/1', null, 'form_choice_submit');
348348
$this->assertErrorsEqual($sfErrors, $fpErrors, 'Choice fields have all the errors.');
349349
}
350+
351+
public function testPasswordField()
352+
{
353+
$btnId = 'form_password_field_submit';
354+
// Check the valid values
355+
$sfErrors = $this->getAllErrorsOnPage('password_field/1/0', null, $btnId);
356+
$fpErrors = $this->getAllErrorsOnPage('password_field/1/1', null, $btnId);
357+
$this->assertErrorsEqual($sfErrors, $fpErrors, 'Choice fields are valid.');
358+
359+
$session = $this->session;
360+
$changeAndGetErrors = function ($first, $second) use ($session) {
361+
$page = $session->getPage();
362+
$submit = $page->findButton('form_password_field_submit');
363+
364+
// Check the Length constraint
365+
$page->findField('form_password_field_password_first')->setValue($first);
366+
$page->findField('form_password_field_password_second')->setValue($second);
367+
$submit->click();
368+
$errors = array();
369+
/** @var \Behat\Mink\Element\NodeElement $item */
370+
foreach ($page->findAll('css', 'ul.form-errors li') as $item) {
371+
$errors[] = $item->getText();
372+
}
373+
374+
return $errors;
375+
};
376+
377+
$sfErrors = array(
378+
$this->getAllErrorsOnPage('password_field/0/0', null, $btnId), // blank fields
379+
$changeAndGetErrors('a', 'a'), // too short
380+
$changeAndGetErrors('lorem', 'qwerty'), // not equal
381+
);
382+
383+
$fpErrors = array(
384+
$this->getAllErrorsOnPage('password_field/0/1', null, $btnId), // blank fields
385+
$changeAndGetErrors('a', 'a'), // too short
386+
$changeAndGetErrors('lorem', 'qwerty'), // not equal
387+
);
388+
389+
$this->assertEquals($sfErrors, $fpErrors, 'All the errors are correct');
390+
}
350391
}

Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\CommentEntity;
88
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\CustomizationEntity;
99
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\EmptyChoiceEntity;
10+
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\PasswordFieldEntity;
1011
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\TagEntity;
1112
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\TaskEntity;
1213
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\UniqueEntity;
1314
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form\BasicConstraintsEntityType;
1415
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form\CollectionType;
1516
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form\CustomizationType;
1617
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form\EmtyChoiceType;
18+
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form\PasswordFieldType;
1719
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form\TaskType;
1820
use Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form\UniqueType;
1921
use Symfony\Component\HttpFoundation\Request;
@@ -679,4 +681,32 @@ public function empty_choiceAction(Request $request, $isValid, $js)
679681
)
680682
);
681683
}
684+
685+
public function password_fieldAction(Request $request, $isValid, $js)
686+
{
687+
$entity = new PasswordFieldEntity();
688+
689+
if ((bool)$isValid) {
690+
$entity->setPassword('test_pass');
691+
}
692+
693+
$form = $this->createForm(
694+
new PasswordFieldType(),
695+
$entity,
696+
array(
697+
'js_validation' => (bool)$js
698+
)
699+
);
700+
701+
$form->handleRequest($request);
702+
$tpl = 'DefaultTestBundle:FunctionalTests:empty_choice.html.twig';
703+
704+
return $this->render(
705+
$tpl,
706+
array(
707+
'form' => $form->createView(),
708+
'extraMsg' => $request->isMethod('post') ? 'done' : '',
709+
)
710+
);
711+
}
682712
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Symfony\Component\Validator\Constraints as Assert;
7+
8+
/**
9+
* Book
10+
*
11+
* @ORM\Table()
12+
* @ORM\Entity()
13+
*/
14+
class PasswordFieldEntity
15+
{
16+
/**
17+
* @var integer
18+
*
19+
* @ORM\Column(name="id", type="integer")
20+
* @ORM\Id
21+
* @ORM\GeneratedValue(strategy="AUTO")
22+
*/
23+
private $id;
24+
25+
/**
26+
* @var integer
27+
*
28+
* @ORM\Column(name="password", type="string", length=255)
29+
* @Assert\NotBlank(message="pass_not_blank_message")
30+
* @Assert\Length(min="3", minMessage="pass_min_length_message")
31+
*/
32+
private $password;
33+
34+
/**
35+
* Get Password
36+
*
37+
* @return int
38+
*/
39+
public function getPassword()
40+
{
41+
return $this->password;
42+
}
43+
44+
/**
45+
* Set password
46+
*
47+
* @param int $password
48+
*
49+
* @return PasswordFieldEntity
50+
*/
51+
public function setPassword($password)
52+
{
53+
$this->password = $password;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* Get Id
60+
*
61+
* @return int
62+
*/
63+
public function getId()
64+
{
65+
return $this->id;
66+
}
67+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
8+
use Symfony\Component\Validator\Constraints\NotBlank;
9+
10+
/**
11+
* Class PasswordFieldType
12+
*
13+
* @package Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Form
14+
*/
15+
class PasswordFieldType extends AbstractType
16+
{
17+
/**
18+
* @param FormBuilderInterface $builder
19+
* @param array $options
20+
*/
21+
public function buildForm(FormBuilderInterface $builder, array $options)
22+
{
23+
$builder->add(
24+
'password',
25+
'repeated',
26+
array(
27+
'type' => 'password',
28+
'invalid_message' => 'diff_pass_message',
29+
'first_options' => array('label' => 'Password'),
30+
'second_options' => array('label' => 'Repeat Password'),
31+
)
32+
)
33+
->add('submit', 'submit');
34+
}
35+
36+
/**
37+
* @param OptionsResolverInterface $resolver
38+
*/
39+
public function setDefaultOptions(OptionsResolverInterface $resolver)
40+
{
41+
$resolver->setDefaults(
42+
array(
43+
'data_class' => 'Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\PasswordFieldEntity',
44+
'attr' => array(
45+
'novalidate' => 'novalidate'
46+
)
47+
)
48+
);
49+
}
50+
51+
/**
52+
* @return string
53+
*/
54+
public function getName()
55+
{
56+
return 'form_password_field';
57+
}
58+
}

Tests/app/chmod.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
4+
ROOT=$(pwd)
5+
6+
fix() {
7+
mkdir -p ${ROOT}/$1
8+
touch "${ROOT}/$1/.gitkeep"
9+
sudo setfacl -R -m u:${HTTPDUSER}:rwX -m u:${USER}:rwX ${ROOT}/$1
10+
sudo setfacl -dR -m u:${HTTPDUSER}:rwX -m u:${USER}:rwX ${ROOT}/$1
11+
}
12+
13+
fix cache
14+
fix logs

Tests/app/logs/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

Tests/app/logs/.gitkeep

Whitespace-only changes.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"symfony/assetic-bundle": ">=2.3",
3131
"phpunit/phpunit": "3.7.*",
3232
"behat/mink-bundle": "dev-master",
33-
"behat/mink-selenium2-driver": "dev-master",
33+
"behat/mink-selenium2-driver": "1.1.0",
3434
"satooshi/php-coveralls": "dev-master"
3535
},
3636

0 commit comments

Comments
 (0)