After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 50276 - Entry, combo, spin button validation
Entry, combo, spin button validation
Status: RESOLVED OBSOLETE
Product: gtk+
Classification: Platform
Component: Widget: Other
1.3.x
Other All
: Normal enhancement
: Medium API
Assigned To: gtk-bugs
gtk-bugs
: 164805 315988 446056 (view as bug list)
Depends on:
Blocks: 113561
 
 
Reported: 2001-01-01 22:07 UTC by Havoc Pennington
Modified: 2018-04-15 00:11 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
patch to add validation to widgets for gtk+-1.2.10 (7.18 KB, patch)
2002-01-01 17:28 UTC, bluefish
none Details | Review
Proposal and patch to implement validation for GtkEntry,GtkSpinbutton and GtkCombo widgets (78.94 KB, application/x-bzip2)
2003-01-07 08:36 UTC, sbaillie
  Details
More detailed notes on proposal above (attachment (id=13397)) (8.47 KB, text/plain)
2003-02-09 08:29 UTC, sbaillie
  Details

Description Havoc Pennington 2001-01-01 22:07:46 UTC
There should be some uniform, convenient way to do validation of input for
GtkEntry, combo box, and spin button. Right now you connect to insert_text
for entry, which is pretty easy, but could be nicer. For spin button,
there's some undocumented API involving "input" and "output" signals that
should be unified with GtkEntry (and documented). The task here is twofold:
figure out the programmer API and use cases, and figure out the
user-visible interface (what happens on invalid input - beeping?).
Comment 1 Havoc Pennington 2001-01-29 19:39:30 UTC
Put all GTK 1.3.x bugs on 2.0.0 milestone
Comment 2 Havoc Pennington 2001-09-17 04:07:57 UTC
There's some useful discussion of this topic here:

  http://mail.gnome.org/archives/gtk-list/2001-August/msg00224.html

We can conceivably make some progress in 2.2 without breaking binary
compatibility, it depends on what the overall solution looks like.
Comment 3 Havoc Pennington 2001-12-16 22:53:15 UTC
More discussion here:
  http://mail.gnome.org/archives/gtk-list/2001-December/msg00164.html
Comment 4 Matthias Clasen 2001-12-17 10:31:19 UTC
Here is what Java has for input validation:
http://java.sun.com/j2se/1.3/docs/guide/swing/InputChanges.html
Comment 5 Matthias Clasen 2001-12-17 10:44:56 UTC
And here is what Qt has:
http://doc.trolltech.com/3.0/qvalidator.html
Comment 6 bluefish 2002-01-01 17:28:23 UTC
Created attachment 6324 [details] [review]
patch to add validation to widgets for gtk+-1.2.10
Comment 7 Havoc Pennington 2002-03-13 15:25:56 UTC
Another thing we might consider is "templates"
e.g. you have "(    )    -       " in gray in the entry, 
and you can edit the phone number inside there.
Or for an IP address you have the four dots in gray.
Comment 8 Christian Reis (not reading bugmail) 2002-07-15 14:22:51 UTC
Sun's site contains:

"The following example has two text fields, with the first one
expecting the string "pass" to be entered by the user. If that string
is entered in the first text field, then the user can advance to the
second text field either by clicking in it or by pressing TAB.
However, if another string is entered in the first text field, then
the user will be unable to transfer focus to the second text field."

This is hardly good usability. We don't want the user to be notified
when he's finished typing in the code, but as soon as he does
something we don't want. Granted, password checks are special, but I
believe input validation covers syntax issues, not if the information
passed in. IOW, if you are typing in a product code, and it's a
7-digit numeric code, then entry of letters can be blocked (a label
indicating the error would be nice), but it isn't the validator's task
to check if the code actually exists in the system.

And blocking the transfer of focus is quite evil, IMHO.
Comment 9 Havoc Pennington 2003-01-04 07:05:57 UTC
another post
http://mail.gnome.org/archives/gtk-devel-list/2003-January/msg00019.html
Comment 10 sbaillie 2003-01-07 08:36:17 UTC
Created attachment 13397 [details]
Proposal and patch to implement validation for GtkEntry,GtkSpinbutton and GtkCombo widgets
Comment 11 sbaillie 2003-01-10 07:37:10 UTC
The attachment above (id=13397) is a compressed archive file. When you
click
on it, it will be saved as file called showattachment.cgi. 
It should be renamed to : ValidationProposal.tar.bz2
Comment 12 Matthias Clasen 2003-01-27 01:36:33 UTC
I like the ideas from QtValidator to a) allow to indicate prefixes of
valid values (this information would have to be used inside GtkEntry,
e.g. to prevent entering letters in a digit-only field) and to allow a
"fixup" (this could be used in an error dialog). Another needed
feature is the ability to delay checking of contraints until a group
of related inputs is committed (ie an ok button is pressed). Strict
validation on focus-out is only appropriate if everything is
instant-apply, otherwise it is just annoying to the user.


Comments on the patch:

1) I think it would be nicer to add a validate() virtual function to
GtkWidget instead of poking in the entry internals from gtkwidget.c.
The default implementation would simply return TRUE;

