@@ -262,20 +262,20 @@ class URLField(DefaultFieldMixin, fields.URLField):
262262 pass
263263
264264
265- class MultipleFieldMixin (DefaultFieldMixin ):
266- def get_multiple_choices_required (self ):
267- """
268- Add only the required message, but no 'ng-required' attribute to the input fields,
269- otherwise all Checkboxes of a MultipleChoiceField would require the property "checked".
270- """
271- errors = []
272- if self . required :
273- msg = _ ( "At least one checkbox has to be selected." )
274- errors . append (( '$error.multifield' , msg ) )
265+ class RadioFieldMixin (DefaultFieldMixin ):
266+ def get_potential_errors (self ):
267+ if isinstance ( self . widget , widgets . RadioSelect ):
268+ errors = []
269+ for key , msg in self . get_input_required_errors ():
270+ if key == '$error.required' :
271+ msg = _ ( "At least one radio button has to be selected." )
272+ errors . append (( key , msg ))
273+ else :
274+ errors = self . get_input_required_errors ( )
275275 return errors
276276
277277
278- class ChoiceField (MultipleFieldMixin , fields .ChoiceField ):
278+ class ChoiceField (RadioFieldMixin , fields .ChoiceField ):
279279 def __init__ (self , * args , ** kwargs ):
280280 super (ChoiceField , self ).__init__ (* args , ** kwargs )
281281 if isinstance (self .widget , widgets .Select ) and self .initial is None and len (self .choices ):
@@ -284,13 +284,6 @@ def __init__(self, *args, **kwargs):
284284 def has_subwidgets (self ):
285285 return isinstance (self .widget , widgets .RadioSelect )
286286
287- def get_potential_errors (self ):
288- if isinstance (self .widget , widgets .RadioSelect ):
289- errors = self .get_multiple_choices_required ()
290- else :
291- errors = self .get_input_required_errors ()
292- return errors
293-
294287 def update_widget_attrs (self , bound_field , attrs ):
295288 bound_field .form .update_widget_attrs (bound_field , attrs )
296289 if isinstance (self .widget , widgets .RadioSelect ):
@@ -314,16 +307,24 @@ def get_converted_widget(self, widgets_module):
314307 return new_widget
315308
316309
317- class ModelChoiceField (MultipleFieldMixin , model_fields .ModelChoiceField ):
310+ class ModelChoiceField (RadioFieldMixin , model_fields .ModelChoiceField ):
318311 pass
319312
320313
321- class TypedChoiceField (MultipleFieldMixin , fields .TypedChoiceField ):
322- def get_potential_errors (self ):
323- if isinstance (self .widget , widgets .RadioSelect ):
324- errors = self .get_multiple_choices_required ()
325- else :
326- errors = self .get_input_required_errors ()
314+ class TypedChoiceField (RadioFieldMixin , fields .TypedChoiceField ):
315+ pass
316+
317+
318+ class MultipleFieldMixin (DefaultFieldMixin ):
319+ def get_multiple_choices_required (self ):
320+ """
321+ Add only the required message, but no 'ng-required' attribute to the input fields,
322+ otherwise all Checkboxes of a MultipleChoiceField would require the property "checked".
323+ """
324+ errors = []
325+ if self .required :
326+ msg = _ ("At least one checkbox has to be selected." )
327+ errors .append (('$error.multifield' , msg ))
327328 return errors
328329
329330
0 commit comments