Fixed unique piggy check.

This commit is contained in:
James Cole 2015-03-27 20:20:52 +01:00
parent 2694297466
commit b9000519e4
2 changed files with 31 additions and 7 deletions

View File

@ -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';

View File

@ -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;
}
}