retrieves an array of modified fields and associated new values.
/** * Validates a given record and saves possible errors in Doctrine_Validator::$stack * * @param Doctrine_Record $record * @return void */ public function validateRecord(Doctrine_Record $record) { $table = $record->getTable(); // if record is transient all fields will be validated // if record is persistent only the modified fields will be validated $fields = $record->exists() ? $record->getModified() : $record->getData(); foreach ($fields as $fieldName => $value) { $table->validateField($fieldName, $value, $record); } }
/** * enforces the "can't modify the past" rules * * @param $record * @return null */ private function enforceTemporalModConstraints(Doctrine_Record &$record) { $today = date($this->date_format); $modified_table = $record->getTable()->getComponentName(); $modified_from = $record->getModified(true); $modified_to = $record->getModified(); $problem = false; if (@$modified_from[$this->_options['eff_date']] && $modified_from[$this->_options['eff_date']] < $today) { $problem = 'from effective'; $date = $modified_from[$this->_options['eff_date']]; } elseif (@$modified_from[$this->_options['exp_date']] && $modified_from[$this->_options['exp_date']] < $today) { $problem = 'from expiration'; $date = $modified_from[$this->_options['exp_date']]; } elseif (!is_null($record[$this->_options['exp_date']]) && $record[$this->_options['exp_date']] < $today) { $problem = 'expiration'; $date = $record[$this->_options['exp_date']]; } if ($problem) { throw new Doctrine_Temporal_PastModificationException("Won't modify past {$modified_table} {$problem} date: " . $date); } // now, check for modification of a current record OTHER THAN SETTING EXP_DATE if ($record[$this->_options['eff_date']] < $today) { foreach ($modified_to as $k => &$v) { // let the exp_date change, since it doesn't have any effect on the past if ($k == $this->_options['exp_date']) { continue; } // make sure the value has actually changed (sometimes re-setting the same value will give a false positive) if ($v == $record[$k]) { continue; } // some other modified field was found, which is not allowed (since it would affect the past) throw new Doctrine_Temporal_PastModificationException("Won't modify current record (except terminating it). Requested to save {$k} = {$v} as of {$today}."); } } }
/** * validates a given record and saves possible errors * in Doctrine_Validator::$stack * * @param Doctrine_Record $record * @return void */ public function validateRecord(Doctrine_Record $record) { $columns = $record->getTable()->getColumns(); $component = $record->getTable()->getComponentName(); $errorStack = $record->getErrorStack(); // if record is transient all fields will be validated // if record is persistent only the modified fields will be validated $data = $record->exists() ? $record->getModified() : $record->getData(); $err = array(); foreach ($data as $key => $value) { if ($value === self::$_null) { $value = null; } elseif ($value instanceof Doctrine_Record) { $value = $value->getIncremented(); } $column = $columns[$key]; if ($column['type'] == 'enum') { $value = $record->getTable()->enumIndex($key, $value); if ($value === false) { $errorStack->add($key, 'enum'); continue; } } if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) { if (!$this->validateLength($column, $key, $value)) { $errorStack->add($key, 'length'); continue; } } foreach ($column as $name => $args) { if (empty($name) || $name == 'primary' || $name == 'protected' || $name == 'autoincrement' || $name == 'default' || $name == 'values' || $name == 'sequence' || $name == 'zerofill') { continue; } if (strtolower($name) == 'length') { if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_LENGTH_VLD)) { if (!$this->validateLength($column, $key, $value)) { $errorStack->add($key, 'length'); } } continue; } if (strtolower($name) == 'type') { if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) { if (!self::isValidType($value, $column['type'])) { $errorStack->add($key, 'type'); } } continue; } $validator = self::getValidator($name); if (!$validator->validate($record, $key, $value, $args)) { $errorStack->add($key, $name); //$err[$key] = 'not valid'; // errors found quit validation looping for this column //break; } } if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) { if (!self::isValidType($value, $column['type'])) { $errorStack->add($key, 'type'); continue; } } } }
/** * @return bool */ public function isValid() { $tableColumns = $this->_table->getColumns(); $dirtyColumns = $this->_data->getModified(); foreach ($dirtyColumns as $key => $value) { unset($dirtyColumns[$key]); $dirtyColumns[strtolower($key)] = $value; } #die(Zend_Debug::dump($tableColumns)); foreach ($tableColumns as $columnName => $columnStructure) { if (array_key_exists($columnName, $dirtyColumns)) { $validatorChain = new Zend_Validate(); // Notnull if (array_key_exists('notnull', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_NotEmpty(array('string' => true, 'empty_array' => true, 'null' => true, 'space' => true))); } // Email if (array_key_exists('email', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_EmailAddress()); } // Notblank if (array_key_exists('notblank', $columnStructure)) { $validatorChain->addValidator(new Kebab_Validate_NotBlank()); } // Nospace if (array_key_exists('nospace', $columnStructure)) { // check null $validatorChain->addValidator(new Zend_Validate_NotEmpty(array('null' => true))); // check space $validatorChain->addValidator(new Zend_Validate_Regex(array('pattern' => '/\\s/'))); } // Past // Future // Min Length if (array_key_exists('minlength', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_GreaterThan(array('min' => $columnStructure['minlength']))); } // Country // Ip if (array_key_exists('ip', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_Ip()); } // HtmlColor // Range if (array_key_exists('range', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_Between(array('min' => $columnStructure['range'][0], 'max' => $columnStructure['range'][1]))); } //Unique if (array_key_exists('unique', $columnStructure)) { $validatorChain->addValidator(new Kebab_Validate_Unique($columnName, $this->_data)); } // Regex if (array_key_exists('regexp', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_Regex(array('pattern' => $columnStructure['regexp']))); } // Digits if (array_key_exists('digits', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_Digits()); } // Date if (array_key_exists('date', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_Date()); } // CC if (array_key_exists('cc', $columnStructure)) { $validatorChain->addValidator(new Zend_Validate_CreditCard()); } // Unsigned // Check All if (!$validatorChain->isValid($dirtyColumns[$columnName])) { $translator = Zend_Registry::get('Zend_Translate'); Zend_Validate_Abstract::setDefaultTranslator($translator); $this->_errors[$columnName] = $validatorChain->getMessages(); } unset($validatorChain); } } if (count($this->_errors) > 0) { return false; } else { return true; } }