Aktualizacja walidacji formy

This commit is contained in:
Roman Pyrih
2026-05-12 09:59:58 +02:00
parent fc2748e5db
commit 56c982f99e

View File

@@ -33,131 +33,245 @@
</div>
<? endif;?>
<? if ( $this -> client ):?><div class="right"><? endif;?>
<form class="form-horizontal" action="/zloz-zamowienie" method="POST" id="form-order">
<input type="hidden" name="order_submit_token" value="<?= htmlspecialchars( (string)($this -> order_submit_token ?? ''), ENT_QUOTES, 'UTF-8' );?>">
<? if ( !$this -> client ):?>
<form class="form-horizontal needs-validation" action="/zloz-zamowienie" method="POST" id="form-order" novalidate>
<input type="hidden" name="order_submit_token" value="<?= htmlspecialchars( (string)($this -> order_submit_token ?? ''), ENT_QUOTES, 'UTF-8' );?>">
<? if ( !$this -> client ):?>
<div class="form-group row">
<div class="col-12">
<input type="email" name="email" class="form-control" required placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'email' ) );?>">
<div class="invalid-feedback">
Podaj prawidłowy adres e-mail.
</div>
</div>
</div>
<? endif;?>
<div class="form-group row">
<div class="col-12">
<input type="email" name="email" class="form-control" required placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'email' ) );?>">
</div>
</div>
<? endif;?>
<div class="form-group row">
<div class="col-12">
<input type="text" id="name" name="name" class="form-control" required placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'imie' ) );?>" value="<?= htmlspecialchars( $address_current['name'] );?>">
</div>
</div>
<div class="form-group row">
<div class="col-12">
<input type="text" id="surname" name="surname" class="form-control" required placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'nazwisko' ) );?>" value="<?= htmlspecialchars( $address_current['surname'] );?>">
</div>
</div>
<div class="form-group">
<input type="text" id="street" name="street" class="form-control" <? if ( !in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> required<? endif;?> placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'ulica-i-nr-domu' ) );?> <? if ( in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> (<?= \Shared\Helpers\Helpers::lang( 'opcjonalnie' );?>)<? endif;?>" value="<?= htmlspecialchars( $address_current['street'] );?>">
</div>
<div class="form-group">
<div class="row">
<div class="col-12 col-md-6">
<input type="text" id="postal_code" name="postal_code" pattern="[0-9]{2}-[0-9]{3}" title="Format xx-xxx" class="form-control" <? if ( !in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> required<? endif;?> placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'kod-pocztowy' ) );?><? if ( in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> (<?= \Shared\Helpers\Helpers::lang( 'opcjonalnie' );?>)<? endif;?>" " value="<?= htmlspecialchars( $address_current['postal_code'] );?>">
</div>
<div class="col-12 col-md-6">
<input type="text" id="city" name="city" class="form-control" <? if ( !in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> required<? endif;?> placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'miasto' ) );?><? if ( in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> (<?= \Shared\Helpers\Helpers::lang( 'opcjonalnie' );?>)<? endif;?>" " value="<?= htmlspecialchars( $address_current['city'] );?>">
</div>
</div>
</div>
<div class="form-group">
<div class="form-control form-control-phone" tooltip="Wprowadź numer w formacie: 123456789">
<div class="form-control-phone-code">
<img src="/images/system/lang_pl.png" alt="">
<span>+48</span>
</div>
<input type="tel" id="phone" name="phone" class="form-control form-control-input" required placeholder="<?= ucfirst(\Shared\Helpers\Helpers::lang('telefon')); ?>" value="<?= htmlspecialchars($address_current['phone']); ?>" pattern="[0-9]{9}">
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12" id="fvat">
<input type="checkbox" name="fvat">
<?= \Shared\Helpers\Helpers::lang( 'faktura-vat' );?>
</div>
</div>
</div>
<div class="fvat-data">
<div class="form-group">
<div class="row">
<div class="col-12">
<input type="text" id="firm_name" name="firm_name" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'nazwa-firmy' ) );?>" value="">
<input type="text" id="name" name="name" class="form-control" required placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'imie' ) );?>" value="<?= htmlspecialchars( $address_current['name'] );?>">
<div class="invalid-feedback">
Podaj imię.
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12">
<input type="text" id="firm_street" name="firm_street" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'ulica-i-nr-domu' ) );?>" value="">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12 col-md-6">
<input type="text" id="firm_postal_code" name="firm_postal_code" pattern="[0-9]{2}-[0-9]{3}" title="Format xx-xxx" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'kod-pocztowy' ) );?>" value="">
</div>
<div class="col-12 col-md-6">
<input type="text" id="firm_city" name="firm_city" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'miasto' ) );?>" value="">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12">
<input type="text" id="firm_nip" name="firm_nip" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'nip' ) );?>" value="">
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12" id="agreement">
<input type="checkbox" name="agreement" required="">
<?= \Shared\Helpers\Helpers::lang( 'przeczytalem-i-akceptuje-polityke-prywatnosci-i-regulamin' );?>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="form-group row">
<div class="col-12">
<button type="submit" class="btn btn-success" id="order-send"><span class="text"><?= ucfirst( \Shared\Helpers\Helpers::lang( 'zamawiam-z-obowiazkiem-zaplaty' ) );?></span></button>
<input type="text" id="surname" name="surname" class="form-control" required placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'nazwisko' ) );?>" value="<?= htmlspecialchars( $address_current['surname'] );?>">
<div class="invalid-feedback">
Podaj nazwisko.
</div>
</div>
</div>
</div>
<? if ( $this -> client ):?>
<div class="form-group">
<input type="text" id="street" name="street" class="form-control" <? if ( !in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> required<? endif;?> placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'ulica-i-nr-domu' ) );?><? if ( in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> (<?= \Shared\Helpers\Helpers::lang( 'opcjonalnie' );?>)<? endif;?>" value="<?= htmlspecialchars( $address_current['street'] );?>">
<div class="invalid-feedback">
Podaj ulicę i numer domu.
</div>
</div>
</div>
<? endif;?>
</form>
<div class="form-group">
<div class="row">
<div class="col-12 col-md-6">
<input type="text" id="postal_code" name="postal_code" pattern="[0-9]{2}-[0-9]{3}" title="Format xx-xxx" class="form-control" <? if ( !in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> required<? endif;?> placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'kod-pocztowy' ) );?><? if ( in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> (<?= \Shared\Helpers\Helpers::lang( 'opcjonalnie' );?>)<? endif;?>" value="<?= htmlspecialchars( $address_current['postal_code'] );?>">
<div class="invalid-feedback">
Podaj kod pocztowy w formacie 00-000.
</div>
</div>
<div class="col-12 col-md-6">
<input type="text" id="city" name="city" class="form-control" <? if ( !in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> required<? endif;?> placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'miasto' ) );?><? if ( in_array( $this -> transport_method['id'], [ 1, 2, 3, 9] ) ):?> (<?= \Shared\Helpers\Helpers::lang( 'opcjonalnie' );?>)<? endif;?>" value="<?= htmlspecialchars( $address_current['city'] );?>">
<div class="invalid-feedback">
Podaj miasto.
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="form-control form-control-phone" tooltip="Wprowadź numer w formacie: 123456789">
<div class="form-control-phone-code">
<img src="/images/system/lang_pl.png" alt="">
<span>+48</span>
</div>
<input type="tel" id="phone" name="phone" class="form-control form-control-input" required placeholder="<?= ucfirst(\Shared\Helpers\Helpers::lang('telefon')); ?>" value="<?= htmlspecialchars($address_current['phone']); ?>" pattern="[0-9]{9}">
<div class="invalid-feedback">
Podaj numer telefonu w formacie 123456789.
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12" id="fvat">
<div class="form-check">
<input type="checkbox" name="fvat" id="fvat_checkbox" class="form-check-input">
<label class="form-check-label" for="fvat_checkbox">
<?= \Shared\Helpers\Helpers::lang( 'faktura-vat' );?>
</label>
</div>
</div>
</div>
</div>
<div class="fvat-data">
<div class="form-group">
<div class="row">
<div class="col-12">
<input type="text" id="firm_name" name="firm_name" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'nazwa-firmy' ) );?>" value="" data-required-if-fvat="1">
<div class="invalid-feedback">
Podaj nazwę firmy.
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12">
<input type="text" id="firm_street" name="firm_street" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'ulica-i-nr-domu' ) );?>" value="" data-required-if-fvat="1">
<div class="invalid-feedback">
Podaj ulicę i numer domu firmy.
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12 col-md-6">
<input type="text" id="firm_postal_code" name="firm_postal_code" pattern="[0-9]{2}-[0-9]{3}" title="Format xx-xxx" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'kod-pocztowy' ) );?>" value="" data-required-if-fvat="1">
<div class="invalid-feedback">
Podaj kod pocztowy firmy w formacie 00-000.
</div>
</div>
<div class="col-12 col-md-6">
<input type="text" id="firm_city" name="firm_city" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'miasto' ) );?>" value="" data-required-if-fvat="1">
<div class="invalid-feedback">
Podaj miasto firmy.
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12">
<input type="text" id="firm_nip" name="firm_nip" class="form-control" placeholder="<?= ucfirst( \Shared\Helpers\Helpers::lang( 'nip' ) );?>" value="" pattern="[0-9]{10}" data-required-if-fvat="1">
<div class="invalid-feedback">
Podaj NIP w formacie 10 cyfr, bez spacji i myślników.
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12" id="agreement">
<div class="form-check">
<input type="checkbox" name="agreement" id="agreement_checkbox" class="form-check-input" required>
<label class="form-check-label" for="agreement_checkbox">
<?= \Shared\Helpers\Helpers::lang( 'przeczytalem-i-akceptuje-polityke-prywatnosci-i-regulamin' );?>
</label>
<div class="invalid-feedback">
Musisz zaakceptować politykę prywatności i regulamin.
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-12">
<button type="submit" class="btn btn-success" id="order-send">
<span class="text"><?= ucfirst( \Shared\Helpers\Helpers::lang( 'zamawiam-z-obowiazkiem-zaplaty' ) );?></span>
</button>
</div>
</div>
</div>
<? if ( $this -> client ):?>
</div>
</div>
<? endif;?>
</form>
<? if ( $this -> client ):?></div><? endif;?>
</div>
<script class="footer">
$(document).ready(function () {
(function() {
'use strict';
function validatePhone() {
var phone = $('#phone');
var phonePattern = /^[0-9]{9}$/;
window.addEventListener('load', function() {
var form = document.getElementById('form-order');
if (!phone.val() || !phonePattern.test(phone.val())) {
$('#order-send').prop('disabled', true);
} else {
$('#order-send').prop('disabled', false);
if (!form) {
return;
}
}
$('#phone').on('input', function () {
validatePhone();
});
var fvatCheckbox = document.getElementById('fvat_checkbox');
var fvatRequiredFields = form.querySelectorAll('[data-required-if-fvat="1"]');
validatePhone();
});
function updateFvatRequiredFields() {
var isRequired = fvatCheckbox && fvatCheckbox.checked;
Array.prototype.forEach.call(fvatRequiredFields, function(field) {
field.required = isRequired;
if (!isRequired) {
field.setCustomValidity('');
}
});
}
if (fvatCheckbox) {
fvatCheckbox.addEventListener('change', updateFvatRequiredFields);
updateFvatRequiredFields();
}
form.addEventListener('submit', function(event) {
updateFvatRequiredFields();
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
var firstInvalidField = form.querySelector(':invalid');
if (firstInvalidField) {
firstInvalidField.focus();
}
}
form.classList.add('was-validated');
}, false);
}, false);
})();
// $(document).ready(function () {
// function validatePhone() {
// var phone = $('#phone');
// var phonePattern = /^[0-9]{9}$/;
// if (!phone.val() || !phonePattern.test(phone.val())) {
// $('#order-send').prop('disabled', true);
// } else {
// $('#order-send').prop('disabled', false);
// }
// }
// $('#phone').on('input', function () {
// validatePhone();
// });
// validatePhone();
// });
$( function()
{