Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullableIntegerField #14

Closed
kmarekspartz opened this issue Oct 2, 2013 · 4 comments
Closed

NullableIntegerField #14

kmarekspartz opened this issue Oct 2, 2013 · 4 comments

Comments

@kmarekspartz
Copy link

I would like a field, which if left blank is None, and otherwise behaves like IntegerField.

As a workaround, we made this class:

class NullableIntegerField(IntegerField):
    """
    An IntegerField where the field can be null if the input data is an empty
    string.
    """

    def process_formdata(self, valuelist):
        if valuelist:
            if valuelist[0] == '':
                self.data = None
            else:
                try:
                    self.data = int(valuelist[0])
                except ValueError:
                    self.data = None
                    raise ValueError(self.gettext('Not a valid integer value'))

Is there a better way to do this? Should I make some tests and a pull request?

@lepture
Copy link
Contributor

lepture commented Oct 2, 2013

IntegerField with DataRequired validator.

@kmarekspartz
Copy link
Author

That doesn't seem to do what I'm looking for, though the Optional validator may be. Let me try it out tomorrow.

@prencher
Copy link
Contributor

prencher commented Oct 2, 2013

It's not the most intuitive thing in the world, but Optional is indeed what you want here, and for any other coercing validator. They are essentially required by default.

We have some plans for 2.0 to address this once and for all, without the slightly kludgey approach of using Optional.

@prencher prencher closed this as completed Oct 2, 2013
@kmarekspartz
Copy link
Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants