2011-07-29 113 views
4

在magento中是否有任何服務器端表單驗證?我創建了一個使用magentos表單驗證的工具,但如果有人禁用了javascipt並輸入了一些可能有害的東西,它就不會工作。如果沒有內置課程的話。有人可以請我指出如何實現服務器端表單驗證作爲備份的方向。這裏是形式Magento服務器端表單驗證

<div style="border:0px solid red; margin:0px auto;"> 

<?php $_product = $this->getProduct(); ?> 


<form id="test" action="<?php echo Mage::getUrl('pricenotify/pricenotify/db') ?>" method="post"> 

      <label for="price">Price *</label> 
      <input type="text" id="price" name="price" value="" class="required-entry validate-number"/><br /> 
      <label for="email">Email Address *</label> 
      <input type="text" id="email" name="email" value="" class="required-entry validate-email"/> 
      <input type="hidden" id="id" name="id" value="<?php echo $_product->getId() ?>" /> 
      <input type="hidden" id="propri" name="propri" value="<?php echo $_product->getPrice() ?>" /> 

      <input type="submit" name="submit" value="<?php echo $this->__('Submit') ?>" onclick="if(customForm.validator && customForm.validator.validate()) this.form.request(); return false;" /> 

</form> 

<script type="text/javascript"> 
//< ![CDATA[ 
var customForm = new VarienForm('test',false); 
//]]> 
</script> 

回答

-4

Magento的使用原型來驗證我的形式我的代碼。要實現這種驗證,只需在輸入標籤中添加「required-entry」即可。

+8

OP明確要求進行服務器端驗證。原型只有js和客戶端(也真的**不**安全) –

3

是的,Magento對某些表單進行了服務器端驗證。然而,添加表單的模塊負責驗證它 - 所以如果你正在處理像插件這樣的第三方代碼,它可能不在那裏。

通常,驗證碼與模塊的模型部分一起存在。例如,在Magento的內置評論功能中,當提交評論表單時,其數據由/app/code/core/Mage/Review/Model/Review.php文件中的validate()函數進行驗證。我會先看看這些代碼,並以現有的Mage/Core模塊中的代碼爲例。

在你給的情況下,爲驗證邏輯傳統的地方會/app/code/local/YourCompany/PriceNotify/Model/Pricenotify.php

8

如果你想保持它的簡單,你可以做驗證在你的控制器

try { 
      $postObject = new Varien_Object(); 
      $postObject->setData($post); 

      $error = false; 

      if (!Zend_Validate::is($postObject->getPrice(), 'NotEmpty')) { 
       $error = true; 
      } 

      if (!Zend_Validate::is($postObject->getEmail(), 'EmailAddress')) { 
       $error = true; 
      } 

      if ($error) { 
       throw new Exception(); 
      } 


      //save to db 

      return; 
     } catch (Exception $e) { 
      Mage::getSingleton('customer/session')->addError(Mage::helper('pricenotify')->__('Unable to submit your request. Please, try again later')); 
      $this->_redirect('/'); 

      return; 
     } 

Zend_Validate的: http://files.zend.com/help/Zend-Framework/zend.validate.html

+0

謝謝先生的答案它幫助我 –