From b9000519e46c46d63e37352e9283209168363318 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 27 Mar 2015 20:20:52 +0100 Subject: [PATCH] Fixed unique piggy check. --- app/Http/Requests/PiggyBankFormRequest.php | 2 +- app/Validation/FireflyValidator.php | 36 ++++++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/Http/Requests/PiggyBankFormRequest.php b/app/Http/Requests/PiggyBankFormRequest.php index b794ba6871..768eda19a4 100644 --- a/app/Http/Requests/PiggyBankFormRequest.php +++ b/app/Http/Requests/PiggyBankFormRequest.php @@ -30,7 +30,7 @@ class PiggyBankFormRequest extends Request public function rules() { - $nameRule = 'required|between:1,255|uniqueForUser:piggy_banks,name'; + $nameRule = 'required|between:1,255|uniquePiggyBankForUser:piggy_banks,name'; $targetDateRule = 'date'; if (intval(Input::get('id'))) { $nameRule = 'required|between:1,255'; diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index c2f4087517..d4b4771e90 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -83,16 +83,16 @@ class FireflyValidator extends Validator $validTypes = array_keys(Config::get('firefly.subTitlesByIdentifier')); - $type = isset($this->data['what']) && in_array($this->data['what'],$validTypes) ? $this->data['what'] : null; + $type = isset($this->data['what']) && in_array($this->data['what'], $validTypes) ? $this->data['what'] : null; // some fallback: - if(is_null($type)) { - $type = in_array(Input::get('what'),$validTypes) ? Input::get('what') : null; + if (is_null($type)) { + $type = in_array(Input::get('what'), $validTypes) ? Input::get('what') : null; } // still null? - if(is_null($type)) { + if (is_null($type)) { // find by other field: - $type = isset($this->data['account_type_id']) ? $this->data['account_type_id'] : 0; - $dbType = AccountType::find($type); + $type = isset($this->data['account_type_id']) ? $this->data['account_type_id'] : 0; + $dbType = AccountType::find($type); } else { $longType = Config::get('firefly.accountTypeByIdentifier.' . $type); $dbType = AccountType::whereType($longType)->first(); @@ -142,5 +142,29 @@ class FireflyValidator extends Validator return false; } + + /** + * @param $attribute + * @param $value + * @param $parameters + * + * @return bool + */ + public function validateUniquePiggyBankForUser($attribute, $value, $parameters) + { + $query = DB::table($parameters[0])->where('piggy_banks.'.$parameters[1], $value); + $query->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id'); + $query->where('accounts.user_id', Auth::user()->id); + if (isset($paramers[2])) { + $query->where('piggy_banks.id', '!=', $parameters[2]); + } + $count = $query->count(); + if ($count == 0) { + return true; + } + + return false; + + } }