File tree Expand file tree Collapse file tree 2 files changed +11
-6
lines changed
src/Symfony/Component/DomCrawler Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -232,22 +232,22 @@ protected function initialize()
232
232
233
233
$ found = false ;
234
234
foreach ($ this ->xpath ->query ('descendant::option ' , $ this ->node ) as $ option ) {
235
- $ this ->options [] = $ this ->buildOptionValue ($ option );
235
+ $ optionValue = $ this ->buildOptionValue ($ option );
236
+ $ this ->options [] = $ optionValue ;
236
237
237
238
if ($ option ->getAttribute ('selected ' )) {
238
239
$ found = true ;
239
240
if ($ this ->multiple ) {
240
- $ this ->value [] = $ option -> getAttribute ( 'value ' ) ;
241
+ $ this ->value [] = $ optionValue [ 'value ' ] ;
241
242
} else {
242
- $ this ->value = $ option -> getAttribute ( 'value ' ) ;
243
+ $ this ->value = $ optionValue [ 'value ' ] ;
243
244
}
244
245
}
245
246
}
246
247
247
248
// if no option is selected and if it is a simple select box, take the first option as the value
248
- $ option = $ this ->xpath ->query ('descendant::option ' , $ this ->node )->item (0 );
249
- if (!$ found && !$ this ->multiple && $ option instanceof \DOMElement) {
250
- $ this ->value = $ option ->getAttribute ('value ' );
249
+ if (!$ found && !$ this ->multiple && !empty ($ this ->options )) {
250
+ $ this ->value = $ this ->options [0 ]['value ' ];
251
251
}
252
252
}
253
253
}
Original file line number Diff line number Diff line change @@ -280,6 +280,11 @@ public function testOptionWithNoValue()
280
280
{
281
281
$ node = $ this ->createSelectNodeWithEmptyOption (array ('foo ' => false , 'bar ' => false ));
282
282
$ field = new ChoiceFormField ($ node );
283
+ $ this ->assertEquals ('foo ' , $ field ->getValue ());
284
+
285
+ $ node = $ this ->createSelectNodeWithEmptyOption (array ('foo ' => false , 'bar ' => true ));
286
+ $ field = new ChoiceFormField ($ node );
287
+ $ this ->assertEquals ('bar ' , $ field ->getValue ());
283
288
$ field ->select ('foo ' );
284
289
$ this ->assertEquals ('foo ' , $ field ->getValue (), '->select() changes the selected option ' );
285
290
}
You can’t perform that action at this time.
0 commit comments