Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

nuBeforeSave() data validation using Lookup fields Topic is solved

Questions related to using nuBuilder Forte.
Post Reply
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

nuBeforeSave() data validation using Lookup fields

Unread post by incoherence »

Hi, a simple question...

I have nuBeforeSave() validation working, based on the Code Library entry: "Edit Screen: How to validate form entries before saving?".

That example does not use any Lookup fields though. I need to check that the Lookup is set to one particular value (out of about five possibilities) and disallow saving unless that is the case.

In code, I can get the primary key value for the currently populated/displayed Lookup field on the form, but I would prefer not to check against hard-coded key primary key values in my data validation code!

Is there a way, in JavaScript, of fetching the value that is actually displayed on the form in the Lookup field instead of the primary key value associated with that displayed value, that I am currently getting with:-

keyValue = $('#lookup_field_id').val();

(If not possible, I guess I would have to look up the field with some simple SQL, but I am hoping that validation of forms that have Lookup fields is a fairly common requirement and that there is an easy way to do this...?).
kev1n
nuBuilder Team
Posts: 4294
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: nuBeforeSave() data validation using Lookup fields

Unread post by kev1n »

Hi,

There are two possibilities:

- 1. Use SQL as you mentioned

- 2. In JavaScript, set a Hash Cookie with the lookup code:

E.g. if the object id is sus_zzzzsys_access_id, set the code field as a Hash Cookie:

Code: Select all

function nuBeforeSave() {
	nuSetProperty('sus_zzzzsys_access_idcode', sus_zzzzsys_access_idcode.value);
}	


In PHP retrieve it like this:

Code: Select all

$sus_zzzzsys_access_idcode	= "#sus_zzzzsys_access_idcode#";
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

Re: nuBeforeSave() data validation using Lookup fields

Unread post by incoherence »

Thank you Kev1n, this helped a lot - I got the validation working wholly in JavaScript.
I think I read somewhere that JS data validation, because it runs on the client, could be bypassed by someone who knows what they are doing.
Presumably that means that the best (most secure) place to do data validation is on the server side, in PHP, is that correct?
I found the Code Library entry: "Edit Screen: How to validate form entries before saving?" was very useful to get me started.
That only covers validation on the JS side though - is there anything similar that you can point me to as an example of how to do data validation on the server side in PHP, please?
Thanks again for your help! :thumb:
kev1n
nuBuilder Team
Posts: 4294
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: nuBeforeSave() data validation using Lookup fields

Unread post by kev1n »

To validate field on the server side, use PHP code and Hash Cookies.

Simple Example:

Code: Select all

if ("#your_field" !== 'abc') {
  nuDisplayError('the field your_field contains an invalid value');   // abort saving
  return;
}
Let me know if you need more information.
kev1n
nuBuilder Team
Posts: 4294
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: nuBeforeSave() data validation using Lookup fields

Unread post by kev1n »

Is this solved or do you still need our support?
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

Re: nuBeforeSave() data validation using Lookup fields

Unread post by incoherence »

Thanks for asking, kev1n! I have been a bit preoccupied with adding single sign on (SSO) functionality to nuBuilder (hopefully I can share it soon (for review/consideration to be integrated into the codebase), but should be able to get back to this data validation topic in November.
kev1n
nuBuilder Team
Posts: 4294
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: nuBeforeSave() data validation using Lookup fields

Unread post by kev1n »

incoherence wrote: Sun Oct 30, 2022 4:17 pm but should be able to get back to this data validation topic in November.
I'm wondering if this is solved.
incoherence
Posts: 22
Joined: Sun May 08, 2022 2:36 pm
Has thanked: 9 times
Been thanked: 1 time

Re: nuBeforeSave() data validation using Lookup fields

Unread post by incoherence »

Thanks Kev1n - yes, I just completed rewriting my JS data validation code as PHP (and SQL for Lookup fields).

Solved and many thanks for your help!
Post Reply