cancel
Showing results for 
Search instead for 
Did you mean: 

how to validate the radio button is selected?

SOLVED

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.

<?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>

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

<?php
// 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

1 REPLY

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!');
  			}
	}	
   }
}