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

StringField default value does not work #225

Closed
hector opened this Issue Oct 23, 2015 · 4 comments

Comments

2 participants
@hector
Copy link

hector commented Oct 23, 2015

When setting a default value to a StringField it will not use this default value in case formdata has been provided. The problem comes from this line

class StringField(Field):
    """
    This field is the base for most of the more complicated fields, and
    represents an ``<input type="text">``.
    """
    widget = widgets.TextInput()

    def process_formdata(self, valuelist):
        if valuelist:
            self.data = valuelist[0]
        else:
            self.data = '' # HERE THE DEFAULT VALUE IS OVERWRITTEN

    def _value(self):
        return text_type(self.data) if self.data is not None else ''

which overwrites the default value.

@hector

This comment has been minimized.

Copy link
Author

hector commented Oct 23, 2015

Probably this is enough to solve the problem:

def process_formdata(self, valuelist):
        if valuelist:
            self.data = valuelist[0]
        elif self.data is None:
            self.data = ''

Anyway, why does the StringField set an empty string to data, couldn't it just use the parent's class Field.process_formdata method.

@hector

This comment has been minimized.

Copy link
Author

hector commented Nov 22, 2015

Anyone there?

@crast crast added the wontfix label Dec 15, 2015

@crast

This comment has been minimized.

Copy link
Contributor

crast commented Dec 15, 2015

This is intentional behaviour for wtforms 2.x

It may work differently or be configurable in wtforms 3.x... but this option comes at the cost of security and it will be really clear to the user when they make that choice in wtforms 3

See this FAQ entry: http://wtforms.readthedocs.org/en/latest/faq.html#why-does-blank-input-not-go-back-to-the-default-value

@crast crast closed this Dec 15, 2015

@hector

This comment has been minimized.

Copy link
Author

hector commented Dec 15, 2015

I see, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.