4

magento 中是否有任何服务器端表单验证?我已经创建了一个 from 并使用 magentos 表单验证,但如果有人禁用 javascipt 并输入可能有害的东西,它就不会工作。如果没有内置的类。有人可以向我指出如何实现服务器端表单验证作为备份的方向。这是我的表单代码

<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

3 回答 3

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

于 2012-09-24T20:04:33.467 回答
4

是的,Magento 对某些表单进行了服务器端验证。但是,添加表单的模块负责对其进行验证 - 因此,如果您正在处理像插件这样的第三方代码,它可能不存在。

通常,验证代码与模块的模型部分一起存在。例如,在 Magento 的内置评论功能中,当提交评论表单时,其数据由文件validate()中的函数验证/app/code/core/Mage/Review/Model/Review.php。我将首先查看该代码以及现有 Mage/Core 模块中的代码作为示例。

在您提供的情况下,验证逻辑的常规位置是/app/code/local/YourCompany/PriceNotify/Model/Pricenotify.php

于 2011-12-02T00:28:01.060 回答
-5

Magento 使用原型来验证表单。要实现此验证,只需将“required-entry”添加到您的输入标签。

于 2011-08-08T16:50:54.660 回答