custom error message for form fields.

111 views
Skip to first unread message

Mengu

unread,
Dec 14, 2011, 6:39:32 PM12/14/11
to ToscaWidgets-discuss
hello everyone,

i need some help on adding custom error messages to the form fields. i
have a signup form:

class SigninFields(WidgetsList):
email =
forms.TextField(validator=forms.validators.Email(not_empty=True))
password =
forms.PasswordField(validator=forms.validators.NotEmpty)

signin_form = forms.TableForm(fields=SigninFields(), action="login")

in my controller, i use @validate decorator so in case of invalid
input it handles them, not me.

now, after the initial validation check i have to check if there is
any users with the given email and password. if not, i want to raise
an Invalid exception and i want to display it next to the "email"
field where normally it would display the "please enter a value" error
message.

thanks in advance.

Mengu

unread,
Dec 14, 2011, 7:12:42 PM12/14/11
to ToscaWidgets-discuss
okay, thanks to rzn in #turbogears, we found a way to do it but it
feels hacky. my current solution right now is: http://pastie.org/private/aykh15fjssgo21ejuihrg
i'm also adding the codes here just in case it can be deleted. please
let me know if there is just a better way to do this.

thanks again.

@expose('app.templates.users.signin')
def signin(self, email=None, password=None):
if tmpl_context.form_errors.has_key('_the_form'):
tmpl_context.signin_form =
signin_form(error=tmpl_context.validation_exception)
else:
tmpl_context.signin_form = signin_form()
return dict()

@expose()
@validate(signin_form, error_handler=signin)
def login(self, email, password):
password = md5(password).hexdigest()
user =
self.user_service.get_user_by_email_and_password(email=email,
password=password)
if not user:
raise Invalid('No user matched with the given info',
email, None,
error_dict={'email': 'No user matched with the
given info.'})
session["user_id"] = user.id
session["name"] = "%s %s" % (user.firstname, user.lastname)
session["usergroup_id"] = user.usergroup_id
session.save()

Alessandro Molina

unread,
Dec 15, 2011, 6:20:39 AM12/15/11
to toscawidge...@googlegroups.com
I would suggest sublcassing the validators.Email creating a UserEmail
validator that checks for email format and checks if the user exists.
Then if you want to check the user and password together you can set
as form validator a schema that has a chained_validator, of just go
for the easy hack and check it inside the UserEmail from the environ.

> --
> You received this message because you are subscribed to the Google Groups "ToscaWidgets-discuss" group.
> To post to this group, send email to toscawidge...@googlegroups.com.
> To unsubscribe from this group, send email to toscawidgets-dis...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/toscawidgets-discuss?hl=en.
>

Mengu

unread,
Dec 15, 2011, 9:22:45 AM12/15/11
to ToscaWidgets-discuss
hi alessandro,
thanks for the response. you gave me the idea. so this is how i'm
rolling it now:
from tg import requestfrom tw.forms.validators import All,
FancyValidator
class ExistingUser(FancyValidator):    def to_python(self, value,
state):        password = request.POST.get('password')        email =
request.POST.get('email')        user = get_user_from_db(email,
password)        if not user:            raise Invalid("No user found
with this info", value, state)        return value
class SigninFields(WidgetsList):    email =
forms.TextField(validator=All(forms.validators.Email(not_empty=True),
ExistingUser))    password =
forms.PasswordField(validator=forms.validators.NotEmpty)
so if forms fail with @validate it is directly redirected to my signin
page and display the error next to the email field.
thanks again everyone. hope this helps to other tw users as well.
On Dec 15, 1:20 pm, Alessandro Molina <alessandro.mol...@gmail.com>
wrote:

Mengu

unread,
Dec 15, 2011, 9:23:57 AM12/15/11
to ToscaWidgets-discuss
i hate google groups when it kills the formatting. code can be found
at http://paste.ofcode.org/wu2Ay875T3jHRxqsvYiMry.
Reply all
Reply to author
Forward
0 new messages