On Sep 5, 2006, at 7:08 PM, Zanfa wrote:
>
> Im trying to create somekind of registration and I ran into 2
> problems.
> First, I made a form and used some validators and everything went fine
> but when I wanted to add FieldsMatch(for checking if passwords match)
> validator I couldn't find a way.
You'll need to add the FieldsMatch validator to the
chained_validators parameter of the Schema that will validate the form:
register_form = widgets.TableForm(
fields = RegisterFields(),
submit_text = "Register",
validator = Schema(chained_validators=[FieldsMatch("password",
"password2")])
)
The form will take care of appending the widgets' validators into the
effective schema it will use for validation.
> Second, I need to check if someone has already registered under
> desired
> username and I have no idea how to make this.
You have two ways of doing this:
a) making a validators.String subclass that will hit the database to
check if the username is already there and using it as a validator in
the "username" TextField, something like:
class UsernameValidator(validators.String):
def validate_python(self, username, state=None):
try:
Users.byUsername(username)
raise validators.Invalid("The username provided already
exists")
except LookupError:
pass
b) using errorhandling to dispatch on the IntegrityError exception
that will get raised when the database complains (if username is a pk
or has an unique index on it):
@expose()
def regsiter(...., tg_exceptions=None):
if tg_exceptions:
# do something useful
.....
@exception_handler(register, "isinstance(tg_exceptions, IntegrityError)"
@error_handler(register)
@expose(...)
def register_form(....)
I would personally go with 1) because it's easier, more portable
(different db backends raise different exceptions on an integrity
error) and the error message get's displayed beside the username
field. All must be said, you have to cope with and extra db hit (if
already using SO... who cares? ;) )
> Ok my code:
>
> class RegisterFields(widgets.WidgetsList):
> username = widgets.TextField(name="username", label="Username:",
> validator=validators.NotEmpty())
> password = widgets.PasswordField(name="password",
> label="Password:",
> validator=validators.NotEmpty())
> password2 = widgets.PasswordField(name="password2",
> label="Password
> x2:",
> validator=validators.NotEmpty())
> email = widgets.TextField(name="email", label="E-mail:",
> validator=validators.Email
> (not_empty=True))
> firstname = widgets.TextField(name="firstname",
> label="Firstname:",
> validator=validators.NotEmpty())
> lastname = widgets.TextField(name="lastname", label="Lastname:",
> validator=validators.NotEmpty())
>
> register_form = widgets.TableForm(fields=RegisterFields(),
> submit_text="Register")
>
>
> @expose()
> @validate(form=register_form)
> @error_handler(register)
> def registerform(self, username, password, password2, email,
> firstname, lastname):
> return username +" "+ password +" "+ email +" "+ firstname +"
> "+ lastname
Just one note, I would change those NotEmptys with somehting like
String(not_empty=True) to do the same thing. I can't rememeber now,
but I *think* there was a good reason for it.
Alberto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---