Widgets: how to sinalize to the user that an input is required?

9 views
Skip to first unread message

Jorge Godoy

unread,
Jan 17, 2006, 6:33:24 PM1/17/06
to TurboGears

Hi!


Is there any attribute that can be used to differentiate a required "widget"
of an optional widget?


TIA,
--
Jorge Godoy <jgo...@gmail.com>

Karl Guertin

unread,
Jan 17, 2006, 8:04:17 PM1/17/06
to turbo...@googlegroups.com
On 1/17/06, Jorge Godoy <jgo...@gmail.com> wrote:
> Is there any attribute that can be used to differentiate a required "widget"
> of an optional widget?

You either don't pass a validator or do
validator=V.Any(V.Int,V.Empty). I import validators as V.

Jorge Godoy

unread,
Jan 17, 2006, 8:09:44 PM1/17/06
to turbo...@googlegroups.com
Karl Guertin <gray...@gmail.com> writes:

And how can I apply some CSS so that the user knows he has to fill in these
fields before seeing an error? I have "validator = V.String(if_empty = None)"
that gives me the same result, but without the visual feedback when the form
is shown (imgine 'red dots' on required fields, for example).

--
Jorge Godoy <jgo...@gmail.com>

Karl Guertin

unread,
Jan 17, 2006, 8:29:22 PM1/17/06
to turbo...@googlegroups.com
On 1/17/06, Jorge Godoy <jgo...@gmail.com> wrote:
> And how can I apply some CSS so that the user knows he has to fill in these
> fields before seeing an error? I have "validator = V.String(if_empty = None)"
> that gives me the same result, but without the visual feedback when the form
> is shown (imgine 'red dots' on required fields, for example).

And that is the mystery. I have been skating around this issue. There
should really be a 'required' attribute on the base widget. If true it
would add 'required_field' to the class so you can css it and would
set the validator to V.NotEmpty by default.

If a validator is passed in, that overrides V.NotEmpty. This means
that you can do something like V.String(if_empty = None) and secretly
make a required field not required, but I think this would work for
most situations.

Jorge Godoy

unread,
Jan 17, 2006, 8:35:12 PM1/17/06
to turbo...@googlegroups.com
Karl Guertin <gray...@gmail.com> writes:

> And that is the mystery. I have been skating around this issue. There
> should really be a 'required' attribute on the base widget. If true it
> would add 'required_field' to the class so you can css it and would
> set the validator to V.NotEmpty by default.

Or some way that allows one to change the class. It would be even better this
way since you can specify classes for other circunstances besides required
fields (or pass differente 'required_classes' for each widget...).

> If a validator is passed in, that overrides V.NotEmpty. This means
> that you can do something like V.String(if_empty = None) and secretly
> make a required field not required, but I think this would work for
> most situations.

I prefer not touching validators. Just the class would solve the problem and
allow for more customization.

--
Jorge Godoy <jgo...@gmail.com>

rick

unread,
Jan 17, 2006, 9:06:19 PM1/17/06
to TurboGears
Yes. Widget should definetely take a class parameter.
At this point you will have to make your own widgets inherited from
Widget to get them to behave accordingly.

In the mean time.... the name is unique and it is passed into the ID
param of each field.

so in css you can set input#first_name { background.color: red; }

or something like that.

Good luck,

Rick

Jorge Godoy

unread,
Jan 17, 2006, 9:31:27 PM1/17/06
to turbo...@googlegroups.com
"rick" <rick.ri...@gmail.com> writes:

It is not a viable solution when you have several forms with lots of widgets
each.

I'm trying to create a patch to implement the 'class' attribute for all
widgets on 'base.py'.

--
Jorge Godoy <jgo...@gmail.com>

Karl Guertin

unread,
Jan 17, 2006, 9:40:36 PM1/17/06
to turbo...@googlegroups.com
On 1/17/06, Jorge Godoy <jgo...@gmail.com> wrote:
> I'm trying to create a patch to implement the 'class' attribute for all
> widgets on 'base.py'.

Eh, I need it for a project. I'll do it right now unless you're mostly done.

Jorge Godoy

unread,
Jan 17, 2006, 9:43:40 PM1/17/06
to turbo...@googlegroups.com
Karl Guertin <gray...@gmail.com> writes:

I'm knocking my head with it... I'll send you what I've done... It's a
little late here and I am tired, so I believe that you can help me ;-)


--
Jorge Godoy <jgo...@gmail.com>

Karl Guertin

unread,
Jan 17, 2006, 10:13:40 PM1/17/06
to turbo...@googlegroups.com
On 1/17/06, Jorge Godoy <jgo...@gmail.com> wrote:
> > Eh, I need it for a project. I'll do it right now unless you're mostly done.

This is in r530. I used css_classes because although it isn't quite as
accurate, there's no chance of someone thinking it's some sort of
multiple inheritance tracking or something.

Jorge Godoy

unread,
Jan 17, 2006, 10:24:19 PM1/17/06
to turbo...@googlegroups.com
Karl Guertin <gray...@gmail.com> writes:

Worked for me. ;-) I just had to make it a list, otherwise it splits the
string.

--
Jorge Godoy <jgo...@gmail.com>

Michele Cella

unread,
Jan 18, 2006, 5:15:41 AM1/18/06
to TurboGears

While working on ticket #125 I touched also the required field problem,
I think all these things should be managed by the form (label display,
required display, error display) not by the field itself.

