refactor: extracts form submit button

template and js validation
This commit is contained in:
2024-11-21 13:00:11 -03:00
parent d3ff62299d
commit 5030eb42ce
4 changed files with 36 additions and 23 deletions

View File

@@ -28,3 +28,10 @@ body {
margin-bottom: .5rem; margin-bottom: .5rem;
} }
} }
.submit-button {
width: 100%;
@media(min-width: 576px) {
width: 200px;
}
}

View File

@@ -0,0 +1,18 @@
(() => {
'use strict'
// Fetch all the forms we want to apply custom Bootstrap validation styles to
const forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission
Array.from(forms).forEach(form => {
form.addEventListener('submit', event => {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()

View File

@@ -0,0 +1,3 @@
{% macro form_submit_button(text) %}
<button class="btn btn-outline-primary submit-button" type="submit">{{text}}</button>
{% endmacro %}

View File

@@ -1,5 +1,6 @@
{% extends "layout/layout.html" %} {% extends "layout/layout.html" %}
{% from "forms/validation-block.html" import form_field_validation %} {% from "forms/validation-block.html" import form_field_validation %}
{% from "forms/submit-btn.html" import form_submit_button %}
{% from "layout/inner_header.html" import inner_header%} {% from "layout/inner_header.html" import inner_header%}
{% block title %} register {% endblock %} {% block title %} register {% endblock %}
{% block head %} {% block head %}
@@ -90,36 +91,20 @@
{{ form_field_validation(FORM_ERRORS['REQUIRED']) }} {{ form_field_validation(FORM_ERRORS['REQUIRED']) }}
</div> </div>
<div class="col-12 text-center"> <div class="col-12 text-center">
<button class="btn btn-outline-primary register__form-button" type="submit">Register</button> {{ form_submit_button("Register") }}
</div> </div>
</form> </form>
</div> </div>
</div> </div>
<script src="{{ url_for('static', filename='js/validate-form.js') }}"></script>
<script> <script>
(() => { (() => {
'use strict' 'use strict'
const password = document.getElementById("password");
// Fetch all the forms we want to apply custom Bootstrap validation styles to const password_validation = document.getElementById("password-confirmation");
const forms = document.querySelectorAll('.needs-validation')
// Loop over them and prevent submission
Array.from(forms).forEach(form => {
form.addEventListener('submit', event => {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
const password = document.getElementById("password")
const password_validation = document.getElementById("password-confirmation")
password.addEventListener('change', (c) => { password.addEventListener('change', (c) => {
password_validation.setAttribute("pattern", c.target.value) password_validation.setAttribute("pattern", c.target.value);
}) });
})() })()
</script> </script>
{% endblock %} {% endblock %}