在zend框架中使用表单中的文件元素时,isValid方法不起作用

时间:2012-03-26 09:56:54

标签: zend-framework zend-form

表格代码:

     $companyName = new Zend_Form_Element_Text('company_name');
    $companyName->setLabel('Company Name');

    $companyName->setRequired(true);
    $companyName->setAttrib('size', 30)
                ->setAttrib('class', 'required');



    $companyDescription = new Zend_Form_Element_TextArea('company_description');
    $companyDescription->setLabel('CompanyDescription')
            ->setAttrib('cols', '10')
            ->setAttrib('rows', '3')
            ->setAttrib('style', 'resize: none;')
            ->setAttrib('class', 'required');
    $companyAddress = new Zend_Form_Element_Text('company_address');
    $companyAddress->setLabel('Company Address');

    $companyAddress->setRequired(true);
    $companyAddress->setAttrib('size', 30)
                    ->setAttrib('class', 'required');

    $file = new Zend_Form_Element_File('uploaded_file');
    echo PUBLIC_PATH;
    $file->setDestination(PUBLIC_PATH . '/fold/')
          ->setLabel('File')
          ->setRequired(true)
          ->addValidator('NotEmpty');

    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setAttrib('class', 'all-button');


    $this->addElements(array($companyName, $companyDescription, $companyAddress,$file,$submit));


    $this->setDecorators(array(
        'FormElements',
        array('HtmlTag', array('tag' => 'table', 'id' => 'employees')),
        'Form'
    ));

    $this->setElementDecorators(array(
        'ViewHelper',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'odd')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
    ));

    //Adding Browse

    $file->setDecorators(array(
        'File',
        array(array('data' => 'HtmlTag'), array('tag' => 'td')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
    ));

    $this->setDecorators(array('FormElements',
        array('HtmlTag',array('tag'=>'table','id'=>'employees1')),'Form'));


    $submit->setDecorators(array('ViewHelper',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array(array('emptyrow' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element', 'placement' => 'PREPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
    ));

动作代码addCompany:

      public function addCompanyAction() {
    $userInfo = Zend_Auth::getInstance()->getStorage()->read();
    $privilege_id = $userInfo->privilege_id;
    $this->view->privilege = $privilege_id;
    $form = new Company_Form_AddCompany();

     $form->setMethod(Zend_Form::METHOD_POST)
           ->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART);


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

    if ($this->_request->isPost()) {
        //echo 'outside post';
        $formData = $this->_request->getPost();
         $location = $form->uploaded_file->getFileName();
         var_dump($location);
        var_dump($formData);
        if ($form->isValid($formData)) {
             $company_name = $this->_getParam('company_name', 0);
             $company_des = $this->_getParam('company_description', 1);
             $company_addr = $this->_getParam('company_address', 2);
            if ($form->uploaded_file->receive()) {
                $location = $form->uploaded_file->getFileName();
                    echo 'Loc: '.$location.'     ';
                    $loc = PUBLIC_PATH . '/fold/';

                    if (rename($location, $loc . $company_name . '.jpg')) {
                        $img_path = $loc . $company_name . '.jpg';
                    }
                    else
                    {
                         $img_path =$location;
                    }

                        //Insert values in table
                    $companyTable = new Project_Model_DbTable_Company();
                        $data = array(
                            'company_name' => $company_name,
                            'company_description' => $company_des,
                            'image_url' => $img_path,
                            'company_address' => $company_addr
                        );

                        $object_company->insert($data);
                        $message="Company added successfully";
                    }



        } else {
            echo 'else is Valid(): ';
            var_dump( $form->getMessages());
            $form->populate($formData);
        }
    }
}

视图脚本的代码add-company.phtml:

   <?php 
     $this->headScript()->appendFile('/js/calendar_db.js', 'text/javascript');
     $this->headLink()->prependStylesheet('/css/calendar.css');
     $this->headScript()->appendFile('/js/validatious-custom-0.9.1.min.js', 'text/javascript');


      ?>
    <div class ="menu">

<?php
require_once '/Menus/setMenuDetails.php';
$privilege_id = $this->privilege;
require_once setMenuDetails ::setMenuDetail($privilege_id);
?>
 </div>
  <div class="page-title" align="center" >Add New Company</div> 
 <div class="back" title="Back"  onclick="history.go(-1);" style="cursor: pointer; "></div>
    <form name="AddCompany" action="/Company/Company/add-company" method="post" class="validate"> 
<div class="profile-details" style="border: 0px solid red; height:700px;width:700px">

    <table id="project"style="text-align: left; margin-top: 5px;" cellpadding="4" cellspacing="3" >


        <thead class ="tableHead">

        </thead>
        <tbody>


            <tr>
                <td><?php echo $this->form->company_name; ?></td>
            </tr>
            <tr>
       <td><?php echo $this->form->company_description; ?></td>
            </tr>

            <tr><td><?php echo $this->form->company_address; ?></td></tr>

    <tr>
                <td><?php  echo $this->form->uploaded_file; ?></td>
            </tr>








   <div class="shadow" style="height: 10px;margin-left:-200px;margin-top:20px">

</div>  


 <div class="page-title" align="center" style="margin-top:30px;margin-left:-          130px;height:20px;border: 0px solid aqua">-->
    <?php

    echo $this->form->submit;
    ?> 
 </tbody>

问题是当我点击提交按钮时表单数据未经验证意味着是否($ form-&gt; isValid($ formData))返回false为什么? 当我在表单中擦除文件部分时,表单数据被验证 帮帮我...

2 个答案:

答案 0 :(得分:0)

您需要检查表单对象上的错误堆栈,线索将在那里。

在您看来,您单独回应表单元素,尝试做...

echo $this->form;

默认情况下,这将产生在相应元素旁边发生的任何错误。

答案 1 :(得分:0)

您可以在表单内创建一些函数,看看:

public function companyDescription()
{
    $companyDescription = new Zend_Form_Element_TextArea('company_description');
    $companyDescription->setLabel('CompanyDescription')
        ->setAttrib('cols', '10')
        ->setAttrib('rows', '3')
        ->setAttrib('style', 'resize: none;')
        ->setAttrib('class', 'required');

    return $companyDescription
}

public function companyAddress()
{
    $companyAddress->setRequired(true);
    $companyAddress->setAttrib('size', 30)
       ->setAttrib('class', 'required');

    return companyAddress;
}

在您的控制器中,您可以这样做:

$this->view->companyDescription = companyDescription;
$this->view->companyAddress = companyAddress;

等等...

在你看来:

<?php 
   echo $this->companyDescription; 
   echo $this->companyAddress;
?>

有了这个,您可以在视图中单独添加每个元素。