From 1fc861026a4054e8b5b2e91142765cb7cbacdd2a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 29 Jan 2012 13:19:47 -0600 Subject: [PATCH] added back support for specifying column in validator unique rule. id is now a third parameter option. --- laravel/validator.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/laravel/validator.php b/laravel/validator.php index f7912065..22f3a406 100644 --- a/laravel/validator.php +++ b/laravel/validator.php @@ -417,11 +417,19 @@ protected function validate_unique($attribute, $value, $parameters) { if (is_null($this->db)) $this->db = Database::connection(); + // We allow the table column to be specified just in case the column does + // not have the same name as the attribute. It must be in the second + // parameter position, right after the databse table name. + if (isset($parameters[1])) $attribute = $parameters[1]; + $query = $this->db->table($parameters[0])->where($attribute, '=', $value); - if (isset($parameters[1])) + // We also allow an ID to be specified that will not be included in the + // uniqueness check. This makes updating columns easier since it is + // fine for the given ID to exist in the table. + if (isset($parameters[2])) { - $query->where('id', '<>', $parameters[1]); + $query->where('id', '<>', $parameters[2]); } return $query->count() == 0;