Created
August 17, 2012 14:37
-
-
Save jippi/3379240 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| App::uses('AppModel', 'Model'); | |
| /** | |
| * Voucher Model | |
| * | |
| * @property VoucherClaim $VoucherClaim | |
| * @property Place $Place | |
| */ | |
| class Voucher extends AppModel { | |
| /** | |
| * Validation rules | |
| * | |
| * @var array | |
| */ | |
| public $validate = array( | |
| 'name' => array( | |
| 'notEmpty' => array( | |
| 'rule' => array('notEmpty'), | |
| 'message' => 'Name is required', | |
| 'allowEmpty' => false, | |
| 'required' => 'create', | |
| ), | |
| ), | |
| 'description' => array( | |
| 'notEmpty' => array( | |
| 'rule' => array('notEmpty'), | |
| 'message' => 'Description is required', | |
| 'allowEmpty' => false, | |
| 'required' => 'create', | |
| ), | |
| ), | |
| 'max_claims' => array( | |
| 'numeric' => array( | |
| 'rule' => array('numeric'), | |
| 'message' => 'Max Claims needs to be a number', | |
| 'allowEmpty' => true, | |
| 'required' => false | |
| ), | |
| ), | |
| 'is_active' => array( | |
| 'boolean' => array( | |
| 'rule' => array('boolean'), | |
| 'message' => 'Is Active is required', | |
| 'required' => 'create', | |
| ), | |
| ), | |
| 'start_time' => array( | |
| 'datetime' => array( | |
| 'rule' => array('datetime'), | |
| 'message' => 'Start Time is required', | |
| 'allowEmpty' => true, | |
| ), | |
| 'valid' => array( | |
| 'rule' => array('dateComparison', array('<', 'end_time')), | |
| 'message' => 'Start time must be before end time' | |
| ) | |
| ), | |
| 'end_time' => array( | |
| 'datetime' => array( | |
| 'rule' => array('datetime'), | |
| 'message' => 'End Time is required', | |
| 'allowEmpty' => true, | |
| ), | |
| 'valid' => array( | |
| 'rule' => array('dateComparison', array('>', 'start_time')), | |
| 'message' => 'End time must be after start time' | |
| ) | |
| ), | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class SampleBehavior extends ModelBehavior { | |
| /** | |
| * Validate that one timestamp with a comparison operator is valid | |
| * | |
| * Example 1 - Make sure end_time in our model can't be before start_time: | |
| * $validate = array('start_time' => array('sanityCheck' => array('rule' => 'dateComparison', 'compareOperator' => '<', 'compareTo' => 'end_time')))); | |
| * | |
| * Example 2 - Make sure end_time in our model can't be before start_time:: | |
| * $validate = array('start_time' => array('sanityCheck' => array('rule' => 'dateComparison', compareOperator' => 'isless', 'compareTo' => 'end_time')))); | |
| * | |
| * @param Model $model | |
| * @param array $params | |
| */ | |
| public function dateComparison(Model $model, $params, $config = array()) { | |
| $sourceField = array_pop(array_keys($params)); | |
| $sourceValue = array_pop(array_values($params)); | |
| if (empty($config['compareOperator'])) { | |
| throw new NodesValidationException('Missing compareOperator key'); | |
| } | |
| $operator = $config['compareOperator']; | |
| if (empty($config['compareTo'])) { | |
| throw new NodesValidationException('Missing compareTo key'); | |
| } | |
| $targetField = $config['compareTo']; | |
| if (!isset($model->data[$model->alias][$targetField])) { | |
| return isset($config['compareAllowEmpty']) ? $config['compareAllowEmpty'] : false; | |
| } | |
| $targetValue = $model->data[$model->alias][$targetField]; | |
| try { | |
| $sourceDate = new DateTime($sourceValue); | |
| } catch (Exception $e) { | |
| $model->invalidate($sourceField, $e->getMessage()); | |
| return false; | |
| } | |
| try { | |
| $targetDate = new DateTime($targetValue); | |
| } catch (Exception $e) { | |
| $model->invalidate($sourceField, $e->getMessage()); | |
| return false; | |
| } | |
| $operator = trim(strtolower($operator)); | |
| switch ($operator) { | |
| case 'isgreater': | |
| case '>': | |
| if ($sourceDate > $targetDate) { | |
| return true; | |
| } | |
| break; | |
| case 'isless': | |
| case '<': | |
| if ($sourceDate < $targetDate) { | |
| return true; | |
| } | |
| break; | |
| case 'greaterorequal': | |
| case '>=': | |
| if ($sourceDate >= $targetDate) { | |
| return true; | |
| } | |
| break; | |
| case 'lessorequal': | |
| case '<=': | |
| if ($sourceDate <= $targetDate) { | |
| return true; | |
| } | |
| break; | |
| case 'equalto': | |
| case '==': | |
| if ($sourceDate == $targetDate) { | |
| return true; | |
| } | |
| break; | |
| case 'notequal': | |
| case '!=': | |
| if ($sourceDate != $targetDate) { | |
| return true; | |
| } | |
| break; | |
| default: | |
| throw new NodesValidationException(__d('cake_dev', 'You must define the $operator parameter for Validation::comparison()')); | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment