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) { // 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'); } } }
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; }
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; }