Last active
February 19, 2022 12:20
-
-
Save magevision/f61914e8eda428679a51178c8103bccc to your computer and use it in GitHub Desktop.
Form Validation Custom Error Placement
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 class="form contact" | |
action="<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>" | |
id="contact-form" | |
method="post" | |
data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>" | |
data-mage-init='{"validation":{}}'> | |
<fieldset class="fieldset"> | |
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Write Us') ?></span></legend><br /> | |
<div class="field note no-label"><?php /* @escapeNotVerified */ echo __('Jot us a note and we’ll get back to you as quickly as possible.') ?></div> | |
<div class="field name required"> | |
<label class="label" for="name"><span><?php /* @escapeNotVerified */ echo __('Name') ?></span></label> | |
<div class="control"> | |
<input name="name" id="name" title="<?php /* @escapeNotVerified */ echo __('Name') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('name') ?: $this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/> | |
</div> | |
</div> | |
<div class="field email required"> | |
<label class="label" for="email"><span><?php /* @escapeNotVerified */ echo __('Email') ?></span></label> | |
<div class="control"> | |
<input name="email" id="email" title="<?php /* @escapeNotVerified */ echo __('Email') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('email') ?: $this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/> | |
</div> | |
</div> | |
<div class="field telephone"> | |
<label class="label" for="telephone"><span><?php /* @escapeNotVerified */ echo __('Phone Number') ?></span></label> | |
<div class="control"> | |
<input name="telephone" id="telephone" title="<?php /* @escapeNotVerified */ echo __('Phone Number') ?>" value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('telephone')) ?>" class="input-text" type="text" /> | |
</div> | |
</div> | |
<div class="field comment required"> | |
<label class="label" for="comment"><span><?php /* @escapeNotVerified */ echo __('What’s on your mind?') ?></span></label> | |
<div class="control"> | |
<textarea name="comment" id="comment" title="<?php /* @escapeNotVerified */ echo __('What’s on your mind?') ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"><?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('comment')) ?></textarea> | |
</div> | |
</div> | |
<?php echo $block->getChildHtml('form.additional.info'); ?> | |
</fieldset> | |
<div id="custom-error-message-container"></div> | |
<div class="actions-toolbar"> | |
<div class="primary"> | |
<input type="hidden" name="hideit" id="hideit" value="" /> | |
<button type="submit" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>" class="action submit primary"> | |
<span><?php /* @escapeNotVerified */ echo __('Submit') ?></span> | |
</button> | |
</div> | |
</div> | |
</form> | |
<script> | |
require([ | |
"jquery", | |
"mage/mage" | |
], function($){ | |
var dataForm = $('#contact-form'); | |
dataForm.mage('validation', { | |
errorPlacement: function(error, element) { | |
if (element.is('#email')) { | |
error.appendTo('#custom-error-message-container'); | |
} else { | |
element.after(error); | |
} | |
}, | |
}); | |
}); | |
</script> |
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
$.widget('mage.validation', { | |
options: { | |
meta: 'validate', | |
onfocusout: false, | |
onkeyup: false, | |
onclick: false, | |
ignoreTitle: true, | |
errorClass: 'mage-error', | |
errorElement: 'div', | |
/** | |
* @param {*} error | |
* @param {*} element | |
*/ | |
errorPlacement: function (error, element) { | |
var errorPlacement = element, | |
fieldWrapper; | |
// logic for date-picker error placement | |
if (element.hasClass('_has-datepicker')) { | |
errorPlacement = element.siblings('button'); | |
} | |
// logic for field wrapper | |
fieldWrapper = element.closest('.addon'); | |
if (fieldWrapper.length) { | |
errorPlacement = fieldWrapper.after(error); | |
} | |
//logic for checkboxes/radio | |
if (element.is(':checkbox') || element.is(':radio')) { | |
errorPlacement = element.parents('.control').children().last(); | |
//fallback if group does not have .control parent | |
if (!errorPlacement.length) { | |
errorPlacement = element.siblings('label').last(); | |
} | |
} | |
//logic for control with tooltip | |
if (element.siblings('.tooltip').length) { | |
errorPlacement = element.siblings('.tooltip'); | |
} | |
errorPlacement.after(error); | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment