Is there any attribute that can be used to differentiate a required "widget"
of an optional widget?
TIA,
--
Jorge Godoy <jgo...@gmail.com>
You either don't pass a validator or do
validator=V.Any(V.Int,V.Empty). I import validators as V.
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>
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.
> 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>
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
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>
Eh, I need it for a project. I'll do it right now unless you're mostly done.
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>
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.
Worked for me. ;-) I just had to make it a list, otherwise it splits the
string.
--
Jorge Godoy <jgo...@gmail.com>
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
> 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>
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.
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'.
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