function isValid() { $valid = true; if (!$this->getData('company_id')) { $this->errors[] = 'company_id must be set.'; $valid = false; } if (!$this->getData('amount_due')) { if (!$this->getData('start_date')) { $this->errors[] = 'start_date must be set.'; $valid = false; } if (!$this->getData('end_date')) { $this->errors[] = 'end_date must be set.'; } } if ($valid && parent::isValid()) { return true; } }
/** * Finds and retuns a HTML formatted string with errors for an ActiveRecord object * * Eg. * <code> * <?=ActiveRecordHelper::error_messages_for($person);?> * </code> * will show all the errors for the person object. * * Valid options: * <ul> * <li>css_class [ default formErrors ] for: <div id="" class="$css_class"></li> * <li>heading [ default 2 1-->6 ] for: <h$heading><2 errors prohibited this form from beeing saved;/h$heading> * <li>singular [ default error ] for: 1 $singular prohibited this...</li> * <li>plural [ default errors ] for: 2 $plural prohibited this...</li> * <li>oname [ default object name] for: the active record object name</li> * </ul> * * @param ActiveRecord object the ActiveRecord object to check for errors * @param array options the options where we can cusomize the look and feel of the error message. * this includes: css_class and heading * @return string a HTML formatted string */ public static function error_messages_for(ActiveRecord $record, $options = array()) { if ($record->isValid()) { return; } $css_class = isset($options['css_class']) ? $options['css_class'] : 'formErrors'; $heading = isset($options['heading']) && (int) $options['heading'] > 0 && (int) $options['heading'] < 6 ? $options['heading'] : 2; $singular = isset($options['singular']) ? $options['singular'] : 'error'; $plural = isset($options['plural']) ? $options['plural'] : 'errors'; $oname = isset($options['oname']) ? $options['oname'] : ucfirst($record->getClassName()); $buffer = '<div id="medickFormErrors" class="' . $css_class . '">'; $part = '<ul>'; foreach ($record->getErrors() as $error) { $part .= '<li>' . $error . '</li>'; } $part .= '</ul>'; $st = sizeof($record->getErrors()) == 1 ? $singular : $plural; $buffer .= '<h' . $heading . '>' . sizeof($record->getErrors()) . ' ' . $st . ' prohibited this '; $buffer .= $oname . ' from being saved</h' . $heading . '>'; $buffer .= "\n<p>There were problems with the following fields:</p>\n"; $buffer .= $part; return $buffer . '</div>'; }
function isValid() { $valid = true; if (!Util::is_a_date($this->get('start_date'))) { $this->errors[] = 'Contract ' . $this->id . ' has no start date.'; $valid = false; } if ($valid && parent::isValid()) { return true; } }