/** * @inheritdoc */ public function rules() { $ownValidators = array('title' => array(array('required'), array('sanitize'), array('type', array('type' => 'string')), array('length', array('min' => 5, 'max' => 50))), 'email' => array(array('required'), array('sanitize'), array('type', array('type' => 'string')), array('length', array('min' => 5, 'max' => 50)), array('email')), 'content' => array(array('required'), array('sanitize', array('allowedTags' => '<a>')), array('type', array('type' => 'string')), array('length', array('min' => 10, 'max' => pow(2, 16))))); $parentValidators = parent::rules(); $validators = array_merge($parentValidators, $ownValidators); return $validators; }
/** * @inheritdoc */ public function rules() { $ownValidators = array('name' => array(array('required'), array('sanitize'), array('type', array('type' => 'string')), array('length', array('min' => 2, 'max' => 50))), 'email' => array(array('required'), array('sanitize'), array('type', array('type' => 'string')), array('length', array('min' => 5, 'max' => 50)), array('email', array('allowEmpty' => true))), 'message' => array(array('required'), array('sanitize', array('allowedTags' => '<a>')), array('type', array('type' => 'string')), array('length', array('min' => 5, 'max' => pow(2, 16)))), 'postId' => array(array('required'), array('sanitize'), array('type', array('type' => 'integer')), array('value', array('min' => 0, 'max' => 4294967295)), array('referencedField', array('modelClass' => 'Post', 'attribute' => 'id')))); $parentValidators = parent::rules(); $validators = array_merge($parentValidators, $ownValidators); return $validators; }
/** * Utility method to get an instance of a model given an identifier in the query string * @param $modelClassName * @param string $queryParameter * @return \GGS\Components\Model */ protected static function getModelByRequest($modelClassName, $queryParameter = 'id') { $id = WebApplication::$request->getQueryStringParameter($queryParameter); if (!isset($id)) { // whoopsie, can't live without id static::exitWithException("Invalid Request: Missing id.", 400); } if (!is_numeric($id) || $id < 1) { // so far we are only using positive integer primary keys so it doesn't make sense to search for a model // with negative key static::exitWithException("Invalid Request: id must be positive integer", 400); } // convert id to a proper integer, no string values; $id = intval($id); // get the qualified model class name with namespace $modelClassName = \GGS\Components\Model::getQualifiedModelClassName($modelClassName); // get the model $model = $modelClassName::getByPk($id); if (!isset($model)) { // model not found? how come? I know, a bad request. static::exitWithException("No records found for: {$id}", 404); } // found it. return $model; }
/** * @inheritdoc */ public function rules() { // using default validator to set default values generated using functions $ownValidators = array('key' => array(array('default', array('value' => static::generateRandomKey())), array('required'), array('type', array('type' => 'string')), array('length', array('exact' => static::KEY_LENGTH)), array('unique')), 'action' => array(array('required'), array('type', array('type' => 'string')), array('length', array('min' => 8, 'max' => 30))), 'userAgent' => array(array('default', array('value' => static::getUserAgent())), array('required'), array('type', array('type' => 'string')), array('length', array('min' => 2, 'max' => 255))), 'userIP' => array(array('default', array('value' => static::getUserIP())), array('required'), array('type', array('type' => 'integer'))), 'expirationTime' => array(array('default', array('value' => static::getExpirationTime())), array('required'), array('type', array('type' => 'integer')))); $parentValidators = parent::rules(); $validators = array_merge($parentValidators, $ownValidators); return $validators; }
public function validate(\GGS\Components\Model &$object, $attribute) { if (empty($object->{$attribute})) { return parent::validate($object, $attribute); } $qualifiedModelClassName = \GGS\Components\Model::getQualifiedModelClassName($this->modelClass); $exists = $qualifiedModelClassName::exists(array($this->attribute => $object->{$attribute})); if (!$exists) { $this->setError($object, $attribute, 'referenced record can not be found'); } return $exists; }
/** * Resolve error for provided input. A small wrapper around model's functions * @param \GGS\Components\Model $model * @param $attribute * @return mixed */ public static function resolveInputErrorMessage(\GGS\Components\Model $model, $attribute) { if ($model->hasError($attribute)) { return $model->getError($attribute); } }
protected function setError(\GGS\Components\Model &$object, $attribute, $message) { $label = $object->resolveAttributeLabel($attribute); $object->setError($attribute, $label . ' ' . $message); }