cancel
Showing results for 
Search instead for 
Did you mean: 

How to validate radio button?

SOLVED

How to validate radio button?

Hi, in my custom module, I have a form which has radio buttons.

When I click the submit button it does not validate the radio button.

 

<?php$question = Mage::getModel('emme_question/question')->getCollection()->getLastItem();
$answers = $question->getSelectedAnswersCollection();
?>
<h4><?php echo $this->escapeHtml($question->getValue()); ?></h4>
<ul>
<?php foreach ($answers as $answer): ?>
<li>
<label><?php echo $this->escapeHtml($answer->getValue()) ?></label>
<input type="radio" name="my_custom_answer" value="<?php echo $answer->getId() ?>" required>
</li>
<?php endforeach ?>
3 REPLIES

Re: How to validate radio button?

Hi @Benix89

 

Try with class name  validate-one-required-by-name and assign same name to both radio buttons.

 

sample code :

 

<ul class="options-list" id="options-1-list">
    <li>
        <input type="radio" value="1" id="options_1" name="myradio1" class="validate-one-required-by-name">
        <span class="label">
            <label for="options_1">Blue </label>
        </span>
    </li>
    <li>
        <input type="radio" value="2" id="options_2" name="myradio1" class="validate-one-required-by-name">
        <span class="label">
            <label for="options_1">Blue </label>
        </span>
    </li>
</ul>
---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: How to validate radio button?

As advised, I added class name validate-one-required-by-name.

I modified the code

 

<?php$question = Mage::getModel('emme_question/question')->getCollection()->getLastItem();
$answers = $question->getSelectedAnswersCollection();
?>
<h4><?php echo $this->escapeHtml($question->getValue()); ?></h4>
<ul>
<?php foreach ($answers as $answer): ?>
<li>
<label><?php echo $this->escapeHtml($answer->getValue()) ?></label>
<input type="radio" name="my_custom_answer" value="<?php echo $answer->getId() ?>" class="validate-one-required-by-name" required>
</li>
<?php endforeach ?>


Now, however, it does not produce any output

Output Payment

Why?

Re: How to validate radio button?

I solved

<?php
$question = Mage::getModel('emme_question/question')->getCollection()->getLastItem();
$answers = $question->getSelectedAnswersCollection();
?>
<div id="custompaymentmethod-question">
    <h4><?php echo $this->escapeHtml($question->getValue()); ?></h4>
    <ul>
    <?php foreach ($answers as $answer): ?>
    <li>
    <label><?php echo $this->escapeHtml($answer->getValue()) ?></label>
    <input type="radio" name="my_custom_answer" value="<?php echo $answer->getId() ?>" required>
    </li>
    <?php endforeach ?>
</div>
<script>
jQuery(function ($) {
  $('#co-payment-form').on('change.mm', function () {
    var is_question_active = ! $('#p_method_custompaymentmethod').is(':checked');
    $('#custompaymentmethod-question input').attr('disabled', is_question_active);
  })
})
</script>