5

私はこれを行うZend Formを取得しました:

 class Products_AddForm extends Zend_Form {
  public function init() {

    $ProductImage1 = $mainform->addElement('file', 'Productimage1',     array(
                               'validators' => array(
                                    array('Count', false, '1' ),
                                    array('Size', false, '10MB'),
                                    array('Extension', false, 'jpg,jpeg,tif,eps'),
                                ),
                                'required'   => false,
                                'label' => 'Product Image1 (jpg/tif/eps)'
            ));

次に、投稿データをチェックするコントローラー:

public function addAction()
{

    $form = $this->getAddForm();

    if($this->getRequest()->isPost()){

        $post =  $this->getRequest()->getPost();

        // check post data
        if($form->isValid($post) )
        { 
        }
        else {
            print_r($form->getErrors());
            print_r($form->getErrorMessages());
            print_r($form->getMessages());
        }

そして、次のようなカスタムView Controller:

echo '<form method="post" action="'.$this->baseUrl('products/add').'" enctype="multipart/form-data">';

            $image1 = $form->getElement('Productimage1');

            $helper1 = $image1->helper;
            echo '<br/>'.$this->translate('Productimage').' (jpg/tif):<br/>'.$bild1->getView()->$helper1(
            $image1->getName(),
            $image1->getValue(),
            $image1->getAttribs(),
            $image1->options
            );

テキストフィールドに情報を入力して投稿すると、$form->isValid() が false になります。フォームとカスタム ビューからファイル パーツを削除すると、完全に機能します。

Formelement は「必須」false です。さらに、コントローラーの false 部分 -> getErrors()、getMessages()、および getErrorMessages() がエラーを返さない?!

ここで何が起こっているか知っている人はいますか?

4

1 に答える 1

2

次のようにしてみてください: これはあなたのフォームです:

   $image = $this->createElement('file','image');
            $image->setLabel('Product Image1 (jpg/tif/eps) ')
                  ->setRequired(false)
                  ->setDestination(??)
                  ->addValidator('Count',false,1)
                  ->addValidator('Size',false,10MB)
                  ->addValidator('Extension',false,'jpg,tif,eps');
            $this->addElement($image);

これはあなたのコントローラーです:

$form = new Application_Form_YOURFORM(array('action' => '', 'method' => 'POST'));
        if($this->_request->isPost() && $form->isValid($this->_request->getPost()))
        {
            if($form->image->isUploaded())
            {
                $form->image->receive();
                $this->image = 'destination' . basename($form->image->getFileName());
            }
            $this->_helper->redirector('..');
        }
        else
        {
            $this->view->form = $form;
        }

あなたの見解:

<?php echo $this->form ?>
于 2013-05-16T10:45:08.813 に答える