[v3] Validator called 3 times before any actual input

Added by Vincenzo Romano 5 months ago

The attached small program shows the "problem".
I don't really understand why the validator shouuld be called 3 times before any input is actually done.
Is this a bug?
If not, is there any explanation?

wttest.cpp Magnifier - test program (1.31 KB)


Replies (10)

RE: [v3] Validator called 3 times before any actual input - Added by Vincenzo Romano 4 months ago

None knows?
What'd be the input validation mechanism like?

RE: [v3] Validator called 3 times before any actual input - Added by Vincenzo Romano 4 months ago

This seems to be the code where the validator, if any, is called (WFormWidget.C) :

void WFormWidget::render(WFlags<RenderFlag> flags)
{
  if ((flags & RenderFull)) {
    if (flags_.test(BIT_JS_OBJECT))
      defineJavaScript(true);

    if (validator()) {
      WValidator::Result result = validator()->validate(valueText());
      WApplication::instance()->theme()
        ->applyValidationStyle(this, result, ValidationInvalidStyle);
    }
  }

  WInteractWidget::render(flags);
}

The other place is the ::validate() method itself.
I don't understand why, upon rendering of a widget, it's validator is called more than once.
Any idea? Or should I study all the source code to understand?

RE: [v3] Validator called 3 times before any actual input - Added by Wim Dumon 4 months ago

Hey Vincenzo,

Validators can indeed be evaluated at different parts in the code, and I haven't considered that this may be a problem, since the execution of the validation method should not be stateful or have side-effects. The example you highlight is to set a style class on form widgets depending on the result of validation, so that the visual feedback a user gets can be set by CSS without programming. There may be other places where validators are called.

Is this problematic to your use of validators? And if it is, how are you using validators?

Best regards,
Wim.

RE: [v3] Validator called 3 times before any actual input - Added by Vincenzo Romano 4 months ago

Thanks Wim.

My aim is to use validators so that the user input has been ... validated before being processed by software.
To me it makes no sense to have some code being run without any reason.
As far as the data I handle this should be harmless.
But as far as the computing resources and the overall data flow, it's not OK at all.

Anyway, I need to understand why and when those validators are being fired.
So I can adapt my design to that if needed.

RE: [v3] Validator called 3 times before any actual input - Added by Wim Dumon 4 months ago

Hey Vincenzo,

Wt may validate the input whenever there's a need for it, not just 'without any reason'. We don't consider it an expensive function, so we do no effort to validate just once and cache the result (an approach which would also have its drawbacks in terms of resources).

Best regards,
Wim.

RE: [v3] Validator called 3 times before any actual input - Added by Vincenzo Romano 4 months ago

I have an empty WLineEdit with a simple validator attached.
While I can understand that the validator can be triggered as soon as the control is rendered even if empty,
I don't understand why it's triggered another two extra times before any actual input is done.
And this is the original reason for my question: to understand why.

Maybe a second validator trigger could happen if the same is being implemented as both client-side and server-side.
But that would be puzzling to me, while still meaningful. Can this be the case?

I still cannot understand the third trigger.

RE: [v3] Validator called 3 times before any actual input - Added by lm at 4 months ago

Sounds like you're pretty interested in getting to the bottom of this. If so, try putting breakpoints on the validator and see what codepaths are calling the validation? If it's different call sites, it would be illuminating. If it's the same call site, Wim's answer is probably most pertinent.

Many events happen on a control's creation: It is constructed, added, rendered, maybe finalized, "blurred", validated, maybe changed (to set its value to "")? Also, as the rest of the page changes (widgets are added, the input is placed into a widget, etc., etc.), perhaps validate is called again. I'm not surprised at all to hear that validation is called several times.

To me it makes no sense to have some code being run without any reason.

It's somewhat likely that the efficiency gained by only calling validate when necessary is ruined by the increased code to check and make sure validation doesn't need to be done, to say nothing of the added code complexity.

If your validator is complex enough that calling multiple times is unacceptable, perhaps you should memoize?

RE: [v3] Validator called 3 times before any actual input - Added by Vincenzo Romano 4 months ago

Thanks for the insight. But I think I haven't been clear. My bad.

If you see my test code, I want to call the validator on textInput() event.
Upon first page rendering the validator is called three times, even before any input is done on that single control.

I want to know, if possible, why this is happening.

RE: [v3] Validator called 3 times before any actual input - Added by Wim Dumon 4 months ago

Hello Vincenzo,

Use the source ;-) Or the debugger, as Im suggested, which will immediately point you to the right locations.

If this turns out to be a bug which causes functional misbehaviour, I'll be happy to help you further. But since we don't specify that a validator will not be called unless strictly and absolutely required, and it would mean a lot of effort to implement such behaviour, I don't think it is relevant if a validator is called (once or multiple times) before any input is entered. Note that indeed, a form field without input can be invalid, if you impose that it cannot be empty, and as such it makes sense to call validators even if no event ever happened and no text has ever been set on that form field.

Best regards,
Wim.

RE: [v3] Validator called 3 times before any actual input - Added by Vincenzo Romano 4 months ago

Hi Wim! I was already on those directions:

Any idea? Or should I study all the source code to understand?

The validator being called once on rendering makes sense and I agree with you (or the other way around).

While I can understand that the validator can be triggered as soon as the control is rendered even if empty,

I don't understand why it's triggered another two extra times before any actual input is done.

Being called three times upon rendering, is simply puzzling to me.

As the overall architecture tend to be highly asynchronous with stuff happening both on the server and on the client,
I think that debugging would be rather overwhelming if not useless.
I am not aiming at "fixing" anything, but just understanding.

What I finally understood is that "it simply can happen and that's it".

(1-10/10)