. */ declare(strict_types=1); namespace FireflyIII\Http\Requests; use FireflyIII\Models\Account; use FireflyIII\Rules\UniqueIban; /** * Class AccountFormRequest. */ class AccountFormRequest extends Request { /** * @return bool */ public function authorize() { // Only allow logged in users return auth()->check(); } /** * @return array */ public function getAccountData(): array { return [ 'name' => $this->string('name'), 'active' => $this->boolean('active'), 'accountType' => $this->string('what'), 'account_type_id' => 0, 'currency_id' => $this->integer('currency_id'), 'virtualBalance' => $this->string('virtualBalance'), 'iban' => $this->string('iban'), 'BIC' => $this->string('BIC'), 'accountNumber' => $this->string('accountNumber'), 'accountRole' => $this->string('accountRole'), 'openingBalance' => $this->string('openingBalance'), 'openingBalanceDate' => $this->date('openingBalanceDate'), 'ccType' => $this->string('ccType'), 'ccMonthlyPaymentDate' => $this->string('ccMonthlyPaymentDate'), 'notes' => $this->string('notes'), ]; } /** * @return array */ public function rules() { $accountRoles = join(',', config('firefly.accountRoles')); $types = join(',', array_keys(config('firefly.subTitlesByIdentifier'))); $ccPaymentTypes = join(',', array_keys(config('firefly.ccTypes'))); $rules = [ 'name' => 'required|min:1|uniqueAccountForUser', 'openingBalance' => 'numeric|required_with:openingBalanceDate|nullable', 'openingBalanceDate' => 'date|required_with:openingBalance|nullable', 'iban' => ['iban', 'nullable', new UniqueIban(null, $this->string('what'))], 'BIC' => 'bic|nullable', 'virtualBalance' => 'numeric|nullable', 'currency_id' => 'exists:transaction_currencies,id', 'accountNumber' => 'between:1,255|uniqueAccountNumberForUser|nullable', 'accountRole' => 'in:' . $accountRoles, 'active' => 'boolean', 'ccType' => 'in:' . $ccPaymentTypes, 'ccMonthlyPaymentDate' => 'date', 'amount_currency_id_openingBalance' => 'exists:transaction_currencies,id', 'amount_currency_id_virtualBalance' => 'exists:transaction_currencies,id', 'what' => 'in:' . $types, ]; /** @var Account $account */ $account = $this->route()->parameter('account'); if (!is_null($account)) { // add rules: $rules['id'] = 'belongsToUser:accounts'; $rules['name'] = 'required|min:1|uniqueAccountForUser:' . intval($this->get('id')); $rules['iban'] = ['iban', 'nullable', new UniqueIban($account, $account->accountType->type)]; } return $rules; } }