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() public function rules()
{ {
$nameRule = 'required|between:1,255|uniqueForUser:piggy_banks,name'; $nameRule = 'required|between:1,255|uniquePiggyBankForUser:piggy_banks,name';
$targetDateRule = 'date'; $targetDateRule = 'date';
if (intval(Input::get('id'))) { if (intval(Input::get('id'))) {
$nameRule = 'required|between:1,255'; $nameRule = 'required|between:1,255';

View File

@ -83,16 +83,16 @@ class FireflyValidator extends Validator
$validTypes = array_keys(Config::get('firefly.subTitlesByIdentifier')); $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: // some fallback:
if(is_null($type)) { if (is_null($type)) {
$type = in_array(Input::get('what'),$validTypes) ? Input::get('what') : null; $type = in_array(Input::get('what'), $validTypes) ? Input::get('what') : null;
} }
// still null? // still null?
if(is_null($type)) { if (is_null($type)) {
// find by other field: // find by other field:
$type = isset($this->data['account_type_id']) ? $this->data['account_type_id'] : 0; $type = isset($this->data['account_type_id']) ? $this->data['account_type_id'] : 0;
$dbType = AccountType::find($type); $dbType = AccountType::find($type);
} else { } else {
$longType = Config::get('firefly.accountTypeByIdentifier.' . $type); $longType = Config::get('firefly.accountTypeByIdentifier.' . $type);
$dbType = AccountType::whereType($longType)->first(); $dbType = AccountType::whereType($longType)->first();
@ -142,5 +142,29 @@ class FireflyValidator extends Validator
return false; 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;
}
} }