class RoomCoinForm(forms.Form):
account_id = forms.IntegerField(required=True)
play_type = forms.IntegerField(required=True, error_messages={'required': _('请选择游戏')})
min_amount = forms.FloatField(required=False)
max_amount = forms.FloatField(required=False)
count_down = forms.IntegerField(required=False, min_value=1, max_value=60)
password = forms.CharField(required=True, error_messages={'required': _('请输入密码')})
验证单个属性
def clean_play_type(self):
play_type = self.cleaned_data['play_type']
if play_type != PLAY_TYPE_SCORE:
raise forms.ValidationError(_('类型错误'))
return play_type
# 验证多个属性
def clean(self):
cleaned_data = super(RoomCoinForm, self).clean()
min_amount = cleaned_data.get('min_amount')
max_amount = cleaned_data.get('max_amount')
if not cleaned_data.get('count_down'):
cleaned_data['count_down'] = 30
if not min_amount:
cleaned_data['min_amount'] = 0.1
if not max_amount:
cleaned_data['max_amount'] = 9999999999
if cleaned_data['count_down'] > cleaned_data.get('max_amount'):
msg = _("最小值不能大于最大值")
raise forms.ValidationError(msg)
return cleaned_data