数据库在验证后从自定义模块表单中更新错误 [英] database getting updated wrong from custom module form after validation

查看:136
本文介绍了数据库在验证后从自定义模块表单中更新错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在Magento中定制模块窗体。我已经使用了Magento给出的默认验证。



这是我的表单并将表单ID传递给varienform

 < form id =my-custom-formaction =method =post> 
< label>形式< /标签>

< label>名字< / label>
< strong>:< / strong>
< input class =input-text required-entrytype =textname =fnamemaxlength =20>< br>
< label>姓氏< / label>
< strong>:< / strong>
< input class =input-text required-entrytype =textname =lnamemaxlength =20>< br>
< label>地址< / label>
< strong>:< / strong>
< textarea class =required-entryplaceholder =输入您的地址name =address>< / textarea>< br>
< label>状态< / label>
< strong>:< / strong>
< input class =required-entrytype =textmaxlength =20name =state>< br>
< label>城市< / label>
< strong>:< / strong>
< input class =required-entrytype =textmaxlength =20name =city>< br>
< label> Mobile No< / label>
< strong>:< / strong>
< input class =required-entrytype =numbermaxlength =10name =mobileno> <峰; br>
< input type =submitname =submitvalue =submit>& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& NBSP;
< input type =buttonname =Cancelvalue =cancel>
< / form>

< script type =text / javascript>
//< ![CDATA [
var customForm = new VarienForm('my-custom-form');
//]]>
< / script>

我已经在indexcontroller中添加了db insert

< pre $ <?php
class MyCustom_Helloworld_IndexController extends Mage_Core_Controller_Front_Action
{
$ b $ * / $
*此方法使用默认操作。
* /
public function indexAction()
{
$ param = $ this-> getRequest() - > getParams();

$ firstname = $ param ['fname'];
$ lastname = $ param ['lname'];
$ address = $ param ['address'];
$ state = $ param ['state'];
$ city = $ param ['city'];
$ mobile = $ param ['mobileno'];

$ model = Mage :: getModel('helloworld / helloworld');
// $ model-> setTitle($ title);
$ model-> setFirstname($ firstname);
$ model-> setLastname($ lastname);
$ model-> setAddress($ address);
$ model-> setState($ state);
$ model-> setCity($ city);
$ model-> setMobileno($ mobile);
$ model-> save();
$ b $ * b $ b *初始化Mage_Core_Model_Layout模型
* /
$ this-> loadLayout();
/ *
*根据布局配置构建页面
* /
$ this-> renderLayout();


$ b

当我运行我的模块时,表单出现,当我没有添加任何值,然后单击提交验证错误得到显示,所以验证没有问题,但是当我检查到数据库时,表正在更新为空值。可能是什么原因?

解决方案

这会阻止您添加空值。

  if($ this-> getRequest() - > getParams()){
$ param = $ this-> getRequest() - > getParams()方法;


$ firstname = $ param ['fname'];
$ lastname = $ param ['lname'];
$ address = $ param ['address'];
$ state = $ param ['state'];
$ city = $ param ['city'];
$ mobile = $ param ['mobileno'];



$ model = Mage :: getModel('helloworld / helloworld');
// $ model-> setTitle($ title);
$ model-> setFirstname($ firstname);
$ model-> setLastname($ lastname);
$ model-> setAddress($ address);
$ model-> setState($ state);
$ model-> setCity($ city);
$ model-> setMobileno($ mobile);
$ model-> save();
$ this-> _redirectReferer();
} else {
/ *
* Mage_Core_Model_Layout模型的初始化
* /
$ this-> loadLayout();
/ *
*根据布局配置构建页面
* /
$ this-> renderLayout();
}


Hi I am working on a custom module form in Magento. I have used the default validation given by Magento.

Here is my form and passing form id to varienform

<form id="my-custom-form" action=""  method="post">
  <label> Form</label>

  <label>First Name</label>
  <strong>:</strong>
  <input class="input-text required-entry" type="text" name="fname" maxlength="20"><br>
  <label>Last Name</label>
  <strong>:</strong>
  <input class="input-text required-entry" type="text" name="lname" maxlength="20"><br>
  <label>Address</label>
  <strong>:</strong>
  <textarea class="required-entry" placeholder="Type your address" name="address"></textarea><br>
  <label>State</label>
  <strong>:</strong>
  <input class="required-entry" type="text" maxlength="20" name="state"><br>
  <label>City</label>
  <strong>:</strong>
  <input class="required-entry" type="text" maxlength="20" name="city"><br>
  <label>Mobile No</label>
  <strong>:</strong>
  <input class="required-entry" type="number" maxlength="10" name="mobileno">    <br>
  <input type="submit" name="submit" value="submit">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input type="button" name="Cancel" value="cancel">
</form>

<script type="text/javascript">
  //< ![CDATA[
    var customForm = new VarienForm('my-custom-form');
  //]]>
</script>

I have added the db insert in indexcontroller

<?php
class MyCustom_Helloworld_IndexController extends     Mage_Core_Controller_Front_Action
{

  /*
  * this method privides default action.
  */
  public function indexAction()
  {
  $param = $this->getRequest()->getParams();

  $firstname = $param['fname'];
  $lastname = $param['lname'];
  $address = $param['address'];
  $state = $param['state'];
  $city = $param['city'];
  $mobile = $param['mobileno'];

  $model = Mage::getModel('helloworld/helloworld');
  // $model->setTitle($title);
  $model->setFirstname($firstname);
  $model->setLastname($lastname);
  $model->setAddress($address);
  $model->setState($state);
  $model->setCity($city); 
  $model->setMobileno($mobile); 
  $model->save();   

  /*
  * Initialization of Mage_Core_Model_Layout model
  */
  $this->loadLayout();
  /*
  * Building page according to layout confuration
  */
  $this->renderLayout();

  }
}

When I run my module the form appears and when I add no values and click submit the validation errors are getting displayed so no problem with validation but when I checked into db the tables are getting updated with null values. What could be the reason ?

解决方案

this will prevent you from adding null value.

if($this->getRequest()->getParams()) {
    $param = $this->getRequest()->getParams();


    $firstname = $param['fname'];
    $lastname = $param['lname'];
    $address = $param['address'];
    $state = $param['state'];
    $city = $param['city'];
    $mobile = $param['mobileno'];



    $model = Mage::getModel('helloworld/helloworld');
    // $model->setTitle($title);
    $model->setFirstname($firstname);
    $model->setLastname($lastname);
    $model->setAddress($address);
    $model->setState($state);
    $model->setCity($city); 
    $model->setMobileno($mobile); 
    $model->save(); 
    $this->_redirectReferer();
    }else {
      /*
    * Initialization of Mage_Core_Model_Layout model
    */
    $this->loadLayout();
    /*
    * Building page according to layout confuration
    */
    $this->renderLayout();
    }

这篇关于数据库在验证后从自定义模块表单中更新错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