Last active
November 4, 2020 07:34
-
-
Save jlnarvaez/0c789b08f50bb2a625e1a31fe8b68bb1 to your computer and use it in GitHub Desktop.
[MAGENTO 2] Validate field manually
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="form-send-mail" action="<?= $block->getUrl('jlnarvaez_mymodule/sample/send') ?>" method="POST" > | |
<div class="field required"> | |
<div class="control"> | |
<input type="email" name="my_email" class="inpt-mail input-text" | |
id="email_address" value="" | |
placeholder="<?= __('Email') ?>" | |
title="<?= __('Email') ?>" data-validate="{required:true, 'validate-email':true}" /> | |
</div> | |
<a href="" class="confirm-send"><?= __('Confirm') ?></a> | |
</div> | |
</form> | |
<script type="text/javascript"> | |
require([ | |
'jquery', | |
'mage/validation' | |
], function ($) { | |
var dataForm = $('#form-send-mail'); | |
dataForm.mage('validation', {}); // Inicializar validador | |
// Validar con el evento "keyup" | |
dataForm.on('keyup', function () { | |
dataForm.validation('isValid'); | |
}); | |
// Validar al enviar los datos del formulario | |
$('.confirm-send').on('click', function (e) { | |
e.preventDefault(); | |
if (dataForm.validation('isValid')) { | |
$(this).closest('form').submit(); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment