More cleanup [skip ci]

This commit is contained in:
James Cole 2015-02-01 09:08:39 +01:00
parent c2d2eb53e8
commit 6873336aca
4 changed files with 24 additions and 53 deletions

View File

@ -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:

View File

@ -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];
}
/**

View File

@ -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) {

View File

@ -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.