i have ip validation rule such :
>>> validate_ipv46_address("1.1.1") traceback (most recent call last): file "<console>", line 1, in <module> file "/usr/local/lib/python2.7/site-packages/django/core/validators.py", line 125, in validate_ipv46_address raise validationerror(_('enter valid ipv4 or ipv6 address.'), code='invalid') validationerror: [u'enter valid ipv4 or ipv6 address.']
and have form functioning like...
class cachecheck(forms.form): type = forms.typedchoicefield(choices=type_choices, initial='fixed') record = forms.typedchoicefield(choices=record_choices, initial='fixed') hostname = forms.charfield(max_length=100) validate_hostname = regexvalidator(regex=r'[a-za-z0-9-_]*\.[a-za-z]{2,6}') def clean(self): cleaned_data = super(cachecheck, self).clean() record = cleaned_data.get("record") hostname = cleaned_data.get("hostname", "") if record == "ptr": validate_ipv46_address(hostname) elif record == "a": validate_hostname(hostname) return cleaned_data
however there few things im unclear on. if enter incorrect ip still passes me cleaned data. cleaned_data method ? how pass validation errors template ?
thanks,
according django documentation code should work , display "an error message @ top of form". won't display error @ correct input element though.
there alternative approach try. assumed validate_ipv46_address()
, validate_hostname()
return boolean instead of raising exception:
def clean(self): cleaned_data = super(cachecheck, self).clean() record = cleaned_data.get("record") hostname = cleaned_data.get("hostname", "") if record == "ptr" , not validate_ipv46_address(hostname): msg = "enter valid ipv4 or ipv6 address." elif record == "a" , not validate_hostname(hostname): msg = "enter valid hostname." if msg: self._errors["hostname"] = self.error_class([msg]) del cleaned_data["hostname"] return cleaned_data
Comments
Post a Comment