As I said in another discussion, the css_classes attribute you added
makes sense only for simple widget (in fact you added it to only a tag
of the form or the grid, and the others?) moreover it works just like
the attrs widget (ok, with the latter you have to provide the original
class but it works in the same manner, it's there for this reason).

The right solution IMHO is relying on the not_empty attribute you pass
to the widget validator, when a form insert a field it should check if
the widget validator has not_empty set and in this case add a
"required_field" class name to the field cell.

>>> test = validators.Int(not_empty = True)
>>> test.not_empty
True
>>> test = validators.Int()
>>> test.not_empty
False
>>>

Then for example in the TableForm:

<td py:if="widget.validator.not_empty"
class="required_field">${widget.insert(getattr(self.widget_value,
widget.name, None), input_values, widget_error.get(widget.name,
None))}</td>
<td py:if="not
widget.validator.not_empty">${widget.insert(getattr(self.widget_value,
widget.name, None), input_values, widget_error.get(widget.name,
None))}</td>

(Note: Kid needs if/else :-()

In this way you just specify in the validator that you want this field
to be not empty and it's displayed in the right way by the form, with
the css_classes solution you need to also specify that special class.

I would expect it to work only by defining not_empty on my validator,
it's DRY.

What do you think?

Ciao
Michele

Jorge Godoy

unread,
Jan 18, 2006, 5:50:47 AM1/18/06
to turbo...@googlegroups.com
"Michele Cella" <michel...@gmail.com> writes:

> I would expect it to work only by defining not_empty on my validator,
> it's DRY.
>
> What do you think?

I said what I think before: this will make you limited to only two cases:
normal and required classes. What about something else, like a discount
field, or a confirmation field? What about an "attention" message where you
want to make it stand more, but not in, e.g., red as for required stuff?

--
Jorge Godoy <jgo...@gmail.com>

Michele Cella

unread,
Jan 18, 2006, 7:30:04 AM1/18/06
to TurboGears
Jorge Godoy wrote:
>
> I said what I think before: this will make you limited to only two cases:
> normal and required classes. What about something else, like a discount
> field, or a confirmation field? What about an "attention" message where you
> want to make it stand more, but not in, e.g., red as for required stuff?
>

That's a special case and since you can need very different things I
don't think a css_classes attribute is really needed, you can already
pass classes to the widget constructor by using the attrs dictionary,
it's there for this reason.

My svn checkout it's broken ATM, anyway I think you can do something
like this:

confirmation = YourField(..., attrs={'class': 'confirmation_field
attention'})

If you need to keep the existing class (as you can do using
css_classes) just add it to the class key.

Ciao
Michele

PS
I'm on dialup and ATM I can't fix my broken (dunno why) svn checkout so
I may be wrong since I can't test things, in that case excuse me.

Karl Guertin

unread,
Jan 18, 2006, 10:51:22 AM1/18/06
to turbo...@googlegroups.com
On 1/18/06, Michele Cella <michel...@gmail.com> wrote:
> As I said in another discussion, the css_classes attribute you added
> makes sense only for simple widget (in fact you added it to only a tag
> of the form or the grid, and the others?) moreover it works just like
> the attrs widget (ok, with the latter you have to provide the original
> class but it works in the same manner, it's there for this reason).

I added the tag to the outermost class on the form. Classes inside
that element can either be accessed via css selectors or, in the case
of forms/grids, putting a css_classes on the elements you're passing
in.

As to the attrs widget, I had tried it and it didn't seem to be
working. It works just fine if there isn't an already existing attr
with the same attribute name.


> The right solution IMHO is relying on the not_empty attribute you pass
> to the widget validator, when a form insert a field it should check if
> the widget validator has not_empty set and in this case add a
> "required_field" class name to the field cell.

AFAIK (and I've looked through most of the formencode codebase) there
is no way to definitively tell from a validator whether a field is
required or not without actually trying the to_python method with an
empty string. The not_empty attribute doesn't cover compound or custom
widgets. For example, I can use:

validator = V.Any(V.Int,V.Empty)

To make the validator field optional and not_empty won't appear on any
of the validators.


> I would expect it to work only by defining not_empty on my validator,
> it's DRY.

I'm not so much a fan of DRY. It seems DRY tends to be synonymous with
'magic happens'.

Michele Cella

unread,
Jan 18, 2006, 1:55:56 PM1/18/06
to TurboGears
Karl Guertin wrote:
>
> AFAIK (and I've looked through most of the formencode codebase) there
> is no way to definitively tell from a validator whether a field is
> required or not without actually trying the to_python method with an
> empty string. The not_empty attribute doesn't cover compound or custom
> widgets. For example, I can use:
>
> validator = V.Any(V.Int,V.Empty)
>
> To make the validator field optional and not_empty won't appear on any
> of the validators.
>

Then this is a formencode bug, just like this:

>>> test = v.NotEmpty()
>>> test.not_empty
False <--- O_o

>
> > I would expect it to work only by defining not_empty on my validator,
> > it's DRY.
>
> I'm not so much a fan of DRY. It seems DRY tends to be synonymous with
> 'magic happens'.

If my field is required (and you will always need to specify it in your
validator) the form should automatically use the field_required css
class, where is the magic?
I would expect it to be so, magic is something unexpected, but that's
not the case.

If this is magic then I should consider magic even my form displaying
validation errors. :-)

Ciao
Michele

Reply all
Reply to author
Forward
0 new messages