Refactor form validation helpers

This commit is contained in:
Paul Chavard 2018-10-09 11:43:25 +02:00
parent 8c16eb4cd0
commit d18b1c8ddc

View file

@ -1,10 +1,19 @@
import $ from 'jquery'; import { delegate } from '@utils';
$(document).on('blur keydown', 'input, textarea', () => { delegate('blur keydown', 'input, textarea', ({ target }) => {
$(this).addClass('touched'); touch(target);
}); });
$(document).on('click', 'input[type="submit"]:not([formnovalidate])', () => { delegate(
const $form = $(this).closest('form'); 'click',
$('input, textarea', $form).addClass('touched'); 'input[type="submit"]:not([formnovalidate])',
}); ({ target }) => {
let form = target.closest('form');
let inputs = form ? form.querySelectorAll('input, textarea') : [];
[...inputs].forEach(touch);
}
);
function touch({ classList }) {
classList.add('touched');
}