1

I have a custom payment method I've implemented and it has a form that looks something like this:

<div class="form-list" id="payment_form_<?php echo $this->getMethodCode() ?>" style="display:none;">
    <div>
        <label for="<?php echo $_code ?>_method_no"><?php echo $this->__('Method No') ?>*</label>
        <span>
          <select id="<?php echo $_code ?>_method_no" name="payment[method_no]"
                  class="dropdown validate-select payment-type-select"
                  value="<?php echo $this->htmlEscape($this->getInfoData('method_no')) ?>">
              <option value=""><?php echo $this->__('--Please Select--') ?></option>
              <option title="<?php echo $this->__('Method One') ?>" value="1">Method One</option>
              <option title="<?php echo $this->__('Method Two') ?>" value="2">Method Two</option>
              <option title="<?php echo $this->__('Method Three') ?>" value="3">Method Three</option>
          </select>
        </span>
    </div>
</div>
<div>
    <?php echo $this->getMethod()->getConfigData('message'); ?>
</div>

My question is, how do I validate this in my model validate function? I know that I have validation on the front end with the class validate-select but I would like to know how I can check the value in the validate function. I have something like this right now but it is not working:

<?php
class Name_Mypaymentmethod_Model_Standard extends Mage_Payment_Model_Method_Abstract {
    protected $_code = 'mypaymentmethod';

    protected $_isInitializeNeeded      = true;
    protected $_canUseInternal          = true;
    protected $_canUseForMultishipping  = false;

    protected $_formBlockType = 'mypaymentmethod/form';
    protected $_infoBlockType = 'mypaymentmethod/info';

    public function getOrderPlaceRedirectUrl() {
        return Mage::getUrl('mypaymentmethod/payment/redirect', array('_secure' => true));
    }

    public function assignData($data) {
        $info = $this->getInfoInstance();

        if ($data->getMethodNo())
        {
          $info->setMethodNo($data->getMethodNo());
        }

        return $this;
    }

    public function validate() {
        parent::validate();
        $info = $this->getInfoInstance();

        if (!$info->getMethodNo())
        {
          $errorCode = 'invalid_data';
          $errorMsg = $this->_getHelper()->__("Please select a method.\n");
        }

        if ($errorMsg) 
        {
          Mage::throwException($errorMsg);
        }

        return $this;
    }

}
?>

It seems like the $info->getMethodNo() function is not working for my select dropdown. How do I get the value? I've been looking through forums, tutorials, posts for the last couple hours and can't find anything that shows validating a select dropdown like this.

Any help would be appreciated! Thanks

2
  • Can you please update the answer and add the full code of the class where the validate() method is declared? Commented Apr 22, 2016 at 17:34
  • @SiarheyUchukhlebau updated the question Commented Apr 22, 2016 at 17:40

1 Answer 1

1

If you are using the Magento regular one page checkout:

  1. Your form comes to the Mage_Checkout_OnepageController::savePaymentAction() method.
  2. Then data are transferred to model:

    $data = $this->getRequest()->getPost('payment', array());
    $result = $this->getOnepage()->savePayment($data);
    

    If you don't see your data at this stage, look for a problem on frontend (form submit).

  3. Then your data are transferred to your payment method in the Mage_Checkout_Model_Type_Onepage::savePayment():

    $payment = $quote->getPayment();
    $payment->importData($data);
    $quote->save(); 
    
  4. Validation happens in the importData() method (Mage_Sales_Model_Quote_Payment::importData()):

    /**
     * Import data array to payment method object,
     * Method calls quote totals collect because payment method availability
     * can be related to quote totals
     *
     * @param   array $data
     * @throws  Mage_Core_Exception
     * @return  Mage_Sales_Model_Quote_Payment
     */
    public function importData(array $data)
    {
        $data = new Varien_Object($data);
        Mage::dispatchEvent(
            $this->_eventPrefix . '_import_data_before',
            array(
                $this->_eventObject=>$this,
                'input'=>$data,
            )
        );
    
        $this->setMethod($data->getMethod());
        $method = $this->getMethodInstance();
    
        /**
         * Payment availability related with quote totals.
         * We have to recollect quote totals before checking
         */
        $this->getQuote()->collectTotals();
    
        if (!$method->isAvailable($this->getQuote())
            || !$method->isApplicableToQuote($this->getQuote(), $data->getChecks())
        ) {
            Mage::throwException(Mage::helper('sales')->__('The requested Payment Method is not available.'));
        }
    
        $method->assignData($data);
        /*
        * validating the payment data
        */
        $method->validate();
        return $this;
    }
    

    If on this step data exist at the beginning, but are absent at the end, that possibly a problem in one of observers ($this->_eventPrefix . '_import_data_before').

You need to check your data on each of these steps.
I hope this information will be useful for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.