This topic is locked
[SOLVED]

How to show appropriate field validation message with sweet alert

1/11/2024 4:38:47 PM
PHPRunner General questions
K
kelgate author

Hi,
Before anyone says RTFM :) I have read the manual and looked for solutions in the forum, FB page, etc.

What I want to do on an inline edit page is compare an input field (competitor's score) with a maximum value (on the same row) and to do the following:

  • if the score is greater than the maximum, stop the record from updating and show a sweet alert with an error message
  • if the score is less than half the maximum, allow the record to update and show a sweet alert with a warning message
  • if the score is within limits, allow the record to update and show a sweet alert with an update successful message.

I am setting session variables in the Before Record Updated (following validation checks) and After Record Updated events and then using those session variables to set proxy values in the Before Display event so that they can trigger an appropriate sweet alert in the Javascript Onload event.

Problem: if I return false in the Before Record Updated event, then the Javascript Onload event doesn't get triggered to display the update successful message.

I have tried using field events to validate the input and show the appropriate message for each validation outcome, but in the case of the greater than the maximum value being entered, the User can click the Save checkmark a second time after getting the correct sweetalert message with the result that the too high value is saved. I guess this is because the Field Event Before Client isn't being triggered again because the field change event is only recognised for the first time through.

Apologies if I have missed something obvious, but I've spent a few days trying things out and have run out of new ideas.

Many thanks in advance.

Admin 1/11/2024

You can use Field Events for this purpose:
https://xlinesoft.com/phprunner/docs/field_events.htm

This will allow you to perform validation and display SweetAlert message without reloading the page, as soon as this field loses the focus.

We also had a DevClub webinar dedicated to this technique back in June:
https://xlinesoft.com/devclub

img alt

fhumanes 1/12/2024

Hi @kelgate,

Look at this example that makes the validation of a field with recovery of information in the Server.

https://fhumanes.com/blog/guias-desarrollo/guia-27-validaciones-por-ajax-personalizados/

In the article is the error message in an "alert" and in the example it is in the standard format of PHPRUNNER.

Greetings,
fernando

K
kelgate author 1/12/2024

Many thanks "Admin".

I have used Field Events as one of my techniques to handle validation and it works well as far as the initial run through for both Return True (where the entered value is acceptable, with just a warning if low) and Return False cases (where the entered value is greater than the maximum allowable score).

The issue is that in the Return False case the page is not reloaded so the entered (too high) value is still present in the input field, but if the User clicks the save checkmark a second time then the Field Event is not triggered so there are no Field event validation checks performed and the invalid value is then saved.

This is using the "standard" in-line edit page. Would it be different if I used Spreadsheet Mode?

Thanks Fernando,

I'll get your example translated and take a more detailed look.

All the best to Both.

Kelvin

Admin 1/12/2024

There tons of options here.

  1. You can clear the field if it didn't pass the validation so the user has to re-enter the correct value. Or you can set it automatically to the highest value available.
  2. If you need to perform some additional validation before record is saved but need to do this in Javascript - use beforeSave event. https://xlinesoft.com/phprunner/docs/how_to_ask_for_confirmation_before_saving_record.htm
  3. Yet another option, you can create a custom validation plugin and utilize the built-in PHPRunner's validation: https://xlinesoft.com/phprunner/docs/validation_types.htm#validation_plugin

K
kelgate author 2/14/2024

Many thanks for your suggestions and apologies for my delay in responding.

I have tried the options you have suggested and have managed to get a field event example working.

However, I have more than one score field in a the relevant table (round-1, round-2, round-3, etc), so the custom field validation plugin is the most convenient for multiple use.

But, I'm struggling to get the Max Score value into the function using in-line edit in a list view.

My code is:

function score_validate(sVal)
{
var ctlMaxTxt = row.getFieldText("max_score");
var ctlMaxInt = parseInt(ctlMaxTxt) || 0;

if(sVal > ctlMaxInt)
return 'Score must not be greater than maximum';
else
return true;

}

but this doesn't work. If I just put a fixed value into ctlMaxInt it works fine, but since the maximum score can vary row-by-row this isn't an option.

I haven't tried setting the score value to zero from this function either, but it is something I'd also like to be able to do.

Thanks again

Kelvin

K
kelgate author 2/17/2024

Thanks folks,

I do have an outstanding question, but you outlined several options, so I'll mark this one as Solved and open another thread with my question.

Thanks again

Kelvin