Various updates to facilitate validating things.

This commit is contained in:
Sander Dorigo
2014-10-06 19:32:09 +02:00
parent 28aaea1aa3
commit d5bcf5497f
13 changed files with 304 additions and 315 deletions

View File

@@ -100,10 +100,73 @@ class Form
'account_id' => 'Asset account'
];
return isset($labels[$name]) ? $labels[$name] : str_replace('_',' ',ucfirst($name));
return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name));
}
/**
* Return buttons for update/validate/return.
*
* @param $type
* @param $name
*/
public static function ffOptionsList($type, $name)
{
$previousValue = \Input::old('post_submit_action');
$previousValue = is_null($previousValue) ? 'store' : $previousValue;
/*
* Store.
*/
$store = '';
switch ($type) {
case 'create':
$store = '<div class="form-group"><label for="default" class="col-sm-4 control-label">Store</label>';
$store .= '<div class="col-sm-8"><div class="radio"><label>';
$store .= \Form::radio('post_submit_action', 'store', $previousValue == 'store');
$store .= 'Store ' . $name . '</label></div></div></div>';
break;
case 'update':
$store = '<div class="form-group"><label for="default" class="col-sm-4 control-label">Store</label>';
$store .= '<div class="col-sm-8"><div class="radio"><label>';
$store .= \Form::radio('post_submit_action', 'update', $previousValue == 'store');
$store .= 'Update ' . $name . '</label></div></div></div>';
break;
default:
throw new FireflyException('Cannot create ffOptionsList for option (store) ' . $type);
break;
}
/*
* validate is always the same:
*/
$validate = '<div class="form-group"><label for="validate_only" class="col-sm-4 control-label">Validate only';
$validate .= '</label><div class="col-sm-8"><div class="radio"><label>';
$validate .= \Form::radio('post_submit_action', 'validate_only', $previousValue == 'validate_only');
$validate .= 'Only validate, do not save</label></div></div></div>';
/*
* Store & return:
*/
switch ($type) {
case 'create':
$return = '<div class="form-group"><label for="return_to_form" class="col-sm-4 control-label">';
$return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
$return .= \Form::radio('post_submit_action','create_another', $previousValue == 'create_another');
$return .= 'After storing, return here to create another one.</label></div></div></div>';
break;
case 'update':
$return = '<div class="form-group"><label for="return_to_edit" class="col-sm-4 control-label">';
$return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
$return .= \Form::radio('post_submit_action','return_to_edit', $previousValue == 'return_to_edit');
$return .= 'After updating, return here.</label></div></div></div>';
break;
default:
throw new FireflyException('Cannot create ffOptionsList for option (store+return) ' . $type);
break;
}
return $store.$validate.$return;
}
/**
* @param $type
* @param $name

View File

@@ -0,0 +1,120 @@
<?php
namespace Firefly\Helper\Controllers;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\MessageBag;
class Recurring implements RecurringInterface
{
/**
* Returns messages about the validation.
*
* @param array $data
*
* @return array
*/
public function validate(array $data)
{
$errors = new MessageBag;
$warnings = new MessageBag;
$successes = new MessageBag;
/*
* Name:
*/
if (strlen($data['name']) == 0) {
$errors->add('name', 'The name should not be this short.');
}
if (strlen($data['name']) > 250) {
$errors->add('name', 'The name should not be this long.');
}
if (! isset($data['id'])) {
$count = \Auth::user()->recurringtransactions()->whereName($data['name'])->count();
} else {
$count = \Auth::user()->recurringtransactions()->whereName($data['name'])->where('id', '!=', $data['id'])->count();
}
if ($count > 0) {
$errors->add('name', 'A recurring transaction with this name already exists.');
}
if (count($errors->get('name')) == 0) {
$successes->add('name', 'OK!');
}
/*
* Match
*/
if (count(explode(',', $data['match'])) > 10) {
$warnings->add('match', 'This many matches is pretty pointless');
}
if (strlen($data['match']) == 0) {
$errors->add('match', 'Cannot match on nothing.');
}
if (count($errors->get('match')) == 0) {
$successes->add('match', 'OK!');
}
/*
* Amount
*/
if (floatval($data['amount_max']) == 0 && floatval($data['amount_min']) == 0) {
$errors->add('amount_min', 'Amount max and min cannot both be zero.');
$errors->add('amount_max', 'Amount max and min cannot both be zero.');
}
if (floatval($data['amount_max']) < floatval($data['amount_min'])) {
$errors->add('amount_max', 'Amount max must be more than amount min.');
}
if (floatval($data['amount_min']) > floatval($data['amount_max'])) {
$errors->add('amount_max', 'Amount min must be less than amount max.');
}
if (count($errors->get('amount_min')) == 0) {
$successes->add('amount_min', 'OK!');
}
if (count($errors->get('amount_max')) == 0) {
$successes->add('amount_max', 'OK!');
}
/*
* Date
*/
try {
$date = new Carbon($data['date']);
} catch (Exception $e) {
$errors->add('date', 'The date entered was invalid');
}
if (strlen($data['date']) == 0) {
$errors->add('date', 'The date entered was invalid');
}
if (!$errors->has('date')) {
$successes->add('date', 'OK!');
}
$successes->add('active', 'OK!');
$successes->add('automatch', 'OK!');
if (intval($data['skip']) < 0) {
$errors->add('skip', 'Cannot be below zero.');
} else if (intval($data['skip']) > 31) {
$errors->add('skip', 'Cannot be above 31.');
}
if (count($errors->get('skip')) == 0) {
$successes->add('skip', 'OK!');
}
$set = \Config::get('firefly.budget_periods');
if (!in_array($data['repeat_freq'], $set)) {
$errors->add('repeat_freq', 'Invalid value.');
}
if (count($errors->get('repeat_freq')) == 0) {
$successes->add('repeat_freq', 'OK!');
}
return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Firefly\Helper\Controllers;
interface RecurringInterface {
/**
* Returns messages about the validation.
*
* @param array $data
*
* @return array
*/
public function validate(array $data);
}

View File

@@ -32,6 +32,11 @@ class HelperServiceProvider extends ServiceProvider
'Firefly\Helper\Controllers\Json'
);
$this->app->bind(
'Firefly\Helper\Controllers\RecurringInterface',
'Firefly\Helper\Controllers\Recurring'
);
$this->app->bind(
'Firefly\Helper\Controllers\SearchInterface',
'Firefly\Helper\Controllers\Search'

View File

@@ -154,7 +154,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
// unique name?
$count = $this->_user->recurringtransactions()->whereName($data['name'])->count();
if($count > 0) {
if ($count > 0) {
$messageBag->add('name', 'A recurring transaction with this name already exists.');
return $messageBag;
}
@@ -183,6 +183,8 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
if ($recurringTransaction->validate()) {
$recurringTransaction->save();
} else {
$messageBag = $recurringTransaction->errors();
}
return $messageBag;
@@ -192,10 +194,11 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
* @param \RecurringTransaction $recurringTransaction
* @param $data
*
* @return mixed|void
* @return MessageBag
*/
public function update(\RecurringTransaction $recurringTransaction, $data)
{
$messageBag = new MessageBag;
$recurringTransaction->name = $data['name'];
$recurringTransaction->match = join(' ', explode(',', $data['match']));
$recurringTransaction->amount_max = floatval($data['amount_max']);
@@ -203,9 +206,9 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
// both amounts zero:
if ($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) {
$recurringTransaction->errors()->add('amount_max', 'Amount max and min cannot both be zero.');
$messageBag->add('amount_max', 'Amount max and min cannot both be zero.');
return $recurringTransaction;
return $messageBag;
}
$recurringTransaction->date = new Carbon($data['date']);
$recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0;
@@ -215,9 +218,11 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
if ($recurringTransaction->validate()) {
$recurringTransaction->save();
} else {
$messageBag = $recurringTransaction->errors();
}
return $recurringTransaction;
return $messageBag;
}

View File

@@ -50,7 +50,7 @@ interface RecurringTransactionRepositoryInterface
* @param \RecurringTransaction $recurringTransaction
* @param $data
*
* @return mixed
* @return MessageBag
*/
public function update(\RecurringTransaction $recurringTransaction, $data);