Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding a class to required fields #50

Closed
wants to merge 4 commits into from

3 participants

@pwfff

I just had to do this for a project I'm working on, and I couldn't find an easy way to do it outside of these template files.

There's no existing framework in twitter-bootstrap for marking required fields so this may be outside of your project's scope, but I can't imagine it hurting anything.

Ideally, there'd be an easy way to toggle this, but I don't know your project well enough to make that kind of modification.

@jterrace

I think it would be nicer if it added something like class="required" instead of a style attribute. That would allow people to define whatever style they wanted for required fields.

@pwfff

This should be better. I moved it to the control-group (since the label isn't really what's 'required'), and changed the class from a generic 'required' to control-group-required, to match the other twitter-bootstrap class names.

In my project I'll do something like:

.control-group-required label { font-weight: bold; }
.control-group-required > input { background-color: whiteSmoke; }
...trap/templates/bootstrap/field_appendprependtext.html
@@ -1,5 +1,5 @@
-<div class="control-group{% if errors %} error{% endif %}">
- <label class="control-label" for="{{ bf_raw.id_for_label }}">{{ label }}</label>
+<div class="control-group{% if errors %} error{% endif %}{% if field.required %} control-group-required{% endif %}">
+ <label class="control-label" for="{{ bf_raw.id_for_label }}">{{ label }}</label>

I don't think you meant to change the indentation of the label tag here. There's one of these below too, in prependedtext.html

@pwfff
pwfff added a note

Good catch... know of a vim plugin for detecting tab widths? :)

Not sure. I use eclipse, so I just set project-specific settings based what's used in that project. HTML is typically indented by two spaces though because it tends to get nested quickly, so you might just want to set that as your default for HTML if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jterrace

lgtm now +1

@pthrasher
Collaborator

Please see twbs/bootstrap#107

Best to mark fields as optional, and you should do this from the label text according to @mdo.

@pwfff

Did you mean twbs/bootstrap#207?

That doesn't really work for my project, which has many more optional fields than required ones (whether that's bad form design or not is beside the point). Plus, since it's an already existing (and quite large) Django codebase, I'd have to do something like extend the BootstrapModelForm constructor to manually append '(required)' to all of the help text, and that seems less than ideal...

Django already has the 'required' metadata stored. What's the harm in exposing it here? People don't have to take advantage if they don't want to.

@pwfff

Actually, it looks like this is already being included, but not used in the template. What's the meaning of this line: https://github.com/earle/django-bootstrap/blob/master/bootstrap/forms.py#L111

@pthrasher
Collaborator

It appears that classes are built up, and are available within the template context. You could make a change to the templates to put the classes where you'd like them.

@pthrasher
Collaborator

Please pull latest, merge in, and fix conflicts. I'll merge this in.

@pthrasher
Collaborator

bump

@pthrasher
Collaborator

I'd like to merge this in, but with no activity or responses, I'm closing it. Please request a new pull request if you'd like this merged in, and be sure to fetch latest, and resolve any potential conflicts.

@pthrasher pthrasher closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  bootstrap/templates/bootstrap/field_appendedtext.html
@@ -1,4 +1,4 @@
-<div class="control-group{% if errors %} error{% endif %}">
+<div class="control-group{% if errors %} error{% endif %}{% if field.required %} control-group-required{% endif %}">
<label class="control-label" for="{{ label_id }}">{{ label }}</label>
<div class="controls">
<div class="input-append">
View
2  bootstrap/templates/bootstrap/field_appendprependtext.html
@@ -1,4 +1,4 @@
-<div class="control-group{% if errors %} error{% endif %}">
+<div class="control-group{% if errors %} error{% endif %}{% if field.required %} control-group-required{% endif %}">
<label class="control-label" for="{{ bf_raw.id_for_label }}">{{ label }}</label>
<div class="controls">
<div class="input-append input-prepend">
View
2  bootstrap/templates/bootstrap/field_default.html
@@ -1,4 +1,4 @@
-<div class="control-group{% if errors %} error{% endif %}">
+<div class="control-group{% if errors %} error{% endif %}{% if field.required %} control-group-required{% endif %}">
<label class="control-label" for="{{ label_id }}">{{ label }}</label>
<div class="controls">
{{ bf }}
View
2  bootstrap/templates/bootstrap/field_prependedtext.html
@@ -1,4 +1,4 @@
-<div class="control-group{% if errors %} error{% endif %}">
+<div class="control-group{% if errors %} error{% endif %}{% if field.required %} control-group-required{% endif %}">
<label class="control-label" for="{{ bf_raw.id_for_label }}">{{ label }}</label>
<div class="controls">
<div class="input-prepend">
Something went wrong with that request. Please try again.