Added the should_validate template variable to the transaction forms to prevent initial errors with newly-created empty forms in the accounting application.

This commit is contained in:
依瑪貓 2020-08-06 15:48:26 +08:00
parent 9a9f715349
commit ede94eb68e
6 changed files with 31 additions and 28 deletions

View File

@ -57,8 +57,8 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:") }}</label>
</div>
<div class="col-sm-10">
<input id="txn-date" class="form-control {% if item.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date.value }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ item.date.errors.0|default:"" }}</div>
<input id="txn-date" class="form-control {% if should_validate and item.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date.value }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{% if should_validate %}{{ item.date.errors.0|default:"" }}{% endif %}</div>
</div>
</div>
@ -92,8 +92,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:") }}</label>
</div>
<div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if item.notes.errors %} is-invalid {% endif %}" name="notes">{{ item.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ item.notes.errors.0|default:"" }}</div>
<textarea id="txn-note" class="form-control {% if should_validate and item.notes.errors %} is-invalid {% endif %}" name="notes">{{ item.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ item.notes.errors.0|default:"" }}{% endif %}</div>
</div>
</div>

View File

@ -27,7 +27,7 @@ First written: 2020/8/5
<input type="hidden" name="{{ record_type }}-{{ no }}-id" value="{{ record.id.value }}" />
{% endif %}
<input id="{{ record_type }}-{{ no }}-ord" class="{{ record_type }}-ord" type="hidden" name="{{ record_type }}-{{ no }}-ord" value="{{ order }}" />
<select id="{{ record_type }}-{{ no }}-account" class="form-control record-account {{ record_type }}-account {% if record.account.errors %} is-invalid {% endif %}" name="{{ record_type }}-{{ no }}-account">
<select id="{{ record_type }}-{{ no }}-account" class="form-control record-account {{ record_type }}-account {% if should_validate and record.account.errors %} is-invalid {% endif %}" name="{{ record_type }}-{{ no }}-account">
{% if record.account is not None %}
<option value="{{ record.account.value|default:"" }}" selected="selected">{{ record.account.value|default:"" }} {{ record.account_title|default:"" }}</option>
{% else %}
@ -35,17 +35,17 @@ First written: 2020/8/5
{% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</select>
<div id="{{ record_type }}-{{ no }}-account-error" class="invalid-feedback">{{ record.account.errors.0|default:"" }}</div>
<div id="{{ record_type }}-{{ no }}-account-error" class="invalid-feedback">{% if should_validate %}{{ record.account.errors.0|default:"" }}{% endif %}</div>
</div>
<div class="col-lg-6">
<div class="row">
<div class="col-sm-8">
<input id="{{ record_type }}-{{ no }}-summary" class="form-control record-summary {% if record.summary.errors %} is-invalid {% endif %}" type="text" name="{{ record_type }}-{{ no }}-summary" value="{{ record.summary.value|default:"" }}" maxlength="128" data-toggle="modal" data-target="#summary-modal" />
<div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{{ record.summary.errors.0|default:"" }}</div>
<input id="{{ record_type }}-{{ no }}-summary" class="form-control record-summary {% if should_validate and record.summary.errors %} is-invalid {% endif %}" type="text" name="{{ record_type }}-{{ no }}-summary" value="{{ record.summary.value|default:"" }}" maxlength="128" data-toggle="modal" data-target="#summary-modal" />
<div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{% if should_validate %}{{ record.summary.errors.0|default:"" }}{% endif %}</div>
</div>
<div class="col-sm-4">
<input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if record.amount.errors %} is-invalid {% endif %}" type="number" min="1" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" />
<div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{{ record.amount.errors.0|default:"" }}</div>
<input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if should_validate and record.amount.errors %} is-invalid {% endif %}" type="number" min="1" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" />
<div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{% if should_validate %}{{ record.amount.errors.0|default:"" }}{% endif %}</div>
</div>
</div>
</div>

View File

@ -28,7 +28,7 @@ First written: 2020/8/5
<input type="hidden" name="{{ record_type }}-{{ no }}-id" value="{{ record.id.value }}" />
{% endif %}
<input id="{{ record_type }}-{{ no }}-ord" class="{{ record_type }}-ord" type="hidden" name="{{ record_type }}-{{ no }}-ord" value="{{ order }}" />
<select id="{{ record_type }}-{{ no }}-account" class="form-control record-account {{ record_type }}-account {% if record.account.errors %} is-invalid {% endif %}" name="{{ record_type }}-{{ no }}-account">
<select id="{{ record_type }}-{{ no }}-account" class="form-control record-account {{ record_type }}-account {% if should_validate and record.account.errors %} is-invalid {% endif %}" name="{{ record_type }}-{{ no }}-account">
{% if record.account is not None %}
<option value="{{ record.account.value|default:"" }}" selected="selected">{{ record.account.value|default:"" }} {{ record.account_title|default:"" }}</option>
{% else %}
@ -36,17 +36,17 @@ First written: 2020/8/5
{% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</select>
<div id="{{ record_type }}-{{ no }}-account-error" class="invalid-feedback">{{ record.account.errors.0|default:"" }}</div>
<div id="{{ record_type }}-{{ no }}-account-error" class="invalid-feedback">{% if should_validate %}{{ record.account.errors.0|default:"" }}{% endif %}</div>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<input id="{{ record_type }}-{{ no }}-summary" class="form-control record-summary {% if record.summary.errors %} is-invalid {% endif %}" type="text" name="{{ record_type }}-{{ no }}-summary" value="{{ record.summary.value|default:"" }}" maxlength="128" data-toggle="modal" data-target="#summary-modal" />
<div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{{ record.summary.errors.0|default:"" }}</div>
<input id="{{ record_type }}-{{ no }}-summary" class="form-control record-summary {% if should_validate and record.summary.errors %} is-invalid {% endif %}" type="text" name="{{ record_type }}-{{ no }}-summary" value="{{ record.summary.value|default:"" }}" maxlength="128" data-toggle="modal" data-target="#summary-modal" />
<div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{% if should_validate %}{{ record.summary.errors.0|default:"" }}{% endif %}</div>
</div>
<div class="col-lg-4">
<input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if record.amount.errors %} is-invalid {% endif %}" type="number" min="1" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" />
<div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{{ record.amount.errors.0|default:"" }}</div>
<input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if should_validate and record.amount.errors %} is-invalid {% endif %}" type="number" min="1" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" />
<div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{% if should_validate %}{{ record.amount.errors.0|default:"" }}{% endif %}</div>
</div>
</div>
</div>

