1010use Nette ;
1111use Nette \Utils \Html ;
1212use Stringable ;
13- use function array_flip , array_key_first , array_keys , array_merge , explode , func_num_args , in_array , is_array , key , substr ;
13+ use function array_flip , array_key_exists , array_key_first , array_keys , array_merge , explode , func_num_args , in_array , is_array , is_string , key , substr ;
1414
1515
1616/**
@@ -85,6 +85,10 @@ public function getLabel($caption = null): Html
8585 public function getControlPart ($ key = null ): Html
8686 {
8787 $ key = key ([(string ) $ key => null ]);
88+ if (!array_key_exists ($ key , $ this ->getItems ())) {
89+ throw new Nette \InvalidArgumentException ("Item ' $ key' does not exist in field ' {$ this ->getName ()}'. " );
90+ }
91+
8892 return parent ::getControl ()->addAttributes ([
8993 'id ' => $ this ->getHtmlId () . '- ' . $ key ,
9094 'checked ' => in_array ($ key , (array ) $ this ->value , strict: true ),
@@ -100,10 +104,17 @@ public function getControlPart($key = null): Html
100104 */
101105 public function getLabelPart ($ key = null ): Html
102106 {
107+ if (!func_num_args ()) {
108+ return $ this ->getLabel ();
109+ }
110+
111+ $ key = key ([(string ) $ key => null ]);
112+ if (!array_key_exists ($ key , $ this ->getItems ())) {
113+ throw new Nette \InvalidArgumentException ("Item ' $ key' does not exist in field ' {$ this ->getName ()}'. " );
114+ }
115+
103116 $ itemLabel = clone $ this ->itemLabel ;
104- return func_num_args ()
105- ? $ itemLabel ->setText ($ this ->translate ($ this ->getItems ()[$ key ]))->for ($ this ->getHtmlId () . '- ' . $ key )
106- : $ this ->getLabel ();
117+ return $ itemLabel ->setText ($ this ->translate ($ this ->getItems ()[$ key ]))->for ($ this ->getHtmlId () . '- ' . $ key );
107118 }
108119
109120
0 commit comments