在前面,简单的实现了一个magento的标准模块,即前台表单提交,后台可以查看并进行编辑的功能,但是仅仅只是实现了这样一个功能,还有很多bug需要修复,比如:表单验证,防止刷新重复提价数据等等这些问题需要考虑。
先来解决第一个问题,关于表单验证,需要在客户端和服务端进行双重验证,在客户端对表单验证,就涉及到在magento中如何添加js文件的问题,这里多阐述下如何在magento中加载js代码:

1.全局调用js文件,使用于多个页面都需要加载js文件

文件路径:
/app/design/frontend/default/Your_Template/layout/page.xml
xml布局文件例如:
<action method="addJs"><script>varien/js.js</script></action>  
<action method="addItem"><type>skin_js</type><name>js/count.js</name></action>

2.通过模板文件xxx.phtml页面,使用Magento自带的帮助函数来引用JS文件,例如:

<?php echo $this->helper('core/js')->includeScript('count/count.js') ?>  

适用于:在某些特定页面加载特定的js文件,而这些js文件在其他页面中不常使用。

3.将js文件添加到主题下面的js文件夹内,例如在xxx.phtml文件添加:

<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/count.js') ?>"></script> 

再在skin/frontend/yourtheme/default/js/文件夹下添加count.js。

4.在对应的block类中调用JS,例如:

protected function_prepareLayout(){  
    $this->getLayout()->getBlock('head')->addJs('mage/adminhtml/sales.js');  
    ...... 
    returnparent::_prepareLayout();  
}  

这里,我用的表单验证方式是采用的第3种:
在count.phtml页面中结尾添加:

<script type="text/javascript">
//< ![CDATA[
  var myForm= new VarienForm('count', true);
//]]>
</script>

然后采用通过检查input中class类名的形式,来对表单进行验证,所有这里需要修改下count.phtml页面,修改如下:

<form id="count" action="<?php echo Mage::getUrl('count/index/save') ?>" method="post" >
        <fieldset>
            <ul>
                <li>
                    <label><?php echo $this->__('Model Name') ?></label>
                </li>
                <li>
                    <input type="text" id="Model_Name" name="Model_Name" class="required-entry"/>
                </li>
                <li>
                    <label><?php echo $this->__('Name') ?></label></li>
                <li>
                    <input type="text" id="Name" name="Name" class="required-entry"/>
                </li>
                <li>
                    <label><?php echo $this->__('Serial Number') ?></label>
                </li>
                <li>
                    <input type="text" id="Serial_Number" name="Serial_Number" class="validate-number required-entry"/>
                </li>
                <li>
                    <label><?php echo $this->__('Email') ?></label>
                </li>
                <li>
                    <input type="text" id="Email" name="Email" class="required-entry validate-email"/>
                </li>
                <li>
                    <label><?php echo $this->__('Data Purchased') ?></label>
                </li>
                <li>
                    <input type="date" id="Data_Purchased" name="Data_Purchased" class="validate-date" />
                </li>
                <li>
                    <label for="Message_Source"><?php echo $this->__('How Did You Hear About INGEAR?') ?></label>
                </li>
                <li>
                    <select id="Messages_Source" name="Messages_Source" class="validate-select">
                        <option value="Web_search">Web search</option>
                        <option value="Online_review">Online review</option>
                        <option value="Online_Ad">Online Ad</option>
                        <option value="Other">Other</option>
                    </select>
                </li>
                <li>
                    <label><?php echo $this->__('Let us know how we\'re doing!Tell us what you think about our products?') ?></label>
                </li>
                <li>
                    <textarea  rows="10" id="Content" name="Content"></textarea>
                </li>
                <li>
                    <input type="submit" value="<?php echo $this->__('sub') ?>" />
                </li>
            </ul>
        </fieldset>
</form>
//在表单结尾处添加实例化 VarienForm这个js对象,
<script type="text/javascript">  
    //< ![CDATA[  
      var myForm= new VarienForm('count', true);  
      //第一参数为form表单的id,第二个参数为重新定位光标位置,true为将光标自动移动input首位,false为禁用此功能。
    //]]>  
</script>  

客户端的js验证就好了,但是对于表单来说,仅仅是客户端对表单内容进行限制和过滤是不够的,接下来,还需要在服务端对表单提交的数据进行验证:
在IndexController.php下的saveaction中添加如下内容:

public function saveAction()
    {
        $data = $this->getRequest()->getPost();
        $count = Mage::getModel('count/count')->setData($data);       
       
       if($count->validate() === true)
       {
           try {
               $count->setData($data)->save();
           } catch (Exception $e) {
               Mage::log('Mage::getModel(count/count)->save():'.$e->getMessage());
           }
       } else {
            //print_r($count->validate());
           Mage::log("Mage::getModel(count/count)->validate():".print_r($count->validate(), true));
           $this->_redirect('*/*');
       }

        $this->_redirect('*/*/');
}

在Model中添加验证方法Model/Count.php:

public function validate()
    {
        $errors = array();
        $helper = Mage::helper('count');
    
        if (!Zend_Validate::is($this->getData('Model_Name'), 'NotEmpty')) {
            $errors[] = $helper->__('Please enter the Model Name.');
        }
    
        if (!Zend_Validate::is($this->getData('Name'), 'NotEmpty')) {
            $errors[] = $helper->__('Please enter the name.');
        }
    
        if (!Zend_Validate::is($this->getData('Email'), 'EmailAddress')) {
            $errors[] = $helper->__('Invalid email address "%s".');
        }
    
        if (!Zend_Validate::is($this->getData('Serial_Number'), 'NotEmpty')) {
            $errors[] = $helper->__('Please define date.');
        }
        
        if (!Zend_Validate::is($this->getData('Data_Purchased'), 'Date')) {
            $errors[] = $helper->__('Please define Data_Purchased.');
        }
        
        if (!Zend_Validate::is($this->getData('Message_Source'), 'NotEmpty')) {
            $errors[] = $helper->__('Please define Message_Source.');
        }
        
        if (!Zend_Validate::is($this->getData('Content'), 'NotEmpty')) {
            $errors[] = $helper->__('Please define Content.');
        }
    
        if (empty($errors) || $this->getShouldIgnoreValidation()) {
            return true;
        }
        //print_r($errors);
        return $errors;
    }

注释下,magento的默认验证规则在lib/Zend/Validate文件夹下,'NotEmpty','EmailAddress'即对应NotEmpty.php,EmailAddress.php文件。


denson
366 声望57 粉丝

引用和评论

0 条评论