2) I believe you should drop entry_pending_validation and work with 
gtk_window_get_focus() instead.

3) Should use GDK_Tab/GDK_KP_Tab instead of a numerical keyval (the
code currently seems to miss Shift-Tab (for cycling backwards)
Comment 13 Matthias Clasen 2003-01-27 08:43:56 UTC
I guess we also need something like the ErrorProvider of WinForms
(a trivial little 
widget which will show an error icon with a tooltip describing the error, and hide the 
icon if there is no error)
Comment 14 sbaillie 2003-02-09 08:29:27 UTC
Created attachment 14215 [details]
More detailed notes on proposal above (attachment (id=13397))
Comment 15 Christian Reis (not reading bugmail) 2003-02-09 20:23:19 UTC
I would like to point out that tying validation exclusively to a focus
event is  incorrect. You are in many cases punishing the user by
allowing him to enter data which you *know* he shouldn't be (since you
have the contents of the widget in every time) typing in and then
saying "oh-oh, you shouldn't have done that".

I agree that for certain cases focus out is the correct event to wait
for, however, the vast majority of cases would have much improved
usability if we caught errors before the focus event - we *should*
worry about every keypress the user produces!

I would like to work on a proposal that involved three things:

- validation tied to insert/delete_text (changed), which would bar the
user from typing, for instance, alfabetical characters in a numeric
field. The error indication would be beeping or flashing a border on
the widget in question, taking into account localization issues.
- validation tied to focus_out, which allowed for post-entry
validation (this has to be done for certain types of data entry, but
it is actually a minority of the cases)
- entry masks. this is an *essential* property for any
commercial-grade toolkit, and every homebrew windows application
written in VB uses this. From personal experience this is something
users appreciate *very* much, and it is very hard to implement without
toolkit support (though it is possible, since I have a prototype
working in Kiwi).

(Unfortunately I am very busy with my MSc thesis until the 21st of
february and my MSc supervisor will kill me if he knows I'm commenting
on bugs instead of working on it, but this is a very important matter
and one I have some experience with)

While I don't strictly argue for implementing all this in GTK+, I do
believe it is a serious enough matter to warrant further
investigation. In other words, if we don't have it in the toolkit,
then a standard set of hooks should be provided, and a validator
framework worked on and documented/distributed accordingly.

If people diverge significantly I'll write up a justification for my
strong position, but I have worked a lot with this sort of issue on a
number of toolkits and I hope I can contribute to a better framework
for GTK+.
Comment 16 Christian Reis (not reading bugmail) 2003-02-09 20:25:09 UTC
Btw, when I say entry masks, I mean the same as Havoc calls 'templates'.
Comment 17 Matthias Clasen 2003-02-10 19:28:10 UTC
Christian, I mostly agree with you, but I think we should add
dialog-level validation to your list. When there are multiple related
controls on a non-immediate-apply dialog, it makes a lot of sense to
not block navigation, but simply mark invalid entries (e.g. like the
above-mentioned ErrorProvider) and disable the Ok button.
Comment 18 Havoc Pennington 2003-02-12 00:31:54 UTC
It seems that often providing text that explains the problem 
alongside the entry is a lot nicer than blocking focus transfer or
doing a dialog; then when the user tries to "submit the form" (presses
OK or whatever) you might pop up a dialog calling attention to the 
problematic entry. Or you might make the submit form button 
insensitive, though that kinda sucks until we have tooltips on 
insensitive buttons.

Windows XP also offers the option of a little tooltip/balloon pointing
out what's wrong with the entry, so you wouldn't have to keep 
space in the widget layout to display the error message.

But it should perhaps be somewhat up to the app what it wants to do
(block focus transfer or not, etc.)
Comment 19 Christian Reis (not reading bugmail) 2003-02-12 02:25:29 UTC
Both blocking focus and opening a dialog are very intrusive. The
`conventional' approach is:

- beep
- flash border around widget

They actually work well; the second being less intrusive but slightly
less effective at warning. The `soft' or `modern' way of doing it
would be to have a small floating label indicating precisely what was
wrong, perhaps one shaped as a baloon.

Disabling the OK button should be done if the form validation blocks
confirming the dialog, but we shouldn't force the user to move the
mouse to the button and hope for a tooltip to get the message; it
should be conveniently displayed to him in a decent position.

I'm not sure how GTK+ can aid implementing this beyond offering
complete validated widgets with very special API, or some additional
signals for people to contribute their own custom widgets.
Comment 20 Kristian Rietveld 2003-03-25 19:20:19 UTC
I agree with Christian's ideas, except for the flashing borders thing.
I think having a tooltip/balloon on error when the entry has focus
might be better and more clear.

The dialog-level validation also sounds nice, but it really requires
some thought on how we are going to implement that in a clean, sane
way for gtk+.

Anyway, I would be happy to volunteer to implement most of Christian's
ideas for 2.4. Especially the entry mask stuff for GtkEntry sounds
interesting to me (I dunno why actually ;).

