Skip to content

Commit 6d17618

Browse files
author
Garito
committed
Added RadioField to model_form
Adding {“radio”: True} to the field’s form_args when you create the form, instead of a SelectField, the field will render a RadioField
1 parent dbb904e commit 6d17618

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ that much better:
2727
* Peter D. Gray
2828
* Massimo Santini
2929
* Len Buckens - https://github.com/buckensl
30+
* Garito - https://github.com/garito

flask_mongoengine/wtf/orm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def convert(self, model, field, field_args):
6868
kwargs["coerce"] = self.coerce(ftype)
6969
if kwargs.pop('multiple', False):
7070
return f.SelectMultipleField(**kwargs)
71+
if kwargs.pop('radio', False):
72+
return f.RadioField(**kwargs)
7173
return f.SelectField(**kwargs)
7274

7375
ftype = type(field).__name__

tests/test_forms.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,23 @@ class DogOwner(db.Document):
290290
self.assertEqual(len(choices), 2)
291291
self.assertFalse(choices[0].checked)
292292
self.assertFalse(choices[1].checked)
293+
294+
def test_modelradiofield(self):
295+
with self.app.test_request_context('/'):
296+
db = self.db
297+
298+
choices = (('male', 'Male'), ('female', 'Female'), ('other', 'Other'))
299+
300+
class Poll(db.Document):
301+
answer = db.StringField(choices=choices)
302+
303+
PollForm = model_form(Poll, field_args={'answer': {'radio': True}})
304+
305+
form = PollForm(answer=None)
306+
self.assertTrue(form.validate())
307+
308+
self.assertEqual(form.answer.type, 'RadioField')
309+
self.assertEqual(form.answer.choices, choices)
293310

294311
def test_passwordfield(self):
295312
with self.app.test_request_context('/'):

0 commit comments

Comments
 (0)