Skip to content

Commit 4fb5752

Browse files
author
Yuri Maltsev
committed
Merge pull request #50 from formapro/JsFormValidatorBundle-47
#47 Empty choice type with expanded=true and multiple=false
2 parents c6cbfa8 + 2f3378a commit 4fb5752

File tree

13 files changed

+75
-49
lines changed

13 files changed

+75
-49
lines changed

Resources/public/js/FpJsFormValidator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ function FpJsCustomizeMethods() {
261261
//noinspection JSCheckFunctionSignatures
262262
FpJsFormValidator.each(this, function (item) {
263263
var element = item.jsFormValidator;
264-
element.validateRecursively();
265264
if (event) {
266265
event.preventDefault();
267266
}
267+
element.validateRecursively();
268268
if (FpJsFormValidator.ajax.queue) {
269269
if (event) {
270270
event.preventDefault();

Resources/public/js/fp_js_validator.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ function FpJsCustomizeMethods() {
261261
//noinspection JSCheckFunctionSignatures
262262
FpJsFormValidator.each(this, function (item) {
263263
var element = item.jsFormValidator;
264-
element.validateRecursively();
265264
if (event) {
266265
event.preventDefault();
267266
}
267+
element.validateRecursively();
268268
if (FpJsFormValidator.ajax.queue) {
269269
if (event) {
270270
event.preventDefault();
@@ -2037,29 +2037,26 @@ function SymfonyComponentFormExtensionCoreDataTransformerBooleanToStringTransfor
20372037
*/
20382038
function SymfonyComponentFormExtensionCoreDataTransformerChoiceToBooleanArrayTransformer() {
20392039
this.choiceList = {};
2040+
this.placeholderPresent = false;
20402041

20412042
this.reverseTransform = function(value){
20422043
if (typeof value !== 'object') {
20432044
throw new Error('Unexpected value type')
20442045
}
20452046

2046-
var result = [];
2047-
var unknown = [];
20482047
for (var i in value) {
20492048
if (value[i]) {
20502049
if (undefined !== this.choiceList[i]) {
2051-
result.push(this.choiceList[i]);
2050+
return this.choiceList[i] === '' ? null : this.choiceList[i];
2051+
} else if (this.placeholderPresent && 'placeholder' == i) {
2052+
return null;
20522053
} else {
2053-
unknown.push(i);
2054+
throw new Error('The choice "' + i + '" does not exist');
20542055
}
20552056
}
20562057
}
20572058

2058-
if (unknown.length) {
2059-
throw new Error('The choices "'+unknown.join(', ')+'" were not found.');
2060-
}
2061-
2062-
return result;
2059+
return null;
20632060
}
20642061
}
20652062
//noinspection JSUnusedGlobalSymbols

Resources/public/js/transformers/ChoiceToBooleanArray.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@
44
*/
55
function SymfonyComponentFormExtensionCoreDataTransformerChoiceToBooleanArrayTransformer() {
66
this.choiceList = {};
7+
this.placeholderPresent = false;
78

89
this.reverseTransform = function(value){
910
if (typeof value !== 'object') {
1011
throw new Error('Unexpected value type')
1112
}
1213

13-
var result = [];
14-
var unknown = [];
1514
for (var i in value) {
1615
if (value[i]) {
1716
if (undefined !== this.choiceList[i]) {
18-
result.push(this.choiceList[i]);
17+
return this.choiceList[i] === '' ? null : this.choiceList[i];
18+
} else if (this.placeholderPresent && 'placeholder' == i) {
19+
return null;
1920
} else {
20-
unknown.push(i);
21+
throw new Error('The choice "' + i + '" does not exist');
2122
}
2223
}
2324
}
2425

25-
if (unknown.length) {
26-
throw new Error('The choices "'+unknown.join(', ')+'" were not found.');
27-
}
28-
29-
return result;
26+
return null;
3027
}
3128
}

Tests/Functional/MainFunctionalTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,15 @@ public function testSubRequest()
206206
$sfErrors,
207207
'Sub request: a form was validated on the server side'
208208
);
209-
$this->assertEquals(
210-
'disabled_validation',
211-
$this->find('#extra_msg')->getText(),
212-
'Sub request: marker form the server side exists'
213-
);
209+
$this->assertTrue($this->wasPostRequest());
214210

215211
$fpErrors = $this->getAllErrorsOnPage('sub_request/-/1');
216212
$this->assertEquals(
217213
array('enabled_field'),
218214
$fpErrors,
219215
'Sub request: a form was validated on the JS side'
220216
);
221-
$this->assertEquals(
222-
'',
223-
$this->find('#extra_msg')->getText(),
224-
'Sub request: marker form the server side does not exist'
225-
);
217+
$this->assertFalse($this->wasPostRequest());
226218
}
227219

228220
/**
@@ -340,11 +332,15 @@ public function testCollection()
340332
public function testEmptyChoice()
341333
{
342334
$sfErrors = $this->getAllErrorsOnPage('empty_choice/1/0', null, 'form_choice_submit');
335+
$this->assertTrue($this->wasPostRequest());
343336
$fpErrors = $this->getAllErrorsOnPage('empty_choice/1/1', null, 'form_choice_submit');
337+
$this->assertTrue($this->wasPostRequest());
344338
$this->assertErrorsEqual($sfErrors, $fpErrors, 'Choice fields are valid.');
345339

346340
$sfErrors = $this->getAllErrorsOnPage('empty_choice/0/0', null, 'form_choice_submit');
341+
$this->assertTrue($this->wasPostRequest());
347342
$fpErrors = $this->getAllErrorsOnPage('empty_choice/0/1', null, 'form_choice_submit');
343+
$this->assertFalse($this->wasPostRequest());
348344
$this->assertErrorsEqual($sfErrors, $fpErrors, 'Choice fields have all the errors.');
349345
}
350346

Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,10 @@ public function empty_choiceAction(Request $request, $isValid, $js)
660660
if ((bool)$isValid) {
661661
$entity->setCity('london');
662662
$entity->setCountries(array('france'));
663+
$entity->setContinent('europe');
663664
}
664665

665-
$form = $this->createForm(
666+
$form = $this->createForm(
666667
new EmtyChoiceType(),
667668
$entity,
668669
array(

Tests/TestBundles/DefaultTestBundle/Entity/EmptyChoiceEntity.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ class EmptyChoiceEntity
3838
*/
3939
private $countries;
4040

41+
/**
42+
* @var string
43+
*
44+
* @ORM\Column(name="continent", type="string", length=255)
45+
* @Assert\NotBlank(message="continent_message")
46+
*/
47+
private $continent;
4148

4249
/**
4350
* Get id
4451
*
45-
* @return integer
52+
* @return integer
4653
*/
4754
public function getId()
4855
{
@@ -96,4 +103,21 @@ public function setCountries($countries)
96103

97104
return $this;
98105
}
99-
}
106+
107+
/**
108+
* @return string
109+
*/
110+
public function getContinent() {
111+
return $this->continent;
112+
}
113+
114+
/**
115+
* @param string $continent
116+
*
117+
* @return EmptyChoiceEntity
118+
*/
119+
public function setContinent($continent) {
120+
$this->continent = $continent;
121+
return $this;
122+
}
123+
}

Tests/TestBundles/DefaultTestBundle/Form/EmtyChoiceType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4848
'expanded' => true,
4949
)
5050
)
51+
->add(
52+
'continent',
53+
'choice',
54+
array(
55+
'empty_value' => 'Choose continent',
56+
'choices' => array(
57+
'africa' => 'Africa',
58+
'asia' => 'Asia',
59+
'europe' => 'Europe',
60+
),
61+
'multiple' => false,
62+
'expanded' => true,
63+
)
64+
)
5165
->add('submit', 'submit');
5266
}
5367

Tests/TestBundles/DefaultTestBundle/Resources/views/FunctionalTests/async_load.html.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
{% endblock init_validation %}
1616

1717
{% block body %}
18-
<div id="extra_msg">{{ extraMsg|default('') }}</div>
1918
{{ form(form) }}
2019
{% if passForm is null %}
2120
<button id="init" onclick="asyncLoad();">Init</button>

Tests/TestBundles/DefaultTestBundle/Resources/views/FunctionalTests/collection.html.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,5 @@
5454

5555
<a href="#" id="add_tag_link">Add tag</a><br/>
5656
<a href="#" id="add_comment_link">Add comment</a>
57-
<div id="extra_msg">{{ extraMsg|default('') }}</div>
5857
{{ form(form) }}
5958
{% endblock %}

Tests/TestBundles/DefaultTestBundle/Resources/views/FunctionalTests/index.html.twig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{% extends 'DefaultTestBundle::layout.html.twig' %}
22

33
{% block body %}
4-
<div id="extra_msg">{{ extraMsg|default('') }}</div>
5-
64
<script type="text/javascript">
75
$(function(){
86
var _t = {

0 commit comments

Comments
 (0)