Grid Updating Event

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
mpol_ch
User
Posts: 877
Location: Switzerland

Grid Updating Event

Post by mpol_ch »

Hi,
I am using a grid update event.
I want to check the sum of "gewichtung", which is column of details.
The function should check sum of gewichtung for the given record of master table, if the total is greater then 1..
But It does not work.
Any idea?
My records in gewichtung are: 0,1, 0,1, 0.4, and so on.

// Grid Updating event
function Grid_Updating($rsnew)
{
    $rsnew = $this->getGridFormValues(); // Get the form values of the new records as an array of array
    $newtotal = 0;
    foreach ($rsnew as $row) // Loop through the new records
    $newtotal += intval($row["gewichtung"]);
    if ($newtotal < 1)
    {
        // To cancel, set return value to False
        $this->setFailureMessage("The new total  must be larger than the old total.");
        return false;
    }
    return true;
}

mpol_ch


arbei
User
Posts: 9384

Post by arbei »

mpol_ch wrote:

My records in gewichtung are: 0,1, 0,1, 0.4, and so on.
$newtotal += intval($row["gewichtung"]);

  1. The signature of the function should be function Grid_Updating($rsold).
  2. Your data is not only integer, you should not use intval(), you should use floatval() instead.

Otherwise your code looks fine, it did not work?


mpol_ch
User
Posts: 877
Location: Switzerland

Post by mpol_ch »

Perfect, thank you for your input.
The following code has solved the issue.

      // Grid Updating event
      function Grid_Updating($rsnew) {
      $rsnew = $this->getGridFormValues(); // Get the form values of the new records as an array of array
      $newtotal = 0;
      foreach ($rsnew as $row) // Loop through the new records
      $newtotal += floatval($row["gewichtung"]);
     if (round($newtotal,2) != 1.00) {
    // To cancel, set return value to False
    $diff=round($newtotal,2) - 1.00;
     $this->setFailureMessage("The new total  must be larger than the old total.".$diff);
      return false;
       }
      return true;
      }

mpol_ch


Post Reply