Post by Ed HillisI would like to have a compound validator give the error message for each of
it elements. For example, if my field could not be empty and it required an
field: 'Please enter something, Must be an integer'
instead of just showing one or the other. Is there a built-in way to do
this? I am completely new here, and may be overlooking any obvious solution.
Thanks -Ed
I'm not quite sure what you mean. I use the following for select lists
whose value is an int:
===
class SelectInt(v.FancyValidator):
"""A combination of the Int and OneOf validators with a custom message"""
__unpackargs__ = ("list",)
not_empty = True
def _to_python(self, value, state):
try:
return int(value)
except ValueError:
self._invalid(value, state)
_from_python = _to_python
def validate_python(self, value, state):
if value not in self.list:
self._invalid(value, state)
def _invalid(self, value, state):
message = "please choose an item from the list"
raise v.Invalid(message, value, state)
===
I also use a custom error message for regular int validators:
===
class Int(v.Int):
messages = {
"integer": _("Please enter a numeric value"),
}
===
If by "compound validator" you mean that it validates several widgets
together, you can do that with a chained validator, which takes a dict
of values and error messages rather than scalar strings.
For nested validators (e.g., a latitude-longitude widget with multiple
fields for degrees, minutes, and seconds), you'd create a Schema
validator for it, and make sure that 'pre_validators =
[NestedVariables()]'. Then the Python value of the subwidget would
appear as a nested dict in Python, and your form would contain widgets
like this:
.<input name="lat_lon.lat_deg" type="text" />
--
Mike Orr <***@gmail.com>