val_fun only if changed

Discuss MySQL Ajax Table Editor Paid Version

val_fun only if changed

Postby MikeW » Thu May 06, 2021 1:35 pm

If I add a record, I do a validation with:

Code: Select all
$tableColumns['datei'] = array(
         'display_text' => 'Datei',
         'perms' => 'EVAXTQSFHO',
         'input_info' => 'size="40"',
         'val_fun' => array(&$this,'validateFun'),
);


and the function:
Code: Select all
public function validateFun($col,$val,$row)
   {
          $query = "select datei from table where datei = :name";
        $queryParams = array('name' => $val);
             $stmt = DBC::get()->prepare($query);
             $stmt->execute($queryParams);
             $count = $stmt->rowCount();
      if ($count == 0)
      {
         return true;
      }
      else
      {
         $this->Editor->retArr[] = array('where' => 'javascript', 'value' => 'alert("Sorry this value \"'.$val.'\" already exists in Datei");');
         return false;
      }
}


This works fine.
But if I edit the another field, I get the warning 'Datei already exist' after saving.
I can remove the permission 'E 'from filed 'datei', then it works all ok.
But, sometimes I must change the value of 'datei',
therefore I can NOT remove the 'E' permission.

I think, I should do the validation only, if the value of 'datei' has changed?
How can I do this ? Spent several hours, no solution.
MikeW
 
Posts: 25
Joined: Mon Oct 31, 2011 11:43 am

Re: val_fun only if changed

Postby MikeW » Wed May 12, 2021 4:45 pm

I found the solution.
Code: Select all
public function validateFun($col,$val,$row)
   {
           $query = "select id, datei from table where datei = :name"; // changed
        $queryParams = array('name' => $val);
         $stmt = DBC::get()->prepare($query);
         $stmt->execute($queryParams);
         $count = $stmt->rowCount();
    $dbrow = $stmt->fetch(); //added

      if ($count == 0 || $dbrow['id'] == $row['id'] ) // changed
      {
         return true;
      
      }

      $tableColumns['id'] = array(
         'display_text' => 'ID',
         'perms' => 'VEAXTSF',
         'hidden_edit' => true, // added
         'hidden_add' => true  // added
      );

           $tableColumns['datei'] = array(
         'display_text' => 'Datei',
         'perms' => 'VEAXTQSFHO', // changed
         'input_info' => 'size="40"',
         'val_fun' => array(&$this,'validateFun'),
      );

This helps me also:
https://mysqlajaxtableeditor.com/discussmate/viewtopic.php?f=4&t=985&p=3883&hilit=+current+id+&sid=af2461c536706395f7c6f68cf6b95b25&sid=af2461c536706395f7c6f68cf6b95b25#p3883
MikeW
 
Posts: 25
Joined: Mon Oct 31, 2011 11:43 am


Return to Paid Version

Who is online

Users browsing this forum: No registered users and 2 guests

cron