Opened 18 years ago

Closed 18 years ago

#1081 closed enhancement (wontfix)

SelectFields should have the option to render them with a default value

Reported by: Yango Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I need to render SelectFields with a default value the first time they are rendered (that is, when the FormWrapper sends the render call with no value).

The following changes do exactly that...

I changed the SelectField's init definition to look like this:

    def __init__(self, field_name, choices=[], size=1, is_required=False, validator_list=[], member_name=None, default_value=''):
        self.field_name = field_name
        self.default_value = str(default_value)
        # choices is a list of (value, human-readable key) tuples because order matters
        self.choices, self.size, self.is_required = choices, size, is_required
        self.validator_list = [self.isValidChoice] + validator_list
        if member_name != None:
            self.member_name = member_name

and the render method like this:

    def render(self, data):
        output = ['<select id="%s" class="v%s%s" name="%s" size="%s">' % \
            (self.get_id(), self.__class__.__name__,
             self.is_required and ' required' or '', self.field_name, self.size)]
        str_data = str(data) # normalize to string
        for value, display_name in self.choices:
            selected_html = ''
            if str(value) == str_data:
                selected_html = ' selected="selected"'
            if str_data == '' and str(value) == self.default_value:
                selected_html = ' selected="selected"'
            output.append('    <option value="%s"%s>%s</option>' % (escape(value), selected_html, display_name))
        output.append('  </select>')
        return '\n'.join(output)

Change History (1)

comment:1 by Adrian Holovaty, 18 years ago

Resolution: wontfix
Status: newclosed

This is not in the scope of the formfields.py framework. Formfields just know how to display themselves -- they don't know (or care) about data. Put this in your business logic.

Note: See TracTickets for help on using tickets.
Back to Top