View File

@ -57,8 +57,8 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:") }}</label>
</div>
<div class="col-sm-10">
<input id="txn-date" class="form-control {% if item.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date.value }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ item.date.errors.0|default:"" }}</div>
<input id="txn-date" class="form-control {% if should_validate and item.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date.value }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{% if should_validate %}{{ item.date.errors.0|default:"" }}{% endif %}</div>
</div>
</div>
@ -92,8 +92,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:") }}</label>
</div>
<div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if item.notes.errors %} is-invalid {% endif %}" name="notes">{{ item.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ item.notes.errors.0|default:"" }}</div>
<textarea id="txn-note" class="form-control {% if should_validate and item.notes.errors %} is-invalid {% endif %}" name="notes">{{ item.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ item.notes.errors.0|default:"" }}{% endif %}</div>
</div>
</div>

View File

@ -57,8 +57,8 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:") }}</label>
</div>
<div class="col-sm-10">
<input id="txn-date" class="form-control {% if item.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date.value }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ item.date.errors.0|default:"" }}</div>
<input id="txn-date" class="form-control {% if should_validate and item.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date.value }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{% if should_validate %}{{ item.date.errors.0|default:"" }}{% endif %}</div>
</div>
</div>
@ -80,11 +80,11 @@ First written: 2020/7/23
</button>
</li>
<li class="list-group-item">
<div id="debit-total-row" class="d-flex justify-content-between align-items-center form-control {% if item.balance_error %} is-invalid {% endif %} balance-row">
<div id="debit-total-row" class="d-flex justify-content-between align-items-center form-control {% if should_validate and item.balance_error %} is-invalid {% endif %} balance-row">
{{ _("Total") }}
<span id="debit-total" class="amount">{{ item.debit_total }}</span>
</div>
<div id="debit-total-error" class="invalid-feedback balance-error">{{ item.balance_error|default:"" }}</div>
<div id="debit-total-error" class="invalid-feedback balance-error">{% if should_validate %}{{ item.balance_error|default:"" }}{% endif %}</div>
</li>
</ul>
</div>
@ -106,11 +106,11 @@ First written: 2020/7/23
</button>
</li>
<li class="list-group-item">
<div id="credit-total-row" class="d-flex justify-content-between align-items-center form-control {% if item.balance_error %} is-invalid {% endif %} balance-row">
<div id="credit-total-row" class="d-flex justify-content-between align-items-center form-control {% if should_validate and item.balance_error %} is-invalid {% endif %} balance-row">
{{ _("Total") }}
<span id="credit-total" class="amount">{{ item.credit_total }}</span>
</div>
<div id="credit-total-error" class="invalid-feedback balance-error">{{ item.balance_error|default:"" }}</div>
<div id="credit-total-error" class="invalid-feedback balance-error">{% if should_validate %}{{ item.balance_error|default:"" }}{% endif %}</div>
</li>
</ul>
</div>
@ -121,8 +121,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:") }}</label>
</div>
<div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if item.notes.errors %} is-invalid {% endif %}" name="notes">{{ item.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ item.notes.errors.0|default:"" }}</div>
<textarea id="txn-note" class="form-control {% if should_validate and item.notes.errors %} is-invalid {% endif %}" name="notes">{{ item.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ item.notes.errors.0|default:"" }}{% endif %}</div>
</div>
</div>

View File

@ -847,9 +847,11 @@ def txn_edit(request, txn_type, txn=None):
HttpResponse: The response.
"""
form = make_txn_form_from_status(request, txn_type, txn)
should_validate = True
if form is None:
if txn is None:
txn = Transaction(date=timezone.localdate())
should_validate = False
if len(txn.debit_records) == 0:
txn.records.append(Record(ord=1, is_credit=False))
if len(txn.credit_records) == 0:
@ -870,6 +872,7 @@ def txn_edit(request, txn_type, txn=None):
return render(request, F"accounting/transactions/{txn_type}/form.html", {
"item": form,
"summary_categories": get_summary_categories,
"should_validate": should_validate,
"new_record_template": new_record_template,
})