Hi,
I have been struggling with the following problem for a while now and need some suggestions.
Use case: Auto-correct some text contained in a field on a subform (grid), before it gets saved to the db table. The auto-correct function does a lookup in another db table to get the new "auto-corrected" text.
I had a look into using nuSetNuDataValue() in the form’s BS (before save), but that seems to be used to update fields on the main form only? Is there any way to accomplish this for subforms?
Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Updating a subform field in BS (before save)
-
- Posts: 9
- Joined: Fri Dec 24, 2021 11:31 am
-
- nuBuilder Team
- Posts: 4299
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 445 times
- Contact:
Re: Updating a subform field in BS (before save)
Hi,
nuGetNuDataValue() + nuSetNuDataValue() only work for the main form. The get & set values from a subform, you can use the functions getSubformValues() + setSubformValues() from below.
In the example below all dates that equal to 2022-01-07 are replaced with 2022-01-08 in the subfrom with id sub_something, in the column with object id cus_date.
Let me know if you have further questions.
nuGetNuDataValue() + nuSetNuDataValue() only work for the main form. The get & set values from a subform, you can use the functions getSubformValues() + setSubformValues() from below.
In the example below all dates that equal to 2022-01-07 are replaced with 2022-01-08 in the subfrom with id sub_something, in the column with object id cus_date.
Code: Select all
$formId = 'sub_something'; // <<<-- Your subform object id here
$field = 'cus_date'; // <---- Replace with your object Id
$values = getSubformValues($nudata, $formId, $field); // get all values of the $field, for each row
for ($c = 0;$c < count($values);$c++) { // loop through the value
if ($values[$c] == '2022-01-07') { // if the values eqauls 2022-01-07
setSubformValues($nudata, $formId, $field, $c, '2022-01-08'); // set a new value
}
}
function getSubformValues($nudata, $formId, $field) {
$arr = array();
for ($d = 0;$d < count($nudata);$d++) {
$sf = $nudata[$d];
if ($formId == $sf->id) {
$key = array_search($field, $sf->fields);
if ($key != false) {
for ($r = 0;$r < count($sf->rows);$r++) {
array_push($arr, $sf->rows[$r][$key]);
}
}
}
}
return $arr;
}
function setSubformValues(&$nudata, $formId, $field, $c, $value) {
for ($d = 0;$d < count($nudata);$d++) {
$sf = $nudata[$d];
if ($formId == $sf->id) {
$key = array_search($field, $sf->fields);
if ($key != false) {
$nudata[$d]->rows[$c][$key] = $value;
$nudata[$d]->edited[$c][$key] = 1;
return true;
}
}
}
return false;
}
-
- Posts: 9
- Joined: Fri Dec 24, 2021 11:31 am