Validating multiple fields - combined, not one at a time

Hi
I have to say WX Python is going well for me. It's surprising how
much of a palaver it is to write GUIs, (not having done it much). But
at least I have a tool that seems to work nicely.

Anyway my question is "Can I validate multiple fields combined?"

What I want is something like this:
- Put up modal dialog
- User enters new customer name, new customer employer, new customer
date of birth (for example) - for the sake of the argument assume
these three fields make a key in a database table
- User clicks 'OK - create new customer'.
- Validator gets all three fields (name, employer, DOB) and checks
that person does not exist in the database
- Puts up error message or continues with the creation.

Before I wade in and create something for this I thought I would check
to see if there is a nifty way of doing it with validators.

Any help much appreciated.

Kind regards

Phil

Hi Phil,

Hi
I have to say WX Python is going well for me. It's surprising how
much of a palaver it is to write GUIs, (not having done it much). But
at least I have a tool that seems to work nicely.

Anyway my question is "Can I validate multiple fields combined?"

What I want is something like this:
- Put up modal dialog
- User enters new customer name, new customer employer, new customer
date of birth (for example) - for the sake of the argument assume
these three fields make a key in a database table
- User clicks 'OK - create new customer'.
- Validator gets all three fields (name, employer, DOB) and checks
that person does not exist in the database
- Puts up error message or continues with the creation.

Before I wade in and create something for this I thought I would check
to see if there is a nifty way of doing it with validators.

Any help much appreciated.

Kind regards

Phil

If you have the database set up so that you have a composite key of
the three fields (name, employer, DOB), then when you try to create a
new record with the same key, it should raise an error. You can catch
the error and have wx display a dialog.

Each Python database module is different, so I can't really show you
an example. Plus, I use SqlAlchemy in my programs...but the idea is
the same regardless of the implementation.

···

On Jan 5, 12:14 pm, Philip Ellis <philipjel...@gmail.com> wrote:

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21 http://us.pycon.org/

Philip Ellis wrote:

Hi
I have to say WX Python is going well for me. It's surprising how
much of a palaver it is to write GUIs, (not having done it much). But
at least I have a tool that seems to work nicely.

Anyway my question is "Can I validate multiple fields combined?"

What I want is something like this:
- Put up modal dialog
- User enters new customer name, new customer employer, new customer
date of birth (for example) - for the sake of the argument assume
these three fields make a key in a database table
- User clicks 'OK - create new customer'.
- Validator gets all three fields (name, employer, DOB) and checks
that person does not exist in the database
- Puts up error message or continues with the creation.

Before I wade in and create something for this I thought I would check
to see if there is a nifty way of doing it with validators.
  

As the user clicks a button it is probably just as easy to the use the button event to do the validation, i.e. do a select on the three entries and show any matching database rows and allow the user to select one or create a new one or if there is no match create a new one.

Generally it is not recommended to use fields like the above as keys for your table(s), it is better practice to use an "id" field which is auto incremented.

See for example or do a "sql database design" google search.
http://www.sqlteam.com/article/database-design-and-modeling-fundamentals

If you are starting this project then you should also look at SQLAlchemy, its ORM (object relational mapper) is very very nice and it does a lot of work for you.

Werner

As Werner pointed out, you can just use the Save button's event
handler to grab the three widget's contents and make sure they're not
empty. That is what I would do. If you want to be draconian, you can
bind each text field to a leave focus event that checks if the field
is empty. Then you could show a dialog if it is empty and veto the
change of focus (which basically forces the user to enter something).

···

On Tue, Jan 5, 2010 at 12:48 PM, Philip Ellis <philipjellis@gmail.com> wrote:

Mike
Sorry, I was not being clear.

I am quite ok checking my database. What I want is to run the check
from a validator that can see all three fields entered. All the
validators I have seen can only work on one field at a time (eg a
wx.TextCtrl- check something is in the field, if it is not then send a
message). I want to have a validator check three fields at once. So
I want to do name.GetValue() and employer.GetValue() and DOB.GetValue
(), then I can write some SQL to check the key is not in my database.

I can think of ways of doing something like this outside of a
validator. But I figured the validator looked like a better tool for
the job IF it could work on all three fields at once.

rgds
phil

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21 http://us.pycon.org/

Mike Driscoll wrote:

If you want to be draconian, you can
bind each text field to a leave focus event that checks if the field
is empty. Then you could show a dialog if it is empty and veto the
change of focus (which basically forces the user to enter something).

Please don't do this. It is indeed Draconian, and would provide a very painful user interface -- users SHOULD be allowed to type invalid data, even if forced to go back an fix it before it actually gets stored. That way you can have typos, and fill in an correct fields in the order you want. It really makes for a much nicer user experience.

If you want, you can flag (by making it red or something) any field that isn't valid, but don't force the user to correct errors in any particular order.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

That was (mostly) a joke. I don't recommend this either. I think the
last time I had to "validate" something like this, I ended up using a
set of conditionals that would throw up a generic message dialog
stating that so-and-so needed to be filled out.

The validation part didn't really come in until I sent the info on and
got a bad response.

···

On Jan 6, 10:31 am, Christopher Barker <Chris.Bar...@noaa.gov> wrote:

Mike Driscoll wrote:
> If you want to be draconian, you can
> bind each text field to a leave focus event that checks if the field
> is empty. Then you could show a dialog if it is empty and veto the
> change of focus (which basically forces the user to enter something).

Please don't do this. It is indeed Draconian, and would provide a very
painful user interface -- users SHOULD be allowed to type invalid data,
even if forced to go back an fix it before it actually gets stored. That
way you can have typos, and fill in an correct fields in the order you
want. It really makes for a much nicer user experience.

If you want, you can flag (by making it red or something) any field that
isn't valid, but don't force the user to correct errors in any
particular order.

-Chris

--
Christopher Barker, Ph.D.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21 http://us.pycon.org/

Probably the easiest way to do it is to bind an event handler for the OK button (instead of letting the default one get the event.) In the handler check your values and if everything is okay then call EndModal(wx.ID_OK). Otherwise inform the user of the problem in some way and return without calling EndModal.

···

On 1/5/10 10:14 AM, Philip Ellis wrote:

Hi
I have to say WX Python is going well for me. It's surprising how
much of a palaver it is to write GUIs, (not having done it much). But
at least I have a tool that seems to work nicely.

Anyway my question is "Can I validate multiple fields combined?"

What I want is something like this:
- Put up modal dialog
- User enters new customer name, new customer employer, new customer
date of birth (for example) - for the sake of the argument assume
these three fields make a key in a database table
- User clicks 'OK - create new customer'.
- Validator gets all three fields (name, employer, DOB) and checks
that person does not exist in the database
- Puts up error message or continues with the creation.

Before I wade in and create something for this I thought I would check
to see if there is a nifty way of doing it with validators.

--
Robin Dunn
Software Craftsman