PHPMaker has the ability to generate the List page that contains the Grid-Edit which can be used to edit or update multiple records by clicking once only. As we have already known, this Grid-Edit is powerful to edit multiple records at once. It has the ability also to either add a new row, delete the existing row, or even delete the existing record(s) in the Grid-Edit on the fly by using Javascript before save all the records in the grid. In other words, there are too many the possibility of records condition before you save them using Grid-Edit, and we have to handle it.
Most of the time, you usually want to validate those multiple records before saving them to the database. This following trick will show you how to validate multiple records in Grid-Edit page. The most important things that you should know is how to get the key values from Grid-Edit, so that you can validate each record based on the record key. In addition, you can learn how to get the certain value that belongs to the certain field. In this example, we assume Email is one of the fields and we want to verify whether the Emails, for example, are the allowed emails in your company (we will not discuss about this as it's just an example).
[hidepost]
-
Open your PHPMaker project (.pmp) file, and then go to Server Events -> Table-Specific -> Common -> Row_Updating. Copy the following code inside the Row_Updating function:
global $objForm; // don't forget this! $objForm->Index = -1; $rowcnt = strval($objForm->GetValue("key_count")); if ($rowcnt == "" || !is_numeric($rowcnt)) $rowcnt = 0;$sKey = ""; $sEmail = ""; for ($rowindex = 1; $rowindex <= $rowcnt; $rowindex++) { $objForm->Index = $rowindex; $rowkey = strval($objForm->GetValue("k_key")); // get the key of each row in grid-edit // put your code here to validate the records based on the record key // ... $sEmail = strval($objForm->GetValue("x_Email")); // get the email value of each row in grid-edit // put your code here if you want to validate whether the emails are the allowed emails or not // ... if ($rowkey != "" && $sEmail != "") { // only process the valid rows! $sKey .= $rowkey.", <br />"; // <-- this will echo the key values, make sure they are valid } } // echo the $sKey value to make sure the key is valid echo $sKey; - Re-generate your script files using PHPMaker, as always.
Unfortunately, it has a limitation, since if you add new row(s), then the new row(s) will not be validated since it is handled by Javascript which will be run on browsers, whereas that validation code above uses PHP which will be run on server side. In other words, the validation code above only suits with the original grid-edit when the page is loaded. However, when you remove one or several rows from the existing grid-edit, then the rest of rows will be validated properly.
Updated on February 28, 2013: The best approach to validate multiple records in Grid-Edit is by using Row_Updating of Server Events.
For example:
function Row_Updating($rsold, &$rsnew) {
// Enter your code here
// To cancel, set return value to FALSE
if ($rsnew["MyFieldName"] != "something") {
$this->setFailureMessage("Sorry, <strong>".$rsnew["MyFieldName"]."</strong> is not valid.");
return FALSE;
}
return TRUE;
}
That code will validate any records that you either added, modified, or deleted via Grid-Edit, too. Isn't PHPMaker great?
[/hidepost]
I’m doing the calculation on the grid is working perfectly, but when I delete a line in the table grid Order_Details the value is not updated in the filed AMOUNT in ORDERS table, exites some way to automate this? And again, thank you.
Yep. This is the limitation of the Grid-Add and Grid-Edit at the moment. There is no the certain event to catch the deleting a row in the grid. Still figuring out how to implement it.
É possível escrever o código: $this->Button->Edit Attrs[“onkeyup”] = “CalculateGrid3(event);”; no botão SALVAR? Para que quando o usuário clicasse em Salvar chamasse a função novamente, para realizar o cálculo correto?
You can write the code: $ this-> Button-> Edit Attrs [ “onkeyup”] = “CalculateGrid3 (event);”; the SAVE button? So that when the user clicks Save call the function again, to perform the correct calculation?
Just try it, and let us know the result.