Ian, thanks. Your help is much appreciated.
Your `FlexPassword(not_empty=False)` works per the requirements I posted,
but I've got another one I hadn't shared previously the prevents me from
using it.
The crux of my issue is that I'm trying to utilize a single validation
schema for both creating new users and allowing existing users to update
their account.
FlexPassword is named as such because for a new user, it enforces 'not
empty' and 'minimum length' conditions, but for an existing user, it allows
an empty password. When an existing user submits the form to update their
account, they can change whatever fields they want and if they leave their
password blank it won't be updated.
Here's the code:
class FlexPassword(formencode.FancyValidator):
def _to_python(self, value, state):
# Ok for registered users to submit a blank password b/c it will not
be changed
if hasattr(state, 'current_user') and len(value) == 0:
return value
if len(value) == 0:
raise formencode.Invalid('Please enter a value', value, state)
elif len(value) < 6:
raise formencode.Invalid('Enter a value at least 6 characters
long', value, state)
return value
Any other ideas for me?
On Mon, Aug 16, 2010 at 12:17 PM, Ian Wilson <[email protected]>wrote:
> Hi,
>
> I think you want FlexPassword(not_empty=True). FancyValidator handles the
> empty case internally before your method is called. There might be another
> method that overrides this but if you need a custom message for not empty
> then you can just override the 'empty' key in messages with your own
> message.
>
> I looked at the code to justify what I said earlier:
>
> Empty case is handled --
> http://bitbucket.org/ianb/formencode/src/tip/formencode/api.py#cl-406
>
> Your _to_python is used --
> http://bitbucket.org/ianb/formencode/src/tip/formencode/api.py#cl-417
>
> -Ian
>
>
> On Mon, Aug 16, 2010 at 9:56 AM, Ryan <[email protected]> wrote:
>
>> My controller is calling _to_python on a schema object:
>>
>> @validate(schema=UserCreateValidation(), form='new',
>> post_only=True)
>> def create(self):
>> # controller code here...
>>
>> Inside the schema is where I call the validator:
>>
>> class UserCreateValidation(formencode.Schema):
>> password = formencode.All(FlexPassword())
>> # more schema code here...
>>
>> I can't call `FlexPassword#_to_python` because `value` and `state`
>> aren't known by the schema.
>>
>> The `UserCreateValidation` schema includes other validations, so I
>> know the controller is calling it even when the password field is
>> blank. What's happening though, is the schema seems to ignore
>> `password = formencode.All(FlexPassword())` when the password is
>> blank.
>>
>> Do I need to add an additional argument to `formencode#All` to make
>> sure it runs `FlexPassword` even when the value is blank?
>>
>>
>> On Aug 13, 5:49 pm, Eric Rasmussen <[email protected]> wrote:
>> > Hi Ryan,
>> >
>> > In your example below, _to_python always works for me if I call it
>> directly
>> > (as a method of the class). Is there any chance that your controller
>> isn't
>> > calling it when the value is an empty string? It might help to see how
>> your
>> > controller is referencing it.
>> >
>> > Best,
>> > Eric
>> >
>> >
>> >
>> > On Fri, Aug 13, 2010 at 7:46 AM, Ryan <[email protected]> wrote:
>> > > I have a custom validator called `FlexPassword`
>> >
>> > > class FlexPassword(formencode.FancyValidator):
>> > > def _to_python(self, value, state):
>> > > raise formencode.Invalid('validator ran', value, state)
>> >
>> > > Here's my schema:
>> >
>> > > password =
>> > > formencode.All(formencode.validators.PlainText(not_empty=True),
>> > > FlexPassword())
>> >
>> > > As expected, when no characters are entered, the PlainText error is
>> > > raised. Also as expected, when a single character is entered, the
>> > > FlexPassword error is raised.
>> >
>> > > But that's not the schema I want to use. This is:
>> >
>> > > password = formencode.All(FlexPassword())
>> >
>> > > Contrary to expectation, when no characters are entered, the
>> > > FlexPassword error is not raised. Huh? As expected, when a single
>> > > character is entered, the FlexPassword error is raised.
>> >
>> > > The way I've got it wired up, the form should always fail validation,
>> > > but with a blank password, it passes. Any insights for me? Thanks.
>> >
>> > > --
>> > > You received this message because you are subscribed to the Google
>> Groups
>> > > "pylons-discuss" group.
>> > > To post to this group, send email to [email protected].
>> > > To unsubscribe from this group, send email to
>> > > [email protected]<pylons-discuss%[email protected]>
>> <pylons-discuss%2bunsubscr...@go oglegroups.com>
>> > > .
>> > > For more options, visit this group at
>> > >http://groups.google.com/group/pylons-discuss?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "pylons-discuss" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<pylons-discuss%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/pylons-discuss?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<pylons-discuss%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" 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/pylons-discuss?hl=en.