zend表单提交错误

时间:2013-10-29 08:28:04

标签: php forms zend-framework

我的代码有问题我试图提交表单来填写数据库并将我重定向到我的索引页面但是当我发送表单时我一直收到此错误:

消息:SQLSTATE [23000]:完整性约束违规:1048列'Number_of_employees'不能为空

以下是addform3的addform,model和form的索引

//Index
      public function addform3Action()
    {
         $form = new Application_Form_CompanyDescription();


        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {                

                $Number_of_employees = $form->getValue('Number of employees');
                $Organisation_type = $form->getValue('$Organisation type');
                $Annual_Turnover = $form->getValue('Annual Turnover');
                $Organisation_Registration_Number = $form->getValue('Organisation Registration Number');
                $Vat_Registrtion_Number = $form->getValue('VatRegistrtion Number');
                $Income_Tax_Reference_Number = $form->getValue('Income Tax Reference Number');
                $Tax_Clearance_Certificate= $form->getValue('Tax Clearance Certificate');
                $Tax_Clearance_Certificate_ExpiryDate = $form->getValue('Tax Clearance Certificate ExpiryDate');
                $Commenced_Trading_Date = $form->getValue('Commenced Trading Date');    
                $BBBEE_Scorecard= $form->getValue('BBBEE Scorecard');
                $Rating_Agency = $form->getValue('Rating Agency');
                $Rating_Expiry_Date = $form->getValue('Rating Expiry Date');                            


                $companydescription= new Application_Model_DbTable_CompanyDescription();
                $companydescription->addform3CompanyDescription($Number_of_employees, $Organisation_type, $Annual_Turnover, $Organisation_Registration_Number, $Vat_Registrtion_Number, $Income_Tax_Reference_Number, $Tax_Clearance_Certificate, $Tax_Clearance_Certificate_ExpiryDate, $Commenced_Trading_Date, $BBBEE_Scorecard, $Rating_Agency, $Rating_Expiry_Date);


                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        }
    }


}  

//model

<?php

class Application_Model_DbTable_CompanyDescription extends Zend_Db_Table_Abstract
{

    protected $_name = 'companydescription';


        public function getCompanyDescription($id)
    {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        throw new Exception("Could not find row $id");
        }
        return $row->toArray();
    }

    public function addform3CompanyDescription($Number_of_employees, $Organisation_type, $Annual_Turnover, $Organisation_Registration_Number, $Vat_Registrtion_Number, $Income_Tax_Reference_Number, $Tax_Clearance_Certificate, $Tax_Clearance_Certificate_ExpiryDate, $Commenced_Trading_Date, $BBBEE_Scorecard, $Rating_Agency, $Rating_Expiry_Date)
    {
        $data = array(
            'Number_of_employees'=> $Number_of_employees,
            'Organisation_type'=> $Organisation_type,
            'Annual_Turnover'=> $Annual_Turnover,
            'Organisation_Registration_Number'=> $Organisation_Registration_Number,
            'Vat_Registrtion_Number'=> $Vat_Registrtion_Number,
            'Income_Tax_Reference_Number'=> $Income_Tax_Reference_Number,
            'Tax_Clearance_Certificate'=> $Tax_Clearance_Certificate,
            'Tax_Clearance_Certificate_ExpiryDate'=> $Tax_Clearance_Certificate_ExpiryDate,
            'Commenced_Trading_Date'=> $Commenced_Trading_Date,
            'BBBEE_Scorecard'=> $BBBEE_Scorecard,
            'Rating_Agency'=> $Rating_Agency,
            'Rating_Expiry_Date'=> $Rating_Expiry_Date,
        );

        $this->insert($data);
    }



}

//aform for addform3

<?php

class Application_Form_CompanyDescription extends Zend_Form
{

    public function init()
    {
        $this->setName('companydescription');
        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');

        $Number_of_employees = new Zend_Form_Element_Text('Number of employees');
        $Number_of_employees->setLabel('Number of employees')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Organisation_type = new Zend_Form_Element_Text('Organisation type');
        $Organisation_type->setLabel('Organisation type')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Annual_Turnover = new Zend_Form_Element_Text('Annual Turnover');
        $Annual_Turnover->setLabel('Annual Turnover')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Organisation_Registration_Number = new Zend_Form_Element_Text('Organisation Registration Number');
        $Organisation_Registration_Number->setLabel('Organisation Registration Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Vat_Registrtion_Number = new Zend_Form_Element_Text('Vat Registrtion Number');
        $Vat_Registrtion_Number->setLabel('Vat Registrtion Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Income_Tax_Reference_Number= new Zend_Form_Element_Text('Income Tax Reference Number');
        $Income_Tax_Reference_Number->setLabel('Income Tax Reference Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Tax_Clearance_Certificate = new Zend_Form_Element_Text('Tax Clearance Certificate');
        $Tax_Clearance_Certificate->setLabel('Tax Clearance Certificate')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Tax_Clearance_Certificate_ExpiryDate = new Zend_Form_Element_Text('Tax Clearance Certificate ExpiryDate');
        $Tax_Clearance_Certificate_ExpiryDate->setLabel('Tax Clearance Certificate ExpiryDate')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Commenced_Trading_Date = new Zend_Form_Element_Text('Commenced Trading Date');
        $Commenced_Trading_Date->setLabel('Commenced Trading Date')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $BBBEE_Scorecard = new Zend_Form_Element_Text('BBBEE Scorecard');
        $BBBEE_Scorecard->setLabel('BBBEE Scorecard')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Rating_Agency = new Zend_Form_Element_Text('Rating Agency');
        $Rating_Agency->setLabel('Rating Agency')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');


        $Rating_Expiry_Date = new Zend_Form_Element_Text('Rating Expiry Date');
        $Rating_Expiry_Date->setLabel('Rating Expiry Date')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');





        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Next');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($id, $Number_of_employees, $Organisation_type, $Annual_Turnover, $Organisation_Registration_Number, $Vat_Registrtion_Number, $Income_Tax_Reference_Number, $Tax_Clearance_Certificate, $Tax_Clearance_Certificate_ExpiryDate, $Commenced_Trading_Date, $BBBEE_Scorecard, $Rating_Agency, $Rating_Expiry_Date, $submit));

    }


}

1 个答案:

答案 0 :(得分:2)

在表单元素名称中使用空格不是一个好主意。呈现表单时将删除它们。因此,对Number of employees值的正确访问将是:

$form->getValue('Numberofemployees');

最好在任何地方使用Number_of_employees,这样您就不需要任何翻译,并且可以使用以下内容:

 $formValues = $form->getValues();
 ...
 $this->insert($formValues);

这里有一个错字:

$form->getValue('$Organisation type');