Let me know what you think, and which direction we are going to take here.
Comment 21 Havoc Pennington 2003-03-31 19:01:33 UTC
So it seems the GtkEntry extensions should be:

 - some way to display an invalid state, balloons/beep/flashing 
   border/whatever
 - some convenient hooks to set_validate_function() so that 
   you can easily write a function that just returns a 
   user-readable explanation of the problem and perhaps an 
   offset into the entry text for where the balloon should point
 - a template feature so you can have a phone number or IP 
   address entry

Then perhaps on the GtkWindow level, a function like 
gtk_window_contains_invalid_data()? which would simplify 
checking all entries (and other widgets too, perhaps) 
when the user tries to close the dialog.
Comment 22 Luis Villa 2004-04-29 13:56:52 UTC
Comment on attachment 6324 [details] [review]
patch to add validation to widgets for gtk+-1.2.10

Obsoleted by newer patch, switch to gtk2.
Comment 23 Matthias Clasen 2005-01-21 21:52:52 UTC
*** Bug 164805 has been marked as a duplicate of this bug. ***
Comment 24 Owen Taylor 2005-09-11 16:39:04 UTC
*** Bug 315988 has been marked as a duplicate of this bug. ***
Comment 25 JoseAntonio S.A. 2005-11-22 18:01:50 UTC
Look at http://dev.investorsidekick.com/begtkextra/ (C) and
http://view.sourceforge.net/classes.php  (C++)  FieldEntry widget.
From the web page:
"The FieldEntry widget is a standard GtkEntry (...). Validation happens on every
key press and ensures that the content doesn't contain any characters that don't
belong. The entry is designed to not get in your way. Copying and pasting also
just work between entries and other programs on the system." 
Comment 26 Federico Mena Quintero 2005-11-25 19:47:52 UTC
Tutorial on Qt's input validation classes:
http://www.digitalfanatics.org/projects/qt_tutorial/chapter10.html
Comment 27 Markku Vire 2005-12-16 10:46:44 UTC
I propose that we would follow the Qt's idea about validator objects.
We could have an abstract class like:

struct _GtkValidatorClass
{
   GtkObjectClass parent_class;

   GValidatorState (*validate) (GValidator *self, const gchar *text, 
                                GError **error);
   gchar * (*fix)              (GValidator *self, const gchar *text);
};

Then the actual implementations, like

* GtkValidatorRequiredField
* GtkValidatorRegEx
* GtkValidatorRange
* GtkValidatorMask

So, the applications could write something like:

  validator = gtk_validator_range_new(1, 100);
  gtk_entry_set_validator(entry, validator); 

  /* Or should we have something like 
     gtk_validatable_set_validator(GTK_VALIDATABLE(entry), validator); */

This approach is heavier than some simple callback function based approach, but
I think it's much cleaner, specially in case of validators that need
private data as well.



When an error is encountered, it needs to be displayed to the user. 
I propose generic interface for this purpose. For example:

struct _GtkErrorProviderIface
{
   GTypeInterface parent;
};

void gtk_error_provider_set_text(GtkErrorProvider *self, const gchar *text);
char *gtk_error_provider_get_text(GtkErrorProvider *self);

void gtk_error_provider_set_icon_name(GtkErrorProvider *self, 
   const gchar *icon_name);
char *gtk_error_provider_get_icon_name(GtkErrorProvider *self);

Interface would require properties "text" and "icon-name". 
The widget implementig this interface could be GtkErrorProviderWidget
that would be visually similar to .Net ErrorProvider component. 
So, an indicator icon is displayed and tooltip is given when mouse 
howers on top of that.

GtkEntry and other widgets could then do something like :

  state = gtk_validator_validate(entry->validator, text, &error);
  
  if (error) {
    gtk_error_provider_set_text(entry->error_provider, error->message);
    g_error_free(error);
  } else
    gtk_error_provider_set_text(entry->error_provider, NULL);

in "changed" handler (or whatever).


I think it would be important to have this GtkErrorProvider to be an 
interface instead of single widget, since different applications 
can have very different needs of how to report errors. 
I can imagine that somebody could embed the error reporting 
feature into some existing component.
Comment 28 Johan (not receiving bugmail) Dahlin 2011-02-23 01:30:13 UTC
*** Bug 446056 has been marked as a duplicate of this bug. ***
Comment 29 Matthias Clasen 2018-02-10 05:00:21 UTC
We're moving to gitlab! As part of this move, we are moving bugs to NEEDINFO if they haven't seen activity in more than a year. If this issue is still important to you and still relevant with GTK+ 3.22 or master, please reopen it and we will migrate it to gitlab.
Comment 30 Matthias Clasen 2018-04-15 00:11:42 UTC
As announced a while ago, we are migrating to gitlab, and bugs that haven't seen activity in the last year or so will be not be migrated, but closed out in bugzilla.

If this bug is still relevant to you, you can open a new issue describing the symptoms and how to reproduce it with gtk 3.22.x or master in gitlab:

https://gitlab.gnome.org/GNOME/gtk/issues/new