Welcome to the nuBuilder Forums!

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

Updating a subform field in BS (before save)

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
rrandall
Posts: 9
Joined: Fri Dec 24, 2021 11:31 am

Updating a subform field in BS (before save)

Unread post by rrandall »

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?
kev1n
nuBuilder Team
Posts: 4304
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)

Unread post by kev1n »

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.

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;

}
Let me know if you have further questions.
rrandall
Posts: 9
Joined: Fri Dec 24, 2021 11:31 am

Re: Updating a subform field in BS (before save)

Unread post by rrandall »

It works! Thank you :D
Post Reply