Validation libraries tend to have a way to pass some user-defined state
down through the validators. It looks like in formencode you can do this as
well via the state argument [1]. You would likely want to pass a dict down
containing either the request or the dbsession and then your validators can
access that attribute of the state.

[1] http://www.formencode.org/en/latest/Validator.html#state

On Sun, Apr 8, 2018 at 8:35 AM, André Prado <andreprad...@gmail.com> wrote:

> Hi folks,
>
> I want to make a schema + a form connecting to SQLAlchemy and be able to
> identify if an username is unique and emails are unique. I am using these
> as a base:
>
> https://docs.pylonsproject.org/projects/pyramid-simpleform/en/latest/
> https://github.com/thruflo/pyramid_simpleauth/blob/
> master/src/pyramid_simpleauth/schema.py#L212
> https://github.com/thruflo/pyramid_simpleauth/blob/
> master/src/pyramid_simpleauth/model.py#L290
>
> For example,
>
> class Signup(formencode.Schema):
>     """Form fields to render and validate for signup."""
>
>     allow_extra_fields=True
>     filter_extra_fields=True
>     username = UniqueUsername(not_empty=True)
>     email = UniqueEmail(resolve_domain=False, not_empty=True)
>     password = Password(not_empty=True)
>     confirm = Password(not_empty=True)
>     chained_validators = [
>         validators.FieldsMatch(
>             'password',
>             'confirm'
>         )
>     ]
>
>
> In the example from simpleauth he has a function called get_existing_user
> that UniqueUsername is calling in the _validate_python method.
>
> However the get_existing_user is accessing a global SQLAlchemy session
> which is always exposed: https://github.com/thruflo/
> pyramid_basemodel/blob/master/src/pyramid_basemodel/__init__.py#L54
>
> I wanted to use what the current Pyramid documentation suggests, which is,
> having a request.dbsession that you propagate through your code instead of
> a global variable.
>
> For example: https://github.com/Pylons/pyramid-cookiecutter-alchemy/
> blob/latest/%7B%7Bcookiecutter.repo_name%7D%7D/%7B%7Bcookiecutter.repo_
> name%7D%7D/models/__init__.py#L52
>
>
> Is there any way to make pyramid_simpleform / formencode receive the
> request.dbsession?
>
> I couldn't figure out from the docs, tried some parameters like state and
> extra but no juice. In the end I did something like:
>
> from pyramid.threadlocal import get_current_request
> request=get_current_request().dbsession
>
> Which the Pyramid docs tells not to do and I don't want to.
>
> Thanks!
>
>
>
> --
> Atenciosamente/Regards
> André Castelan Prado
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pylons-discuss+unsubscr...@googlegroups.com.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/pylons-discuss/CAG%3D2wZeJ3SbCmFbfhdOvPE1aJ9f%
> 3DPYrF8YAx%2BN5A7fWd%3DFhsMg%40mail.gmail.com
> <https://groups.google.com/d/msgid/pylons-discuss/CAG%3D2wZeJ3SbCmFbfhdOvPE1aJ9f%3DPYrF8YAx%2BN5A7fWd%3DFhsMg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAKdhhwG5geVvmRbW_2kq%3D55EEKoNZVLDqnV_5SUUabM8SS8pbw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to