diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index b09766ed02..d7488c1bdb 100644 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -168,14 +168,18 @@ class AccountController extends BaseController */ public function store() { + /** @var \FireflyIII\Database\AccountType\AccountType $accountTypes */ + $accountTypes = App::make('FireflyIII\Database\AccountType\AccountType'); - $data = Input::except('_token'); + $data = Input::except('_token'); + $type = $accountTypes->findByWhat($data['what']); + $data['user_id'] = \Auth::user()->id; + $data['account_type_id'] = $type->id; // always validate: $messages = $this->_repository->validate($data); // flash messages: - Session::flash('warnings', $messages['warnings']); Session::flash('successes', $messages['successes']); Session::flash('errors', $messages['errors']); if ($messages['errors']->count() > 0) { @@ -205,8 +209,9 @@ class AccountController extends BaseController */ public function update(Account $account) { - $data = Input::except('_token'); - $data['what'] = $this->_shortNamesByFullName[$account->accountType->type]; + $data = Input::except('_token'); + $data['account_type_id'] = $account->account_type_id; + $data['user_id'] = \Auth::user()->id; // always validate: diff --git a/app/lib/FireflyIII/Database/Account/Account.php b/app/lib/FireflyIII/Database/Account/Account.php index bee1156de5..37b5576fed 100644 --- a/app/lib/FireflyIII/Database/Account/Account.php +++ b/app/lib/FireflyIII/Database/Account/Account.php @@ -129,7 +129,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte 'amount' => $balance, 'from' => $fromAccount, 'completed' => 0, - 'what' => 'opening', 'to' => $toAccount, 'date' => $date, 'description' => 'Opening balance for new account ' . $account->name @@ -347,41 +346,14 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte */ public function validate(array $model) { - $warnings = new MessageBag; $successes = new MessageBag; - $errors = new MessageBag; - - /* - * Name validation: - */ - if (!isset($model['name'])) { - $errors->add('name', 'Name is mandatory'); - } - - if (isset($model['name']) && strlen($model['name']) == 0) { - $errors->add('name', 'Name is too short'); - } - if (isset($model['name']) && strlen($model['name']) > 100) { - $errors->add('name', 'Name is too long'); - } - $validator = \Validator::make([$model], \Account::$rules); - if ($validator->invalid()) { - $errors->merge($errors); - } + $account = new \Account($model); + $account->isValid(); + $errors = $account->getErrors(); if (isset($model['account_role']) && !in_array($model['account_role'], array_keys(\Config::get('firefly.accountRoles')))) { $errors->add('account_role', 'Invalid account role'); - } else { - $successes->add('account_role', 'OK'); } - - /* - * type validation. - */ - if (!isset($model['what'])) { - $errors->add('name', 'Internal error: need to know type of account!'); - } - /* * Opening balance and opening balance date. */ @@ -397,19 +369,14 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte } } } - - - if (!$errors->has('name')) { - $successes->add('name', 'OK'); - } - if (!$errors->has('openingBalance')) { - $successes->add('openingBalance', 'OK'); - } - if (!$errors->has('openingBalanceDate')) { - $successes->add('openingBalanceDate', 'OK'); + $fields = ['name', 'openingBalance', 'openingBalanceDate', 'active', 'account_role']; + foreach ($fields as $field) { + if (!$errors->has($field)) { + $successes->add($field, 'OK'); + } } - return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; + return ['errors' => $errors, 'successes' => $successes]; } /** diff --git a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php index b7feb1f15b..25ee14cc3d 100644 --- a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php +++ b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php @@ -159,7 +159,7 @@ class TransactionJournal implements TransactionJournalInterface, CUDInterface, C $journal->isValid(); $errors = $journal->getErrors(); - if (!isset($model['what'])) { + if (!isset($model['what']) && !isset($model['transaction_type_id'])) { $errors->add('description', 'Internal error: need to know type of transaction!'); } if (strlen($model['description']) == 0) { diff --git a/app/models/Account.php b/app/models/Account.php index ad2f43d875..98e93478b4 100644 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -12,21 +12,20 @@ use Watson\Validating\ValidatingTrait; class Account extends Eloquent { use SoftDeletingTrait, ValidatingTrait; + protected $dates = ['deleted_at', 'created_at', 'updated_at']; + protected $fillable = ['name', 'user_id', 'account_type_id', 'active']; /** * Validation rules. * * @var array */ - public static $rules - = [ - 'name' => ['required', 'between:1,100'], + protected $rules + = [ + 'name' => 'required|between:1,100', 'user_id' => 'required|exists:users,id', 'account_type_id' => 'required|exists:account_types,id', 'active' => 'required|boolean' - ]; - protected $dates = ['deleted_at', 'created_at', 'updated_at']; - protected $fillable = ['name', 'user_id', 'account_type_id', 'active']; /** * Account type.