Showing results for 
Search instead for 
Did you mean: 

how to validate the radio button is selected?


how to validate the radio button is selected?

I am creating a new payment method. With this new payment method, the customer will buy the product only if it responds correctly to a question that is asked. How do I verify that the customer selects an answer? In addition, except as the customer response to check later if one is correct?


app / design / frontend / base / default / template / custompaymentmethod / form / custompaymentmethod.phtml: It's a template file used to display a custom payment form for our custom payment method.

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

app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.php: It's a model file used to validate and save the custom payment fields information.

// app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.php
class Envato_Custompaymentmethod_Model_Paymentmethod extends Mage_Payment_Model_Method_Abstract {
  protected $_code  = 'custompaymentmethod';
  protected $_formBlockType = 'custompaymentmethod/form_custompaymentmethod';
  protected $_infoBlockType = 'custompaymentmethod/info_custompaymentmethod';

  public function getOrderPlaceRedirectUrl()
    return Mage::getUrl('custompaymentmethod/payment/redirect', array('_secure' => false));

Sorry for my poor English


Re: how to validate the radio button is selected?

I solved

public function validate()
  foreach (Mage::getModel('emme_question/question')->load(1)->getSelectedAnswersCollection() as $answer) 
	if ($answer->getIsCorrect()) 
		if ($answer->getId() == $_POST['my_custom_answer']) 
   			Mage::getSingleton('core/session')->addSuccess('Risposta esatta');
  		} else 
    				Mage::throwException('Risposta sbagliata!');