Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

validation for unique slugs per content type. Closes #197

  • Loading branch information...
commit 301f99b68d88c6bc409ab63a419f8b0ca16e26b2 1 parent bcc09a1
@fahad19 authored
Showing with 47 additions and 2 deletions.
  1. +47 −2 models/node.php
View
49 models/node.php
@@ -67,10 +67,10 @@ class Node extends AppModel {
'message' => 'This field cannot be left blank.',
),
'slug' => array(
- /*'isUniquePerType' => array(
+ 'isUniquePerType' => array(
'rule' => 'isUniquePerType',
'message' => 'This slug has already been taken.',
- ),*/
+ ),
'minLength' => array(
'rule' => array('minLength', 1),
'message' => 'Slug cannot be empty.',
@@ -195,5 +195,50 @@ public function __cacheTerms() {
));
}
}
+/**
+ * Returns false if any fields passed match any (by default, all if $or = false) of their matching values.
+ *
+ * @param array $fields Field/value pairs to search (if no values specified, they are pulled from $this->data)
+ * @param boolean $or If false, all fields specified must match in order for a false return value
+ * @return boolean False if any records matching any fields are found
+ * @access public
+ */
+ function isUniquePerType($fields, $or = true) {
+ if (!is_array($fields)) {
+ $fields = func_get_args();
+ if (is_bool($fields[count($fields) - 1])) {
+ $or = $fields[count($fields) - 1];
+ unset($fields[count($fields) - 1]);
+ }
+ }
+
+ foreach ($fields as $field => $value) {
+ if (is_numeric($field)) {
+ unset($fields[$field]);
+
+ $field = $value;
+ if (isset($this->data[$this->alias][$field])) {
+ $value = $this->data[$this->alias][$field];
+ } else {
+ $value = null;
+ }
+ }
+
+ if (strpos($field, '.') === false) {
+ unset($fields[$field]);
+ $fields[$this->alias . '.' . $field] = $value;
+ }
+ }
+ if ($or) {
+ $fields = array('or' => $fields);
+ }
+ if (!empty($this->id)) {
+ $fields[$this->alias . '.' . $this->primaryKey . ' !='] = $this->id;
+ }
+ if (!empty($this->type)) {
+ $fields[$this->alias . '.type'] = $this->type;
+ }
+ return ($this->find('count', array('conditions' => $fields, 'recursive' => -1)) == 0);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.