Created
February 26, 2018 21:38
-
-
Save magevision/216995e172a023c6ee01e02223f94dd5 to your computer and use it in GitHub Desktop.
Display Gender field as Radio button
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
<div class="field gender<?php if ($block->isRequired()) echo ' required' ?>"> | |
<label class="label" for="<?php /* @escapeNotVerified */ echo $block->getFieldId('gender')?>"> | |
<span><?php /* @escapeNotVerified */ echo __('Gender') ?></span> | |
</label> | |
<div class="control"> | |
<?php $options = $block->getGenderOptions(); ?> | |
<?php $value = $block->getGender(); ?> | |
<?php foreach ($options as $option): ?> | |
<?php if(!$option->getValue()) continue; ?> | |
<div class="field choice"> | |
<input type="radio" | |
class="radio" | |
id="gender-option-<?php /* @escapeNotVerified */ echo $option->getValue() ?>" | |
<?php if ($block->isRequired()) echo 'data-validate="{\'validate-one-required-by-name\':true}"'?> | |
name="<?php /* @escapeNotVerified */ echo $block->getFieldName('gender')?>" | |
data-selector="<?php /* @escapeNotVerified */ echo $block->getFieldName('gender')?>" | |
<?php if ($option->getValue() == $value) echo ' checked="checked"' ?> | |
value="<?php /* @escapeNotVerified */ echo $option->getValue() ?>"/> | |
<label class="label" | |
for="gender-option-<?php /* @escapeNotVerified */ echo $option->getValue() ?>"> | |
<span><?php /* @escapeNotVerified */ echo $option->getLabel() ?></span> | |
</label> | |
</div> | |
<?php endforeach;?> | |
<div id="gender-option-error-message-container"></div> | |
</div> | |
</div> | |
<script> | |
require([ | |
"jquery", | |
"mage/mage" | |
], function($){ | |
var dataForm = $('#form-validate'); | |
dataForm.mage('validation', { | |
errorPlacement: function(error, element) { | |
if (element.attr('data-validate') && element.attr('data-validate').indexOf('validate-one-required-by-name') >= 0) { | |
error.appendTo('#gender-option-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
<?php $_gender = $block->getLayout()->createBlock('Magento\Customer\Block\Widget\Gender') ?> | |
<?php if ($_gender->isEnabled()): ?> | |
<?= $_gender->setGender($block->getFormData()->getGender())->toHtml() ?> | |
<?php endif ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment