public function convert(Jam_Validated $model, $value, $is_loaded) { if (!$value and !$is_loaded and !$model->changed($this->name)) { return Request::$client_ip; } return $value; }
public function validate(Jam_Validated $model, $attribute, $value) { $confirmation = $this->confirmation ? $this->confirmation : $attribute . '_confirmation'; if ($model->{$confirmation} and $value !== $model->{$confirmation}) { $model->errors()->add($attribute, 'confirmed', array(':confirmation' => $confirmation)); } }
public function validate(Jam_Validated $model, $attribute, $value) { $promo_code = $this->valid_promo_code($value); if (!$promo_code) { $model->errors()->add('promo_code_text', 'invalid'); } elseif ($promo_code->is_expired()) { $model->errors()->add('promo_code_text', 'expired'); } }
public function validate(Jam_Validated $model, $attribute, $value) { if ($this->in !== NULL and !in_array($value, $this->in)) { $model->errors()->add($attribute, 'choice_in', array(':in' => join(', ', $this->in))); } if ($this->not_in !== NULL and !!in_array($value, $this->not_in)) { $model->errors()->add($attribute, 'choice_not_in', array(':not_in' => join(', ', $this->not_in))); } }
public function is_processable_attribute(Jam_Validated $model, $attribute) { if ($this->validate_empty) { return TRUE; } if ($field = $model->meta()->field($attribute)) { return !$field->is_empty($model->{$attribute}); } return $model->{$attribute} !== NULL; }
public function validate(Jam_Validated $model, $attribute, $value) { if (!$value instanceof Jam_Range) { throw new Kohana_Exception('Range validation rule can only be applied to range'); } if (!is_numeric($value->min()) or !is_numeric($value->max())) { $model->errors()->add($attribute, 'range_numeric'); return; } $min = min($value->min(), $value->max()); $max = max($value->min(), $value->max()); if ($this->minimum !== NULL and $min <= $this->minimum) { $model->errors()->add($attribute, 'range_minimum', array(':minimum' => $this->minimum)); } if ($this->minimum_or_equal_to !== NULL and $min < $this->minimum_or_equal_to) { $model->errors()->add($attribute, 'range_minimum_or_equal_to', array(':minimum_or_equal_to' => $this->minimum_or_equal_to)); } if ($this->maximum !== NULL and $max >= $this->maximum) { $model->errors()->add($attribute, 'range_maximum', array(':maximum' => $this->maximum)); } if ($this->maximum_or_equal_to !== NULL and $max > $this->maximum_or_equal_to) { $model->errors()->add($attribute, 'range_maximum_or_equal_to', array(':maximum_or_equal_to' => $this->maximum_or_equal_to)); } if ($this->between !== NULL and !($min >= $this->between[0] and $max <= $this->between[1])) { $model->errors()->add($attribute, 'range_between', array(':minimum' => $this->between[0], ':maximum' => $this->between[1])); } if ($this->consecutive !== NULL and $value->min() > $value->max()) { $model->errors()->add($attribute, 'range_consecutive'); } }
public function validate(Jam_Validated $model, $attribute, $value) { $length = mb_strlen($value); if ($this->minimum !== NULL and !($length >= $this->minimum)) { $model->errors()->add($attribute, 'length_minimum', array(':minimum' => $this->minimum)); } if ($this->maximum !== NULL and !($length <= $this->maximum)) { $model->errors()->add($attribute, 'length_maximum', array(':maximum' => $this->maximum)); } if ($this->between !== NULL and !($length >= $this->between[0] and $length <= $this->between[1])) { $model->errors()->add($attribute, 'length_between', array(':minimum' => $this->between[0], ':maximum' => $this->between[1])); } if ($this->is !== NULL and !($length == $this->is)) { $model->errors()->add($attribute, 'length_is', array('is' => $this->is)); } }
public function validate(Jam_Validated $model, $attribute, $value) { $count = count($value); $params = (array) $this; if ($this->minimum !== NULL and !($count >= $this->minimum)) { $model->errors()->add($attribute, 'count_minimum', array(':minimum' => $this->minimum)); } if ($this->maximum !== NULL and !($count <= $this->maximum)) { $model->errors()->add($attribute, 'count_maximum', array(':maximum' => $this->maximum)); } if ($this->within !== NULL and !($count >= $this->within[0] and $count <= $this->within[1])) { $model->errors()->add($attribute, 'count_within', array(':minimum' => $this->within[0], ':maximum' => $this->within[1])); } if ($this->is !== NULL and !($count == $this->is)) { $model->errors()->add($attribute, 'count_is', array(':is' => $this->is)); } }
/** * Get the primary key from whatever value you have * * @param string $model_name The name of the model * @param string|integer|Jam_Validated|array $value the value or a container of the value * @return string|integer|NULL NULL when no value is provided or could be extracted. */ public static function primary_key($model_name, $value) { if (!$value) { return NULL; } if ($value instanceof Jam_Validated) { return $value->id(); } if (is_integer($value) or is_numeric($value)) { return (int) $value; } if (is_string($value)) { return $value; } if (is_array($value)) { return Arr::get($value, Jam::meta($model_name)->primary_key()); } }
public function validate(Jam_Validated $model, $attribute, $value) { // According to the SQL standard NULL is not checked by the unique constraint // We also skip this test if the value is the same as the default value if ($value !== $model->meta()->defaults($attribute)) { // Build query $query = Jam::all($model)->where($attribute, '=', $value); if ($this->scope) { foreach ((array) $this->scope as $scope_attribute) { $query->where($scope_attribute, '=', $model->{$scope_attribute}); } } $query->limit(1); if ($query->count() and (!$model->loaded() or $query[0]->id() !== $model->id())) { // Add error if duplicate found $model->errors()->add($attribute, 'unique'); } } }
/** * Get the belonging model for this association using the foreign key, * if the data was changed, use the key from the changed data. * Assign inverse_of * * @param Jam_Validated $model * @param mixed $value changed data * @param boolean $is_changed * @return Jam_Model */ public function get(Jam_Validated $model, $value, $is_changed) { if ($is_changed) { if ($value instanceof Jam_Validated or !$value) { return $value; } $key = Jam_Association::primary_key($this->foreign_model, $value); if ($key) { $item = Jam::find($this->foreign_model, $key); } elseif (is_array($value)) { $item = Jam::build($this->foreign_model); } else { $item = NULL; } if ($item and is_array($value)) { $item->set($value); } } else { $item = Jam::all($this->foreign_model)->join_table($this->branches_table)->on($this->ansestor_key, '=', ':primary_key')->on($this->depth_key, '=', DB::expr(':depth', array(':depth' => 1)))->end()->where($this->branches_table . '.' . $this->descendant_key, '=', $model->id())->first(); } return $this->set($model, $item, $is_changed); }
public function validate(Jam_Validated $model, $attribute, $value) { if ($this->regex !== NULL and !preg_match($this->regex, $value)) { $model->errors()->add($attribute, 'format_regex', array(':regex' => $this->regex)); } if ($this->filter !== NULL and !(filter_var($value, $this->filter, $this->flag) !== FALSE)) { $model->errors()->add($attribute, 'format_filter', array(':filter' => $this->filter)); } if ($this->ip === TRUE and !Valid::ip($value)) { $model->errors()->add($attribute, 'format_ip'); } if ($this->url === TRUE and !Valid::url($value)) { $model->errors()->add($attribute, 'format_url'); } if ($this->email === TRUE and !Valid::email($value)) { $model->errors()->add($attribute, 'format_email'); } if ($this->credit_card === TRUE and !Valid::credit_card($value)) { $model->errors()->add($attribute, 'format_credit_card'); } if ($this->date === TRUE and strtotime($value) === FALSE) { $model->errors()->add($attribute, 'format_date'); } }
/** * Sets a model to its original state, as if freshly instantiated * * @return Jam_Model */ public function clear() { $this->_loaded = $this->_saved = FALSE; parent::clear(); return $this; }
public function validate(Jam_Validated $model, $attribute, $value) { if (!is_numeric($value)) { $model->errors()->add($attribute, 'numeric'); } if ($this->only_integer !== NULL and !(filter_var($value, FILTER_VALIDATE_INT) !== FALSE)) { $model->errors()->add($attribute, 'numeric_only_integer'); } if ($this->greater_than_or_equal_to !== NULL and !($value >= $this->greater_than_or_equal_to)) { $model->errors()->add($attribute, 'numeric_greater_than_or_equal_to', array(':greater_than_or_equal_to' => $this->greater_than_or_equal_to)); } if ($this->greater_than !== NULL and !($value > $this->greater_than)) { $model->errors()->add($attribute, 'numeric_greater_than', array(':greater_than' => $this->greater_than)); } if ($this->equal_to !== NULL and !($value == $this->equal_to)) { $model->errors()->add($attribute, 'numeric_equal_to', array(':equal_to' => $this->equal_to)); } if ($this->less_than !== NULL and !($value < $this->less_than)) { $model->errors()->add($attribute, 'numeric_less_than', array(':less_than' => $this->less_than)); } if ($this->less_than_or_equal_to !== NULL and !($value <= $this->less_than_or_equal_to)) { $model->errors()->add($attribute, 'numeric_less_than_or_equal_to', array(':less_than_or_equal_to' => $this->less_than_or_equal_to)); } if ($this->between !== NULL and !($value >= $this->between[0] and $value <= $this->between[1])) { $model->errors()->add($attribute, 'numeric_between', array(':minimum' => $this->between[0], ':maximum' => $this->between[1])); } if ($this->odd === TRUE and !($value % 2 == 0)) { $model->errors()->add($attribute, 'numeric_odd', array(':odd' => $this->odd)); } if ($this->even === TRUE and !($value % 2 == 1)) { $model->errors()->add($attribute, 'numeric_even', array(':even' => $this->even)); } }
public function validate(Jam_Validated $model, $attribute, $value) { if (Jam_Validator_Rule_Present::is_empty_value($value, $this->allow_zero) or Jam_Validator_Rule_Present::is_empty_string($value) or Jam_Validator_Rule_Present::is_empty_countable($value) or Jam_Validator_Rule_Present::is_empty_upload_file($value)) { $model->errors()->add($attribute, 'present'); } }
public function validate(Jam_Validated $model, $attribute, $value) { if ($value and !$value->is_empty() and $value->source()) { if ($error = $value->source()->error()) { $model->errors()->add($attribute, 'uploaded_native', array(':message' => $error)); } elseif (!is_file($value->file())) { $model->errors()->add($attribute, 'uploaded_is_file'); } if ($this->only and !in_array(strtolower(pathinfo($value->filename(), PATHINFO_EXTENSION)), $this->valid_extensions())) { $model->errors()->add($attribute, 'uploaded_extension', array(':extension' => join(', ', $this->valid_extensions()))); } if ($this->minimum_size or $this->maximum_size or $this->exact_size) { $size = @filesize($value->file()); if ($this->minimum_size and $minimum_size = Num::bytes($this->minimum_size) and (int) $size < (int) $minimum_size) { $model->errors()->add($attribute, 'uploaded_minimum_size', array(':minimum_size' => $this->minimum_size)); } if ($this->maximum_size and $maximum_size = Num::bytes($this->maximum_size) and (int) $size > (int) $maximum_size) { $model->errors()->add($attribute, 'uploaded_maximum_size', array(':maximum_size' => $this->maximum_size)); } if ($this->exact_size and $exact_size = Num::bytes($this->exact_size) and (int) $size !== (int) $exact_size) { $model->errors()->add($attribute, 'uploaded_exact_size', array(':exact_size' => $this->exact_size)); } } if ($this->minimum_width or $this->minimum_height or $this->maximum_width or $this->maximum_height or $this->exact_width or $this->exact_height) { $dims = @getimagesize($value->file()); if ($dims) { list($width, $height) = $dims; if ($this->exact_width and (int) $width !== (int) $this->exact_width) { $model->errors()->add($attribute, 'uploaded_exact_width', array(':exact_width' => $this->exact_width)); } if ($this->exact_height and (int) $height !== (int) $this->exact_height) { $model->errors()->add($attribute, 'uploaded_exact_height', array(':exact_height' => $this->exact_height)); } if ($this->minimum_width and (int) $width < (int) $this->minimum_width) { $model->errors()->add($attribute, 'uploaded_minimum_width', array(':minimum_width' => $this->minimum_width)); } if ($this->minimum_height and (int) $height < (int) $this->minimum_height) { $model->errors()->add($attribute, 'uploaded_minimum_height', array(':minimum_height' => $this->minimum_height)); } if ($this->maximum_width and (int) $width > (int) $this->maximum_width) { $model->errors()->add($attribute, 'uploaded_maximum_width', array(':maximum_width' => $this->maximum_width)); } if ($this->maximum_height and (int) $height > (int) $this->maximum_height) { $model->errors()->add($attribute, 'uploaded_maximum_height', array(':maximum_height' => $this->maximum_height)); } } } } }
private function _add_messages_all(Jam_Validated $model, array &$messages) { foreach ($model->errors() as $attribute_name => $errors) { if ($model->meta()->association($attribute_name) instanceof Jam_Association_Collection) { foreach ($model->{$attribute_name} as $i => $item) { if (!$item->is_valid()) { $this->_add_messages_all($item, $messages); } } } elseif ($model->meta()->association($attribute_name) and $model->{$attribute_name}) { $this->_add_messages_all($model->{$attribute_name}, $messages); } else { foreach ($errors as $error => $params) { $model_name = UTF8::ucfirst(Inflector::humanize($model->meta()->model())); $messages[] = $model_name . ': ' . Jam_Errors::message($model->meta()->errors_filename(), $attribute_name, $error, Arr::merge($params, array(':model' => $model->meta()->model(), ':attribute' => Jam_Errors::attribute_label($model->meta(), $attribute_name)))); } } } return $messages; }
public function model_before_check(Jam_Validated $model, $value, $changed) { if (!$model->loaded() and !isset($changed[$this->name]) and Jam_Association_Creator::current()) { $model->{$this->name} = Jam::find($this->foreign_model, Jam_Association_Creator::current()); } }
public function set(Jam_Validated $model, $value, $is_changed) { if ($value instanceof Jam_Model) { if ($this->is_polymorphic()) { $value->{$this->polymorphic_key} = $model->meta()->model(); } if ($model->loaded()) { $value->{$this->foreign_key} = $model->id(); } if ($this->as) { $value->retrieved($this->as, $model); } if ($this->inverse_of) { $value->retrieved($this->inverse_of, $model); } } return $value; }