Skip to content

Commit f640e38

Browse files
author
Harrison Heck
committed
Merge pull request #7 from tavofuentes/support/default-value-number-field
Added defaultValue for NumberField.
2 parents 190af1e + ddceca6 commit f640e38

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

FormlyMapper/FormlyField.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ public function getFormlyFieldConfiguration()
3535
*/
3636
protected function buildConfiguration()
3737
{
38+
$this->formlyFieldConfiguration = [];
3839
$this->formlyFieldConfiguration['key'] = $this->fieldConfiguration['name'];
3940
$this->formlyFieldConfiguration['type'] = 'input';
4041

42+
if (isset($this->fieldConfiguration['options']['data'])) {
43+
$this->formlyFieldConfiguration['defaultValue'] = $this->fieldConfiguration['options']['data'];
44+
unset($this->fieldConfiguration['options']['data']);
45+
}
46+
4147
if (isset($this->fieldConfiguration['options'])) {
4248
$templateOptions = $this->fieldConfiguration['options'];
4349
$templateOptions['label'] = ucfirst($this->fieldConfiguration['name']);

FormlyMapper/FormlyField/NumberField.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ protected function buildFieldTypeConfiguration()
2424

2525
if (isset($validation['Symfony\Component\Validator\Constraints\Range'])) {
2626
$constraint = $validation['Symfony\Component\Validator\Constraints\Range'];
27-
$this->formlyFieldConfiguration['templateOptions']['min'] = isset($constraint['min']) ? $constraint['min'] : '';
27+
28+
if (isset($constraint['min'])) {
29+
$this->formlyFieldConfiguration['templateOptions']['min'] = $constraint['min'];
30+
}
2831

2932
if (isset($constraint['minMessage'])) {
3033
$this->formlyFieldConfiguration['validation']['messages']['min'] = $constraint['minMessage'];
3134
}
3235

33-
$this->formlyFieldConfiguration['templateOptions']['max'] = isset($constraint['max']) ? $constraint['max'] : '';
36+
if (isset($constraint['max'])) {
37+
$this->formlyFieldConfiguration['templateOptions']['max'] = $constraint['max'];
38+
}
3439

3540
if (isset($constraint['maxMessage'])) {
3641
$this->formlyFieldConfiguration['validation']['messages']['max'] = $constraint['maxMessage'];

Tests/FormlyMapper/FormlyField/NumberFieldTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ public function testIsAddingNumberFields()
1919
'options' => [
2020
'required' => true,
2121
'label' => 'Width',
22+
'data' => 255,
2223
],
2324
];
2425

2526
$expected = [
2627
'key' => 'width',
2728
'type' => 'input',
29+
'defaultValue' => 255,
2830
'templateOptions' => [
2931
'type' => 'number',
3032
'label' => 'Width',
@@ -46,10 +48,11 @@ public function testIsRangeConstraint()
4648
'options' => [
4749
'required' => true,
4850
'label' => 'Age',
51+
'data' => 18,
4952
],
5053
'validation' => [
5154
'Symfony\Component\Validator\Constraints\Range' => [
52-
'min' => 5,
55+
'min' => 18,
5356
'max' => 100,
5457
'minMessage' => 'Min length minimum value should be at last {{ limit }}',
5558
'maxMessage' => 'Max length maximum value should be at maximum {{ limit }}',
@@ -60,12 +63,13 @@ public function testIsRangeConstraint()
6063
$expected = [
6164
'key' => 'age',
6265
'type' => 'input',
66+
'defaultValue' => 18,
6367
'templateOptions' => [
6468
'type' => 'number',
6569
'label' => 'Age',
6670
'required' => true,
67-
'min' => '5',
68-
'max' => '100',
71+
'min' => 18,
72+
'max' => 100,
6973
],
7074
'validation' => [
7175
'messages' => [
@@ -89,6 +93,7 @@ public function testIsRegexConstraint()
8993
'options' => [
9094
'required' => true,
9195
'label' => 'Age',
96+
'data' => 18,
9297
],
9398
'validation' => [
9499
'Symfony\Component\Validator\Constraints\Regex' => [
@@ -101,6 +106,7 @@ public function testIsRegexConstraint()
101106
$expected = [
102107
'key' => 'age',
103108
'type' => 'input',
109+
'defaultValue' => 18,
104110
'templateOptions' => [
105111
'type' => 'number',
106112
'label' => 'Age',

0 commit comments

Comments
 (0)