Page 1 of 1

nuBeforeSave() data validation using Lookup fields

Posted: Tue Aug 23, 2022 5:08 pm
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...?).

Re: nuBeforeSave() data validation using Lookup fields

Posted: Tue Aug 23, 2022 5:23 pm
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#";

Re: nuBeforeSave() data validation using Lookup fields

Posted: Sun Sep 25, 2022 4:20 pm
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:

Re: nuBeforeSave() data validation using Lookup fields

Posted: Tue Oct 04, 2022 5:47 pm
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.

Re: nuBeforeSave() data validation using Lookup fields

Posted: Thu Oct 27, 2022 5:23 pm
by kev1n
Is this solved or do you still need our support?

Re: nuBeforeSave() data validation using Lookup fields

Posted: Sun Oct 30, 2022 4:17 pm
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.

Re: nuBeforeSave() data validation using Lookup fields

Posted: Thu Dec 01, 2022 6:08 pm
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.

Re: nuBeforeSave() data validation using Lookup fields

Posted: Sun Dec 04, 2022 6:05 am
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!