Example #1
0
 /**
  * Pre-validates either a single field or the entire command.
  *
  * This method uses the pre-validation rules that are enforceable by the 
  * PHP engine -- for example, type rules, ranges, and four-digit dates. 
  * Rules such as "unique" or "existing," or validation by calculation 
  * field, cannot be pre-validated.
  *
  * If you pass the optional $fieldName argument, only that field is 
  * pre-validated. Otherwise, the command is pre-validated as if execute() 
  * were called with "Enable record data pre-validation" selected in 
  * FileMaker Server Admin Console. If pre-validation passes, validate() 
  * returns TRUE. If pre-validation fails, then validate() returns a  
  * FileMaker_Error_Validation object containing details about what failed 
  * to pre-validate.
  *
  * @param string $fieldName Name of field to pre-validate. If empty, 
  *        pre-validates the entire command.
  *
  * @return boolean|FileMaker_Error_Validation TRUE, if pre-validation 
  *         passes. Otherwise, an Error Validation object.
  */
 public function validate($fieldName = null)
 {
     if (!is_a($this, __NAMESPACE__ . '\\Add') && !is_a($this, __NAMESPACE__ . '\\Edit')) {
         return true;
     }
     $layout = $this->fm->getLayout($this->_layout);
     $validationErrors = new \tranduchieu\FileMaker\FileMakerValidationException($this->fm);
     if ($fieldName === null) {
         foreach ($layout->getFields() as $fieldName => $field) {
             if (!isset($this->_fields[$fieldName]) || !count($this->_fields[$fieldName])) {
                 $values = array(0 => null);
             } else {
                 $values = $this->_fields[$fieldName];
             }
             foreach ($values as $value) {
                 try {
                     $field->validate($value);
                 } catch (\tranduchieu\FileMaker\FileMakerValidationException $e) {
                     foreach ($e->getErrors() as $error) {
                         $validationErrors->addError($error[0], $error[1], $error[2]);
                     }
                 }
             }
         }
     } else {
         $field = $layout->getField($fieldName);
         if (!isset($this->_fields[$fieldName]) || !count($this->_fields[$fieldName])) {
             $values = array(0 => null);
         } else {
             $values = $this->_fields[$fieldName];
         }
         foreach ($values as $value) {
             try {
                 $field->validate($value);
             } catch (\tranduchieu\FileMaker\FileMakerValidationException $e) {
                 foreach ($e->getErrors() as $error) {
                     $validationErrors->addError($error[0], $error[1], $error[2]);
                 }
             }
         }
     }
     if ($validationErrors->numErrors()) {
         throw $validationErrors;
     }
     return true;
 }
Example #2
0
 /* get layouts list */
 echo "Get scripts list...";
 $scripts = $fm->listScripts();
 echo implode(', ', $scripts) . '...<span style="color:green">SUCCESS</span>' . PHP_EOL . PHP_EOL;
 /**
  * Test perform script
  */
 echo "Test perform function...";
 $command = $fm->newPerformScriptCommand($layouts[0], 'create sample data');
 $result = $command->execute();
 echo implode(', ', $scripts) . '...<span style="color:green">SUCCESS</span>' . PHP_EOL . PHP_EOL;
 /*
  * get layout
  */
 echo "Get a layout...";
 $layout = $fm->getLayout($layouts[0]);
 echo '<span style="color:green">SUCCESS</span>' . PHP_EOL . PHP_EOL;
 /* get layout infos */
 echo "------------------------------------------" . PHP_EOL;
 echo " Test Layout object's main methods" . PHP_EOL;
 echo "------------------------------------------" . PHP_EOL;
 echo "Current layout : " . $layout->getName() . PHP_EOL . PHP_EOL;
 /*
  * get field List
  */
 echo "Get FieldsList... ";
 echo implode(', ', $layout->listFields()) . '...<span style="color:green">SUCCESS</span>' . PHP_EOL . PHP_EOL;
 /*
  * get a Field
  */
 echo 'Get a Field... ';