Code

Opened 9 years ago

Closed 9 years ago

#1081 closed enhancement (wontfix)

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

Reported by: Yango Owned by: adrian
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: UI/UX:

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)

Attachments (0)

Change History (1)

comment:1 Changed 9 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.