File tree Expand file tree Collapse file tree 2 files changed +23
-7
lines changed
src/Symfony/Component/Form
tests/Symfony/Tests/Component/Form Expand file tree Collapse file tree 2 files changed +23
-7
lines changed Original file line number Diff line number Diff line change @@ -86,16 +86,23 @@ public function setData($data)
86
86
}
87
87
88
88
/**
89
- * Return only value of first password field.
89
+ * Return the value of a child field
90
90
*
91
- * @return string The password.
91
+ * If the value of the first field is set, this value is returned.
92
+ * Otherwise the value of the second field is returned. This way,
93
+ * this field will never trigger a NotNull/NotBlank error if any of the
94
+ * child fields was filled in.
95
+ *
96
+ * @return string The field value
92
97
*/
93
98
public function getData ()
94
99
{
95
- if ($ this ->isSubmitted () && $ this ->isFirstEqualToSecond ()) {
96
- return $ this ->get ($ this ->getOption ('first_key ' ))->getData ();
97
- }
100
+ // Return whichever data is set. This should not return NULL if any of
101
+ // the fields is set, otherwise this might trigger a NotNull/NotBlank
102
+ // error even though some value was set
103
+ $ data1 = $ this ->get ($ this ->getOption ('first_key ' ))->getData ();
104
+ $ data2 = $ this ->get ($ this ->getOption ('second_key ' ))->getData ();
98
105
99
- return null ;
106
+ return $ data1 ?: $ data2 ;
100
107
}
101
108
}
Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ public function testSubmitUnequal()
43
43
$ this ->assertEquals ('bar ' , $ this ->field ['second ' ]->getDisplayedData ());
44
44
$ this ->assertFalse ($ this ->field ->isFirstEqualToSecond ());
45
45
$ this ->assertEquals ($ input , $ this ->field ->getDisplayedData ());
46
- $ this ->assertEquals (null , $ this ->field ->getData ());
46
+ $ this ->assertEquals (' foo ' , $ this ->field ->getData ());
47
47
}
48
48
49
49
public function testSubmitEqual ()
@@ -58,4 +58,13 @@ public function testSubmitEqual()
58
58
$ this ->assertEquals ($ input , $ this ->field ->getDisplayedData ());
59
59
$ this ->assertEquals ('foo ' , $ this ->field ->getData ());
60
60
}
61
+
62
+ public function testGetDataReturnsSecondValueIfFirstIsEmpty ()
63
+ {
64
+ $ input = array ('first ' => '' , 'second ' => 'bar ' );
65
+
66
+ $ this ->field ->submit ($ input );
67
+
68
+ $ this ->assertEquals ('bar ' , $ this ->field ->getData ());
69
+ }
61
70
}
You can’t perform that action at this time.
0 commit comments