Opened 17 years ago

Closed 16 years ago

Last modified 16 years ago

#5136 closed (wontfix)

Give BoundField an is_required property

Reported by: daybreaker12@… Owned by: Adrian Holovaty
Component: Forms Version: dev
Severity: Keywords: field required template evaluation
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm using newforms form object as following:

from django import newforms as forms

class ModifyForm(forms.Form):
    email = forms.EmailField(required=True, label=u'E-Mail')
    homepage = forms.URLField(required=False, label=u'홈페이지')
    signature = forms.CharField(widget=forms.Textarea, required=False, label=u'서명', help_text=u'자신이 올린 자료나 글의 끝에 따라붙는 텍스트입니다.')
            <form id="form-ModifyUser" method="post" action="/user/modify/">
            <table>
                <tr><th>OpenID:</th><td>{{userprof.openid}}</td></tr>
                {% for field in modify_form %}
                <tr>
                    <th>{{field.label_tag}}{% if field.required %}<span class="required">*</span>{% endif %}:</th>
                    <td>
                        {% if field.errors %}{{field.errors}}{% endif %}
                        {{field}}
                        {% if field.help_text %}<br/><span class="help">{{field.help_text}}</span>{% endif %}
                    </td>
                </tr>
                {% endfor %}
            </table>
            <p><input type="submit" value="수정하기" /></p>
            </form>

But field.required returns always None, not True or False, so I cannot display a required asterisk mark for required fields.
Other options such as help_text works fine.

Change History (7)

comment:1 by Malcolm Tredinnick, 17 years ago

Resolution: invalid
Status: newclosed

The "field" in your template is a newforms.forms.BoundField instance. So you want field.field.required.

In future, please ask support questions on the django-users mailing list. This Trac is for bug reports only.

comment:2 by daybreaker12@…, 17 years ago

Thanks, I didn't know that this is actually a bug.

comment:3 by Knox Harrington, 16 years ago

This doesn't seem very intuitive, would it not be preferable to be able to acess field.required in a template ?

Not realizing that you could access this with field.field.required, I wrote a patch to access thin in a template :

--- /usr/lib/python2.4/site-packages/django/newforms/forms.old.py
+++ /usr/lib/python2.4/site-packages/django/newforms/forms.py
@@ -307,6 +307,11 @@

"Returns True if this BoundField's widget is hidden."
return self.field.widget.is_hidden

is_hidden = property(_is_hidden)

+
+ def _is_required(self):
+ "Returns True if this Field is required."
+ return self.field.required
+ is_required = property(_is_required)

def _auto_id(self):

"""

comment:4 by anonymous, 16 years ago

Resolution: invalid
Status: closedreopened

comment:5 by Chris Beaven, 16 years ago

Summary: newforms field required attribute is not evaluated in templatesGive BoundField an is_required property
Triage Stage: UnreviewedDesign decision needed

Agreed Knox, it isn't very intuitive (and has come up before in several previous discussions). I'll promote to design decision - care to attach your patch as a file?

comment:6 by Malcolm Tredinnick, 16 years ago

Resolution: wontfix
Status: reopenedclosed

You can already access it one way. That is enough. A documentation patch to indicate that the underlying field object is available via field.field will be accepted (so as not to just special case this instance) if it's opened as an appropriate ticket.

comment:7 by knox <christobzr@…>, 16 years ago

Ticket #5854 now has a proposed patch for this.

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