Skip to content

Commit d3b5bf6

Browse files
committed
Form::getValues() triggers warning when form is not valid
1 parent d31a0e9 commit d3b5bf6

5 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/Forms/Container.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public function setValues($data, bool $erase = false)
108108
*/
109109
public function getValues($returnType = null, array $controls = null)
110110
{
111+
$form = $this->getForm(false);
112+
if ($form && $form->isSubmitted() && !$form->isValid()) {
113+
trigger_error(__METHOD__ . '() invoked but the form is not valid.', E_USER_WARNING);
114+
}
115+
111116
if ($returnType === self::ARRAY || $returnType === true || $this->mappedType === self::ARRAY) {
112117
$returnType = self::ARRAY;
113118
$obj = new \stdClass;
@@ -181,6 +186,8 @@ public function validate(array $controls = null): void
181186
$control->validate();
182187
}
183188
}
189+
$this->validated = true;
190+
184191
if ($this->onValidate !== null) {
185192
if (!is_iterable($this->onValidate)) {
186193
throw new Nette\UnexpectedValueException('Property Form::$onValidate must be iterable, ' . gettype($this->onValidate) . ' given.');
@@ -195,7 +202,6 @@ public function validate(array $controls = null): void
195202
$handler($this, $values);
196203
}
197204
}
198-
$this->validated = true;
199205
}
200206

201207

tests/Forms/Container.values.ArrayHash.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test('submitted form + getValues()', function () {
7474
'title' => 'sent title',
7575
'first' => ArrayHash::from([
7676
'name' => '',
77-
'age' => '999',
77+
'age' => 999,
7878
'second' => ArrayHash::from([
7979
'city' => 'sent city',
8080
]),
@@ -122,7 +122,7 @@ test('setValues() + iterable', function () {
122122
'title' => 'new1',
123123
'first' => ArrayHash::from([
124124
'name' => 'new2',
125-
'age' => '999',
125+
'age' => 999,
126126
'second' => ArrayHash::from([
127127
'city' => 'sent city',
128128
]),

tests/Forms/Container.values.array.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test('submitted form + getValues(true)', function () {
7474
'title' => 'sent title',
7575
'first' => [
7676
'name' => '',
77-
'age' => '999',
77+
'age' => 999,
7878
'second' => [
7979
'city' => 'sent city',
8080
],
@@ -122,7 +122,7 @@ test('setValues() + array', function () {
122122
'title' => 'new1',
123123
'first' => [
124124
'name' => 'new2',
125-
'age' => '999',
125+
'age' => 999,
126126
'second' => [
127127
'city' => 'sent city',
128128
],
@@ -168,7 +168,7 @@ test('getValues(...arguments...)', function () {
168168
'title' => 'new1',
169169
'first' => [
170170
'name' => 'new2',
171-
'age' => '999',
171+
'age' => 999,
172172
'second' => [
173173
'city' => 'sent city',
174174
],
@@ -196,7 +196,7 @@ test('setMappedType(array)', function () {
196196
'title' => 'new1',
197197
'first' => [
198198
'name' => 'new2',
199-
'age' => '999',
199+
'age' => 999,
200200
'second' => [
201201
'city' => 'sent city',
202202
],

tests/Forms/Container.values.mapping.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ test('submitted form + getValues()', function () {
116116
'title' => 'sent title',
117117
'first' => ArrayHash::from([
118118
'name' => '',
119-
'age' => '999',
119+
'age' => 999,
120120
'second' => ArrayHash::from([
121121
'city' => 'sent city',
122122
]),
@@ -326,7 +326,7 @@ test('getValues() + object', function () {
326326
'title' => 'sent title',
327327
'first' => ArrayHash::from([
328328
'name' => '',
329-
'age' => '999',
329+
'age' => 999,
330330
'second' => ArrayHash::from([
331331
'city' => 'sent city',
332332
]),

tests/Forms/Forms.validationScope.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use Tester\Assert;
1313

1414
require __DIR__ . '/../bootstrap.php';
1515

16-
16+
//Tracy\Debugger::enable();
1717
$datasets = [
1818
['send1', ['container', 'form', 'name', 'age', 'age2'], null],
1919
['send2', ['form'], ['optional' => '', 'details' => []]],

0 commit comments

Comments
 (0)