def clean(self, value): # Have to jump through a few hoops to make this reliable value = super(JSONFormField, self).clean(value) # allow an empty value on an optional field if value is None: return value ## Got to get rid of newlines for validation to work # Data newlines are escaped so this is safe value = value.replace('\r', '').replace('\n', '') if self.evaluate: json_globals = { # "safety" first! '__builtins__': None, 'datetime': datetime, } json_locals = { # value compatibility 'null': None, 'true': True, 'false': False, } try: value = json.dumps(eval(value, json_globals, json_locals), **self.encoder_kwargs) except Exception as e: # eval can throw many different errors raise utils.ValidationError(str(e)) try: return json.loads(value, **self.decoder_kwargs) except ValueError as e: raise utils.ValidationError(str(e))
def clean(self): super(MyConfirmruleAdminForm, self).clean() if self.cleaned_data['ruletype'] == 'route': if not self.cleaned_data['idroute']: raise django_forms_util.ValidationError( 'For ruletype "route" it is required to indicate a route.') elif self.cleaned_data['ruletype'] == 'channel': if not self.cleaned_data['idchannel']: raise django_forms_util.ValidationError( 'For ruletype "channel" it is required to indicate a channel.' ) elif self.cleaned_data['ruletype'] == 'frompartner': if not self.cleaned_data['frompartner']: raise django_forms_util.ValidationError( 'For ruletype "frompartner" it is required to indicate a frompartner.' ) elif self.cleaned_data['ruletype'] == 'topartner': if not self.cleaned_data['topartner']: raise django_forms_util.ValidationError( 'For ruletype "topartner" it is required to indicate a topartner.' ) elif self.cleaned_data['ruletype'] == 'messagetype': if not self.cleaned_data['messagetype']: raise django_forms_util.ValidationError( 'For ruletype "messagetype" it is required to indicate a messagetype.' ) return self.cleaned_data
def clean(self): super(MyRouteAdminForm, self).clean() if self.cleaned_data[ 'fromchannel'] and self.cleaned_data['translateind'] != 2 and ( not self.cleaned_data['fromeditype'] or not self.cleaned_data['frommessagetype']): raise django_forms_util.ValidationError( 'When using an inchannel and not pass-through, both "fromeditype" and "frommessagetype" are required.' ) return self.cleaned_data
def clean(self, value): """ Validates that the input is a valid BiblePassage. Returns a Unicode object. """ value = super(BiblePassageFormField, self).clean(value) if value == u'': return value try: return unicode(to_passage(value)) except InvalidPassage, e: raise utils.ValidationError(e)
def clean(self): super(MyTranslateAdminForm, self).clean() blub = models.translate.objects.filter( fromeditype=self.cleaned_data['fromeditype'], frommessagetype=self.cleaned_data['frommessagetype'], alt=self.cleaned_data['alt'], frompartner=self.cleaned_data['frompartner'], topartner=self.cleaned_data['topartner']) if blub and (self.instance.pk is None or self.instance.pk != blub[0].id): raise django_forms_util.ValidationError( 'Combination of fromeditype,frommessagetype,alt,frompartner,topartner already exists.' ) return self.cleaned_data