@@ -301,18 +301,17 @@ public static function endForm() {
301301 */
302302 public static function checkBox ($ name , $ checked = null , $ htmlAttributes = array ()) {
303303 $ htmlAttributes = $ htmlAttributes === null ? array () : $ htmlAttributes ;
304- $ model = self ::$ viewContext ->model ;
305304
306- if (self ::getModelValue ($ model , $ name , $ modelValue ) === true && $ modelValue === true ) {
305+ if (self ::getModelValue ($ name , $ modelValue, \ FILTER_VALIDATE_BOOLEAN ) === true && $ modelValue === true ) {
307306 $ htmlAttributes ['checked ' ] = 'checked ' ;
308307 $ checked = null ;
309308 }
310309
311- if ($ checked === true ) {
310+ if (filter_var ( $ checked, \ FILTER_VALIDATE_BOOLEAN ) === true ) {
312311 $ htmlAttributes ['checked ' ] = 'checked ' ;
313312 }
314313
315- return self ::input ($ name , 'checkbox ' , $ value , $ htmlAttributes );
314+ return self ::input ($ name , 'checkbox ' , ' true ' , $ htmlAttributes, true );
316315 }
317316
318317 /**
@@ -328,25 +327,27 @@ public static function checkBox($name, $checked = null, $htmlAttributes = array(
328327 public static function dropDownList ($ name , $ list , $ selectedValue = null , $ htmlAttributes = array ()) {
329328 $ result = '' ;
330329
331- $ model = self ::$ viewContext ->model ;
332-
333- if (self ::getModelValue ($ model , $ name , $ modelValue ) === true ) {
330+ if (self ::getModelValue ($ name , $ modelValue ) === true ) {
334331 $ selectedValue = $ modelValue ;
335332 }
336333
337334 $ htmlAttributes = $ htmlAttributes === null ? array () : $ htmlAttributes ;
338- $ htmlAttributes ['name ' ] = $ name ;
335+ $ htmlAttributes ['name ' ] = isset ( $ htmlAttributes [ ' name ' ]) ? $ htmlAttributes [ ' name ' ] : $ name ;
339336
340337 $ result .= '<select ' . self ::buildAttributes ($ htmlAttributes ) . '> ' ;
341338
342339 $ groups = array_filter ($ list , function ($ item ) {
343- return ! isset ($ item ->group );
340+ return $ item instanceof SelectListItem && isset ($ item ->group );
344341 });
345342
346343 $ groups = array_unique (array_map (function ($ item ) {
347344 return $ item ->group ;
348345 }, $ groups ), SORT_REGULAR );
349346
347+ if (count ($ groups ) == 1 && $ groups [0 ] === null ) {
348+ $ groups = array ();
349+ }
350+
350351 if (!empty ($ groups )) {
351352 $ listByGroup = array ();
352353
@@ -370,6 +371,14 @@ public static function dropDownList($name, $list, $selectedValue = null, $htmlAt
370371 $ result .= self ::getSelectOptions ($ listByGroup , $ selectedValue );
371372 $ result .= '</optgroup> ' ;
372373 }
374+
375+ $ listByGroup = array_filter ($ list , function ($ item ) {
376+ return $ item ->group === null ;
377+ });
378+
379+ if (!empty ($ listByGroup )) {
380+ $ result .= self ::getSelectOptions ($ listByGroup , $ selectedValue );
381+ }
373382 }
374383 else {
375384 $ result .= self ::getSelectOptions ($ list , $ selectedValue );
@@ -521,9 +530,7 @@ public static function radioButton($name, $value = null, $checked = null, $htmlA
521530 throw new \Exception ('The $value must not be null if $checked is also null and no "checked" entry exists in $htmlAttributes. ' );
522531 }
523532
524- $ model = self ::$ viewContext ->model ;
525-
526- if (self ::getModelValue ($ model , $ name , $ modelValue ) === true ) {
533+ if (self ::getModelValue ($ name , $ modelValue ) === true ) {
527534 if ($ modelValue == $ value ) {
528535 $ htmlAttributes ['checked ' ] = 'checked ' ;
529536 $ checked = null ;
@@ -551,11 +558,9 @@ public static function radioButton($name, $value = null, $checked = null, $htmlA
551558 public static function textArea ($ name , $ value = '' , $ rows = null , $ columns = null , $ htmlAttributes = array ()) {
552559 $ htmlAttributes = $ htmlAttributes === null ? array () : $ htmlAttributes ;
553560
554- $ htmlAttributes ['name ' ] = $ name ;
561+ $ htmlAttributes ['name ' ] = isset ( $ htmlAttributes [ ' name ' ]) ? $ htmlAttributes [ ' name ' ] : $ name ;
555562
556- $ model = self ::$ viewContext ->model ;
557-
558- if (self ::getModelValue ($ model , $ name , $ modelValue ) === true ) {
563+ if (self ::getModelValue ($ name , $ modelValue ) === true ) {
559564 $ value = $ modelValue ;
560565 }
561566
@@ -635,14 +640,13 @@ public static function getModelState() {
635640 *
636641 * @return string
637642 */
638- private static function input ($ name , $ type = 'text ' , $ value = null , $ htmlAttributes = null ) {
643+ private static function input ($ name , $ type = 'text ' , $ value = null , $ htmlAttributes = null , $ ignoreModelValue = false ) {
639644 $ htmlAttributes = $ htmlAttributes === null ? array () : $ htmlAttributes ;
640- $ model = self ::$ viewContext ->model ;
641645
642- $ htmlAttributes ['type ' ] = $ type ;
643- $ htmlAttributes ['name ' ] = $ name ;
646+ $ htmlAttributes ['type ' ] = isset ( $ htmlAttributes [ ' type ' ]) ? $ htmlAttributes [ ' type ' ] : $ type ;
647+ $ htmlAttributes ['name ' ] = isset ( $ htmlAttributes [ ' name ' ]) ? $ htmlAttributes [ ' name ' ] : $ name ;
644648
645- if (self ::getModelValue ($ model , $ name , $ modelValue ) === true ) {
649+ if ($ ignoreModelValue !== true && self ::getModelValue ($ name , $ modelValue ) === true ) {
646650 $ value = $ modelValue ;
647651 }
648652
@@ -698,27 +702,50 @@ private static function encodeHtmlAttributeString($value) {
698702 return str_replace ('" ' , '" ' , $ value );
699703 }
700704
701- private static function getModelValue ($ model , $ name , &$ value ) {
705+ /**
706+ * Gets value from model state or model.
707+ *
708+ * @param string $name The name of model item.
709+ * @param mixed $value The result.
710+ * @param int $filter See http://php.net/manual/en/filter.filters.php
711+ *
712+ * @return bool
713+ */
714+ private static function getModelValue ($ name , &$ value , $ filter = \FILTER_DEFAULT ) {
715+ if (empty (self ::$ viewContext ->modelState ->items )) {
716+ $ model = self ::$ viewContext ->model ;
717+ }
718+ else {
719+ if (isset (self ::$ viewContext ->modelState ->items [$ name ])) {
720+ $ model = self ::$ viewContext ->modelState ->items ;
721+ }
722+ else {
723+ $ model = self ::$ viewContext ->model ;
724+ }
725+ }
726+
702727 if (empty ($ model )) {
703- $ value = null ;
728+ $ value = filter_var ( null , $ filter ) ;
704729 return false ;
705730 }
706-
731+
707732 if (is_object ($ model )) {
708733 if (isset ($ model ->$ name )) {
709- $ value = $ model ->$ name ;
734+ $ value = filter_var ( $ model ->$ name, $ filter ) ;
710735 return true ;
711736 }
712737 else {
738+ $ value = filter_var (null , $ filter );
713739 return false ;
714740 }
715741 }
716742 elseif (is_array ($ model )) {
717743 if (isset ($ model [$ name ])) {
718- $ value = $ model [$ name ];
744+ $ value = filter_var ( $ model [$ name ] instanceof ModelStateEntry ? $ model [ $ name ]-> value : $ model [ $ name ], $ filter ) ;
719745 return true ;
720746 }
721747 else {
748+ $ value = filter_var (null , $ filter );
722749 return false ;
723750 }
724751 }
0 commit comments