On 26 mai, 15:35, Jorge Godoy <[EMAIL PROTECTED]> wrote:
>

Hi Jorge, thanks for your valuable answer.

> I'd rather use a custom validator.  The logic is clearer, you write less
> repetitive code, you reuse more components, no changes to TG are required,
> you can have all that *now*.

I rewrote my sample using a chained validator doing all the job of
user creation.
I put it at the end, but I didn't test it for real !

1. I put most of the code from the controller in the validator.
2. The validator extend the field_dict it got for validation with some
result of the processing
3. These new values can then be used by the controller notice the
user.

(2) is the sensitive part. I know it works, but I don't know if I can
do that.

>
> Also, your changes imply in changing how TG is working with FormEncode.
>
> What I see is that this would bring more harm than good and would add extra
> code that needed to be maintained in TG.

This is the price for any new feature :-)

>
> On the other hand, nothing prevents you from coding that: you can create new
> decorators and make them do what you want.  There are three dicts that you
> can use  (that I remember right now, maybe there's something more): you can
> get your submited data with a **kwargs dictionary, you can check (/set?)
> validation errors on tg_errors and you can send back values on a dict of your
> own to be redisplayed on the form.

I don't understand how @validate and @error_handler can do their job.
To replay the controller that display the form, @error_handler should
have a copy of the previous request, to redo exactly the same.
Second after the replay of the controller, @error_handler should
modify the returned data to enforce the one the user filled in in the
previous screen !
Can you clarify ? If you know :-)

>
> So, even though there's no written code on TG core that does what you want,
> you can get it to work like that in, lets say, one weekend.

I looked the @validate and @error_handler code but no easy to
understand ....
This will require more than a weekend :-)

Anyway if I get lots of support from people :-) ... maybe

Here is my code.
Is it what you had in mind ?

class NewUserValidation(formencode.FormValidator):

    def validate_python(self, field_dict, state):
        if field_dict['password1']!=field_dict['password2']:
            raise turbogears.validators.Invalid('Passwords dont
match', field_dict, state, error_dict={'password2': 'Password dont
match' })

        newuser=model.User(user_name=user_name, password=password)
        session.save(newuser)
        try:
            session.flush()
        except:
            raise turbogears.validators.Invalid('Username already in
use', field_dict, state, error_dict={'user_name': 'choose another
username' })

        # Extend field_dict with interesting results

        field_dict['user_id']=newuser.user_id


class FormNewUser(widgets.TableForm):
    fields = [
        widgets.TextField('user_name', label='User login',
validator=validators.String(not_empty=True)),
        widgets.PasswordField('password', label="Password" ,
validator=validators.NotEmpty ()) ,
        widgets.PasswordField('password2', label="Password (Repeat)" ,
validator=validators.NotEmpty()) ,
    ]
    validator = validators.Schema(
        chained_validators = [NewUserValidation()]
    )
    submit_text = "Create User"


@turbogears.expose()
@validate(form=new_user_form)
@error_handler(index)
def user_create(self, user_id, **kwargs):

    turbogears.flash("User successfuly added, user_id=!" % user_id)
    raise turbogears.redirect('show_user', dict(user_id=user_id))


--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to