@@ -133,4 +133,80 @@ private static function normalizeMaxLength($model, $attribute, &$options)
133133 }
134134 }
135135 }
136+
137+ /**
138+ * Generates a radio button tag together with a label for the given model attribute.
139+ * This method will generate the "checked" tag attribute according to the model attribute value.
140+ * @param Model $model the model object
141+ * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
142+ * about attribute expression.
143+ * @param array $options the tag options in terms of name-value pairs.
144+ * See [[booleanInput()]] for details about accepted attributes.
145+ *
146+ * @return string the generated radio button tag
147+ */
148+ public static function activeRadio ($ model , $ attribute , $ options = [])
149+ {
150+ return static ::activeBooleanInput ('radio ' , $ model , $ attribute , $ options );
151+ }
152+
153+ /**
154+ * Generates a checkbox tag together with a label for the given model attribute.
155+ * This method will generate the "checked" tag attribute according to the model attribute value.
156+ * @param Model $model the model object
157+ * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
158+ * about attribute expression.
159+ * @param array $options the tag options in terms of name-value pairs.
160+ * See [[booleanInput()]] for details about accepted attributes.
161+ *
162+ * @return string the generated checkbox tag
163+ */
164+ public static function activeCheckbox ($ model , $ attribute , $ options = [])
165+ {
166+ return static ::activeBooleanInput ('checkbox ' , $ model , $ attribute , $ options );
167+ }
168+
169+ /**
170+ * Generates a boolean input
171+ * This method is mainly called by [[activeCheckbox()]] and [[activeRadio()]].
172+ * @param string $type the input type. This can be either `radio` or `checkbox`.
173+ * @param Model $model the model object
174+ * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format
175+ * about attribute expression.
176+ * @param array $options the tag options in terms of name-value pairs.
177+ * See [[booleanInput()]] for details about accepted attributes.
178+ * @return string the generated input element
179+ * @since 2.0.9
180+ */
181+ protected static function activeBooleanInput ($ type , $ model , $ attribute , $ options = [])
182+ {
183+ $ name = isset ($ options ['name ' ]) ? $ options ['name ' ] : static ::getInputName ($ model , $ attribute );
184+ $ value = static ::getAttributeValue ($ model , $ attribute );
185+
186+ if (!array_key_exists ('value ' , $ options )) {
187+ $ options ['value ' ] = '1 ' ;
188+ }
189+ if (!array_key_exists ('uncheck ' , $ options )) {
190+ $ options ['uncheck ' ] = '0 ' ;
191+ } elseif ($ options ['uncheck ' ] === false ) {
192+ unset($ options ['uncheck ' ]);
193+ }
194+ if (!array_key_exists ('label ' , $ options )) {
195+ $ options ['label ' ] = static ::encode ($ model ->getAttributeLabel (static ::getAttributeName ($ attribute )));
196+ } elseif ($ options ['label ' ] === false ) {
197+ unset($ options ['label ' ]);
198+ }
199+
200+ if (isset ($ options ['label ' ])) {
201+ $ options ['label ' ] = '<span> ' . $ options ['label ' ] . '</span> ' ;
202+ }
203+
204+ $ checked = "$ value " === "{$ options ['value ' ]}" ;
205+
206+ if (!array_key_exists ('id ' , $ options )) {
207+ $ options ['id ' ] = static ::getInputId ($ model , $ attribute );
208+ }
209+
210+ return static ::$ type ($ name , $ checked , $ options );
211+ }
136212}
0 commit comments