Skip to content

Commit 372935d

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 dfb1065 commit 372935d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
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: 3 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__
@@ -97,6 +99,7 @@ def conv_String(self, model, field, kwargs):
9799
if field.regex:
98100
kwargs['validators'].append(validators.Regexp(regex=field.regex))
99101
self._string_common(model, field, kwargs)
102+
100103
if 'password' in kwargs:
101104
if kwargs.pop('password'):
102105
return f.PasswordField(**kwargs)

tests/test_forms.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,24 @@ class DogOwner(db.Document):
290290
self.assertEqual(len(choices), 2)
291291
self.assertFalse(choices[0].checked)
292292
self.assertFalse(choices[1].checked)
293-
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)
310+
294311
def test_passwordfield(self):
295312
with self.app.test_request_context('/'):
296313
db = self.db

0 commit comments

Comments
 (0)