wxValidator : Display error message when validation fails

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

wxValidator : Display error message when validation fails

Post by deepti »

Hi,

I am creating an instance of wxTextCtrl. However, depending on the type (text, integer or decimal), the wxTextCtrl instance should display a message when user enters a wrong value. For example, if the type is text, and the user enters a numeric value, a message should be displayed saying the value is invalid.
For this, I am associating a wxValidator. Please see the code below:

if (attrParams.type == "1")
{
wxTextValidator val(wxFILTER_ALPHA, &(textCtrl->m_strValue));
textCtrl->SetValidator(val);
}
else if (attrParams.type == "2")
{
wxIntegerValidator<int> val(&(textCtrl->m_intValue));
textCtrl->SetValidator(val);
}
else if (attrParams.type == "3")
{
wxFloatingPointValidator<float> val(&(textCtrl->m_decimalValue));
textCtrl->SetValidator(val);
}

The Validator is working fine, as in it doesnt allow the user to enter invalid values. But if I add the code below(in order to display a message to user) after each "SetValidator" call, it crashes in case of wxTextValidator. It does not crash when used with wxIntegerValidator and wxFloatingPointValidator, but the message box is not getting displayed either.

if (val.Validate(textCtrl->GetParent()) == false) { wxMessageBox("invalid value"); }

Please see the callstack below:
validatorissue.jpg
validatorissue.jpg (32.17 KiB) Viewed 1205 times
Please help!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxValidator : Display error message when validation fails

Post by ONEEYEMAN »

Hi,
First of all - please use the <code></code> tags so that the code you post will be readable.
Second - thank you for the backtrace, but it is not useful - we have absolutely no idea what those functions are and how the functions are communi=cating with each other.
Third - you should clarify whether you are getting the crash or the program just asserts.
And last but not least, usual stanza - wx version, OS/toolkit version.

Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxValidator : Display error message when validation fails

Post by PB »

deepti wrote:

Code: Select all

if (val.Validate(textCtrl->GetParent()) == false) { wxMessageBox("invalid value"); }
This looks a bit suspicious, where did you get your val from? I do not think that you are supposed to validate like this at all. You set a validator to the control and the control does the validation as needed.

If you want to change how it validation works you should override the validator methods of the control or when enough just call

Code: Select all

if ( !textCtrl->Validate() ) ... 
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: wxValidator : Display error message when validation fails

Post by deepti »

Hi PB,

I found that from the documentation for wxValidator in this site :

http://docs.wxwidgets.org/trunk/classwx ... 436e5c71af

Using the if (!textCtrl->Validate() ) is not helping either. I need a message box to be displayed when the datatype of the value entered is incorrect.
Right now, it just does not let the user enter incorrect values. But that is confusing since the user might not know why numbers are not getting entered while text is!
Is there a way to get this done? Please help
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxValidator : Display error message when validation fails

Post by PB »

deepti wrote:Hi PB,

I found that from the documentation for wxValidator in this site :

http://docs.wxwidgets.org/trunk/classwx ... 436e5c71af
I do not see any such code there.
deepti wrote: Using the if (!textCtrl->Validate() ) is not helping either. I need a message box to be displayed when the datatype of the value entered is incorrect.
Right now, it just does not let the user enter incorrect values. But that is confusing since the user might not know why numbers are not getting entered while text is!
Is there a way to get this done? Please help
I do not think it is a good idea and AFAIK this not how it is usually done. It would be very frustrating for the user to be slapped by a message box every time he tries to type an "invalid" character. This is usually handled by a system beep, which can unfortunately not be heard by the user. In any way, as I told you before, you probably need to use a custom validator derived from a wxWidgets one, with its Validate() overriden.
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: wxValidator : Display error message when validation fails

Post by deepti »

Hi PB,

There is this function at the end of the page in the link I sent:

Code: Select all


virtual bool wxValidator::Validate(wxWindow * 	parent)	
virtual
This overridable function is called when the value in the associated window must be validated.

Parameters
parent	The parent of the window associated with the validator.
Returns
false if the value in the window is not valid; you may pop up an error dialog.
Reimplemented in wxTextValidator, and wxNumericPropertyValidator.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxValidator : Display error message when validation fails

Post by PB »

deepti wrote:Hi PB, There is this function at the end of the page in the link I sent:
The code there has nothing in common with that code of yours I quoted? I.e., it does not have a local instance of wxValidator calling validate with an unassociated control.
Post Reply