Magento Custom在前端添加表单并进行验证

时间:2015-08-10 05:17:10

标签: xml magento

我是magento的新手。我想在前端添加一个表单,在自定义模块中添加一些字段并进行验证。我搜索了很多但没找到好的magento模块开发教程。

我的控制器文件indexController.php 本地/教程/产品/控制器

  public function saveAction()
{  
    if ($this->getRequest()->isPost()){
        $name = ''.$this->getRequest()->getPost('name');
        $category = ''.$this->getRequest()->getPost('category');
        $telephone = ''.$this->getRequest()->getPost('telephone');
        if(isset($name)&&($name!='') && isset($category)&&($category!='')
                                   && isset($telephone)&&($telephone!='') )
       {
          $contact = Mage::getModel('product/producttest');
          $contact->setData('name', $name);
          $contact->setData('category', $category);
          $contact->setData('telephone', $telephone);
          $contact->save();
       }
   }
    $this->loadLayout();
    $this->getLayout()->getBlock('product_newblock');
    $this->renderLayout();
    $this->_redirect('product/index/index');
}

我的Config.xml文件:local / Tutorial / Product / etc

   <?xml version="1.0"?>
    <config>    
    <modules>
    <Tutorial_Product>
        <version>1.6.2.0.4</version>
    </Tutorial_Product>
   </modules>
 <frontend> 
    <routers> 
        <product> 
            <use>standard</use> 
            <args> 
                <module>Tutorial_Product</module> 
                <frontName>product</frontName> 
            </args> 
        </product> 
    </routers> 
    <layout> 
        <updates> 
            <product> 
                <file>product.xml</file> 
             </product> 
        </updates> 
    </layout> 
 </frontend>
  <global> 
    <blocks> 
        <product> 
            <class>Tutorial_Product_Block</class> 
       </product> 
    </blocks>
    <models>
        <product>
             <class>Tutorial_Product_Model</class>
             <resourceModel>test_mysql4</resourceModel>
         </product>
        <test_mysql4>
             <class>Tutorial_Product_Model_Mysql4</class>
             <entities>
                 <producttest>
                   <table>product_test</table>
                 </producttest>
              </entities>
        </test_mysql4>
    </models>
    <resources>
    <!-- connection to write -->
    <producttest_write>
        <connection>
            <use>core_write</use>
        </connection>
    </producttest_write>
    <!-- connection to read -->
       <producttest_read>
          <connection>
             <use>core_read</use>
          </connection>
       </producttest_read>
        </resources>
  </global>
   </config>

我的阻止页面:local / Tutorial / Product / Block

   class Tutorial_Product_Block_Monblock extends Mage_Core_Block_Template {

 public function methodblock() {
    //return 'informations about my block !!' ;
    //$retour='<b>Informations about my Products :</b>' ;
    //$retour .= '<br/><br/>';
    $collection = Mage::getModel('product/producttest')->getCollection()
                             ->setOrder('id','asc');
    $i = 0;
    foreach($collection as $data)
    {
        // $retour= $data->getData('name').' '.$data->getData('category').' '.$data->getData('telephone').'<br />';
        $retour[$i]['name'] = $data->getData('name');
        $retour[$i]['catgeory'] = $data->getData('category');
        $retour[$i]['number'] = $data->getData('telephone');
        $i++;

     }
     Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
     return $retour;
}

}

我的布局文件:rwd / default / layout / product.xml

   <?xml version="1.0"?>
    <layout version="0.1.0">
      <default>
      <reference name="content">
      </reference>
  </default>
  <product_index_index>  
       <reference name="content">
            <block type="product/monblock"  name="product_monblock" template="product/afficher.phtml" />
       </reference>
  </product_index_index>
  <product_index_save>  
       <reference name="content">
            <block type="core/template" name="product_form" template="product/form.phtml" />
       </reference>
  </product_index_save>
 </layout>

我的观看文件:rwd / default / template / product

        <form action="<?php echo Mage::getUrl('product/index/save') ?>" method="post">
 <fieldset>
<ul>
 <li>
   <label for="name">Name</label>
   <input type="text" id="name" name="name" />
 </li>
 <li>
     <label for="category">Category</label>
     <input type="text" id="category" name="category" />
 </li>
 <li>
    <label for="telephone">Telephone</label>
    <input type="text" id="telephone" name="telephone" />
 </li>
 <li>
    <input type="submit" value="Save" />
</li>
</ul>
</fieldset>
</form>

任何人都可以帮助我了解它。 提前致谢

0 个答案:

没有答案