/** * Test for get errors */ public function testErrors() { $data = array('field' => 'title', 'message' => 'Is a required field'); $result = array($data); $error = new Phprojekt_Error(); $error->addError(); $return = $error->getError(); $this->assertEquals(array(array()), $return); $error->addError($data); $return = $error->getError(); $this->assertEquals($result, $return); }
/** * Validate that $start is before $end. * * @param string $start Start date value. * @param string $end End date value. * * @return boolean True for valid. */ public function validateDateRange($start, $end) { if (!empty($start) && !empty($end)) { if (strtotime($start) > strtotime($end)) { $this->error->addError(array('field' => "Period", 'label' => Phprojekt::getInstance()->translate('Period'), 'message' => Phprojekt::getInstance()->translate('End date can not be before Start date'))); return false; } } return true; }
/** * Validate the fields definitions per each field. * * @param array $data The field definition. * @param integer $saveType Type of module save (0 for normal -under project-, 1 for global). * * @return boolean True for valid. */ public function recordValidate($data, $saveType = 0) { $valid = true; $this->_error = new Phprojekt_Error(); if (empty($data)) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('The Module must contain at least one field'))); } if ($valid && empty($data[0]['tableName'])) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('Please enter a name for this module'))); } else { if ($valid && !preg_match("/^[a-zA-Z]/", $data[0]['tableName'])) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('The module name must start with a letter'))); } } $foundFields = array(); $foundProjectId = false; $foundListPosition = false; foreach ($data as $field) { if ($valid && (!isset($field['tableLength']) || !isset($field['tableField']) || !isset($field['tableType']) || !isset($field['formType']))) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('Invalid parameters'))); } if ($valid) { $field['tableLength'] = intval($field['tableLength']); } if ($valid && empty($field['tableField'])) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('All the fields must have a table name'))); break; } else { if ($valid && in_array($field['tableField'], $foundFields)) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('There are two fields with the same ' . 'Field Name'))); break; } else { if ($valid) { $foundFields[] = $field['tableField']; } } } if ($valid && $field['tableType'] == 'varchar') { if ($field['tableLength'] < 1 || $field['tableLength'] > 255) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('The length of the varchar fields must be ' . 'between 1 and 255'))); break; } } if ($valid && $field['tableType'] == 'int') { if ($field['tableLength'] < 1 || $field['tableLength'] > 11) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('The length of the int fields must be between' . ' 1 and 11'))); break; } } if ($valid && $field['formType'] == 'selectValues') { if ($valid && !isset($field['formRange'])) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('Invalid form Range for the select field'))); break; } else { $field['formRange'] = trim($field['formRange']); } if ($valid && !strstr($field['formRange'], '#')) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('Invalid form Range for the select field'))); break; } else { if ($valid && isset($field['selectType'])) { switch ($field['selectType']) { case 'project': case 'user': case 'contact': if ($valid && count(explode('#', $field['formRange'])) != 3) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('Invalid form Range for ' . 'the select field'))); } break; default: if ($valid && !strstr($field['formRange'], '|')) { // Do not have "|" if (count(explode('#', $field['formRange'])) != 3) { // Invalid module format $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('Invalid form ' . 'Range for the select field'))); } else { // Check if the module format is correct list($module, $key, $value) = explode('#', $field['formRange']); $module = trim($module); $key = trim($key); $value = trim($value); if (Phprojekt_Module::getId($module) == 0) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('Invalid form ' . 'Range for the select field'))); } } } else { // Have "|", check it foreach (explode('|', $field['formRange']) as $range) { if ($valid && count(explode('#', trim($range))) != 2) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('Invalid form Range ' . 'for the select field'))); } } } break; } } } if ($field['tableField'] == 'project_id') { $foundProjectId = true; } } if (isset($field['listPosition']) && $field['listPosition'] > 0) { $foundListPosition = true; } } if ($valid && !$foundProjectId && $saveType != 1) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('The module must have a project selector called ' . 'project_id'))); } if ($valid && !$foundListPosition) { $valid = false; $this->_error->addError(array('field' => 'Module Designer', 'label' => Phprojekt::getInstance()->translate('Module Designer'), 'message' => Phprojekt::getInstance()->translate('The module must have at least one field with the ' . 'list position greater than 0'))); } return $valid; }
/** * Return if the values are valid or not. * * @return boolean True for valid. */ public function recordValidate($class, $data, $fields) { $valid = true; foreach ($data as $varname => $value) { if (isset($class->{$varname})) { // Validate with the database_manager stuff foreach ($fields as $field) { if ($field['key'] == $varname) { // Check required if (true === $field['required']) { $error = $this->validateIsRequired($value); if (null !== $error) { $valid = false; $this->error->addError(array('field' => $varname, 'label' => $field['label'], 'message' => $error)); break; } } // Check value length if (isset($field['length']) && $field['length'] > 0) { if (strlen($value) > $field['length']) { $valid = false; $this->error->addError(array('field' => $varname, 'label' => $field['label'], 'message' => Phprojekt::getInstance()->translate('Maximum length exceeded for ' . 'field'))); break; } } // Check value in range if (($field['type'] == 'selectbox' || $field['type'] == 'multipleselectbox') && !empty($value)) { $found = false; foreach ($field['range'] as $range) { if ($range['id'] == $value) { $found = true; break; } } if (!$found) { $valid = false; $this->error->addError(array('field' => $varname, 'label' => $field['label'], 'message' => Phprojekt::getInstance()->translate('Value out of range'))); break; } } else { if ($field['type'] == 'rating' && !empty($value)) { if ($value > $field['range']['id'] || $value < 1) { $valid = false; $this->error->addError(array('field' => $varname, 'label' => $field['label'], 'message' => Phprojekt::getInstance()->translate('Value out of range'))); break; } } } // Check value by type $error = $this->validateValue($class, $varname, $value); if (false === $error) { $valid = false; $this->error->addError(array('field' => $varname, 'label' => $field['label'], 'message' => Phprojekt::getInstance()->translate('Invalid Format'))); } break; } } // Validate an special fieldName $validator = 'validate' . ucfirst($varname); if ($validator != 'validateIsRequired') { if (method_exists($class, $validator)) { $error = call_user_func(array($class, $validator), $value); if (null !== $error) { $valid = false; $this->error->addError(array('field' => $varname, 'label' => $field['label'], 'message' => $error)); } } } } } return $valid; }