custom validator help

16 views
Skip to first unread message

jose

unread,
Jan 26, 2007, 6:08:13 AM1/26/07
to turbo...@googlegroups.com
Hi all,

I created this validator and it works except when it raises the invalid
message, because message is displayed in the right side of all fields in
the form.
I would like to display the message only near the field which generates
the error.
How can I do it?
Thank you,

class DateInizioFine(validators.FancyValidator):
def _to_python(self, value, state):
inizio = value.get('data_inizio_attivita')
fine = value.get('data_fine_attivita')
if fine and inizio > fine:
raise validators.Invalid('la data fine non deve essere
maggiore della data inizio!', value, state)
return value

jo

Alberto Valverde

unread,
Jan 26, 2007, 6:16:58 AM1/26/07
to turbo...@googlegroups.com

This validator is validating a compound widget so it needs to build
an error dict and attach it to the exception. Gonna paste a validator
I have that does exactly the same thing, but in spanish ;)

class ValidadorFechas(FormValidator):
messages = {
'publica_menor' : 'La fecha de publicacion debe ser menor
que la de '
'despublicacion',
'despublica_mayor' : 'La fecha de despublicacion debe ser
mayor que '
'la de publicacion',
}
def validate_python(self, value, state=None):
pub = value.get('fecha_publica', None)
des = value.get('fecha_despublica', None)
if (pub and des) and pub > des:
message = self.message('publica_menor', state)
errors = dict(
fecha_publica = self.message('publica_menor', state),
fecha_despublica = self.message( 'despublica_mayor',
state),
)
raise Invalid(message, value, state, error_dict=errors)

Alberto

jose

unread,
Jan 26, 2007, 10:34:12 AM1/26/07
to turbo...@googlegroups.com
Alberto Valverde wrote:

Thank you Alberto, it works fine!

jo

Reply all
Reply to author
Forward
0 new messages