Page 1 of 1
Validazione example
Posted: Fri Apr 12, 2024 2:48 pm
by Basso
Good morning, could you help me by providing a simple example where during the validation phase of a field, the entered value is checked, and if it exceeds a predetermined value, a new window is opened asking whether to proceed or cancel data saving.
Thanks in advance
Basso
Re: Validazione example
Posted: Fri Apr 12, 2024 6:22 pm
by kev1n
Hi,
You can use the `nuBeforeSave` function to perform validation checks on a field before saving the data. In this function, you can use `nuGetValue` to get the value of the field you want to validate. If the value exceeds a predetermined threshold, you can use JavaScript functions to display a confirmation dialog asking the user whether to proceed or cancel the data saving.
Here's an example implementation:
Define the `nuBeforeSave` function:
- This function will be triggered before saving the data.
- Inside the function, use `nuGetValue` to retrieve the value of the field you want to validate.
- Check if the value exceeds the predetermined threshold.
- If the value exceeds the threshold, open a confirmation dialog.
- If the user chooses to proceed, return `true` to allow saving. If the user chooses to cancel, return `false` to prevent saving.
Here's a code snippet demonstrating the implementation (Add to the form's Custom Code field):
Code: Select all
function nuBeforeSave() {
// Retrieve the value of the field you want to validate
var fieldValue = nuGetValue('your_field_id'); // Replace 'your_field_id' with the actual object ID
// Define the predetermined threshold
var threshold = 100; // Replace 100 with the desired threshold value
// Check if the field value exceeds the threshold
if (fieldValue > threshold) {
// Show a confirmation dialog asking the user whether to proceed
var proceed = confirm("The value you entered exceeds the predetermined threshold of " + threshold + ". Do you want to proceed?");
// Return true if the user wants to proceed, false otherwise
if (proceed) {
return true; // Allow data saving
} else {
return false; // Cancel data saving
}
}
// If the field value does not exceed the threshold, allow data saving
return true;
}
- The `nuGetValue('your_field_id')` function retrieves the value of the field you want to validate. Replace `'your_field_id'` with the actual ID of the field.
- The function checks if the retrieved value exceeds a predetermined threshold (in this case, 100).
- If the value exceeds the threshold, a confirmation dialog is shown using `confirm()`.
- The function returns `true` to allow data saving if the user chooses to proceed, or `false` to cancel data saving if the user chooses to cancel.
Re: Validazione example
Posted: Tue Apr 16, 2024 4:50 pm
by Basso
Thank you for your response.
Unfortunately, I tried to implement the code you sent me, but unfortunately I received un error code ( see below) .
I use PHP 8.2.
You will surely have understood that I'm not an expert, but thank you anyway if you want to help me
Basso
Before Save of Form FF2
/var/www/html/nuBuilderForte/core/nucommon.php(1448) : eval()'d code
syntax error, unexpected token "var"
traced from...
(line:116) /var/www/html/nuBuilderForte/core/nuapi.php - nuUpdateDatabase
(line:364) /var/www/html/nuBuilderForte/core/nudata.php - nuUpdateDatabaseRunBeforeSaveEvents
(line:623) /var/www/html/nuBuilderForte/core/nudata.php - nuEval
Re: Validazione example
Posted: Tue Apr 16, 2024 5:15 pm
by kev1n
Where did you insert the code? It is JavaScript and has to be placed in the form's custom code, not in a PHP event.
Re: Validazione example
Posted: Tue Apr 16, 2024 5:33 pm
by Basso
Many Thanks , now workink fine
Re: Validazione example
Posted: Tue Apr 16, 2024 5:59 pm
by Basso
Sorry kev1n,
Could you give me a small example to use in PHP BEFORE SAVING or AFTER SAVING
For example, I would like to increase the value that a user is entering by 5.
Thanks in advance
Re: Validazione example
Posted: Tue Apr 16, 2024 7:11 pm
by kev1n
In PHP Before Save:
Code: Select all
// Get the value of the object "test_field"
$value = '#test_field#';
if (is_numeric($value)) {
// Increase the value of the variable by 5
$value += 5;
$nuMainForm = nuHash()['nuFORMdata'][0]->id;
// Replace the value of the object "test_field" with the new value
nuSetNuDataValue($nudata, $nuMainForm, 'test_field', $value);
}
Re: Validazione example
Posted: Tue Apr 16, 2024 7:32 pm
by Basso
Many Thanks , now workink fine