Code

Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#5136 closed (wontfix)

Give BoundField an is_required property

Reported by: daybreaker12@… Owned by: adrian
Component: Forms Version: master
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: UI/UX:

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.

Attachments (0)

Change History (7)

comment:1 Changed 7 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 Changed 7 years ago by daybreaker12@…

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

comment:3 Changed 6 years ago by Knox Harrington

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 Changed 6 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to reopened

comment:5 Changed 6 years ago by SmileyChris

  • Summary changed from newforms field required attribute is not evaluated in templates to Give BoundField an is_required property
  • Triage Stage changed from Unreviewed to Design 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 Changed 6 years ago by mtredinnick

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

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 Changed 6 years ago by knox <christobzr@…>

Ticket #5854 now has a proposed patch for this.

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.