Zend Framework:使用Zend表单元素上传文件 [英] Zend Framework: Upload file by using Zend Form Element

查看:43
本文介绍了Zend Framework:使用Zend表单元素上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在使用Zend Framework中的Form.它必须成为公司可以填写候选人详细信息的表格.

At this moment I am working on a Form in Zend Framework. It has to become a Form where a company can fill in candidate details.

我对Zend相当陌生,所以这就是我发布问题的原因.

I am pretty new with Zend, so that's why I am posting my question.

这是表单的样子(删除了一些字段以使代码更短)

This is what the form looks like (some fields removed to make the code shorter)

<?php

class Application_Form_Validate_ContactMethodSelected extends Zend_Validate_Abstract {
const INVALID = 'invalid';

protected $_messageTemplates = array(
    self::INVALID => 'Ten minste 1 vorm van contact invullen, telefoon, mobiel of e-mail'
);

public function isValid($value, $context = array())
{
// You need to use your element names, consider making these dynamic
$checkFields = array('telefoon','mobiel','mail');
// Check if all are empty
foreach ( $checkFields as $field ) {
if (isset($context[$field]) && !empty($context[$field])) {

    if (!empty($value)) {
        // This is the element with content... validate as true
        return true;
    }
    // we are going to return false and no error
    // to break validation chain on other empty values
    // This is a quick hack, don't have time to invest in this
    return false;
    }
}

// All were empty, set your own error message
$this->_error(self::INVALID);
return false;
}

}


class Application_Form_Nieuwkandidaat extends Zend_Form {

public function init() {
    $this->setMethod('post');

    $DB = Zend_Db_Table::getDefaultAdapter();

    $id = $this->createElement('hidden', 'id');
    $voornaam = $this->createElement('text', 'voornaam');
    $voornaam->setLabel('Voornaam:')
            ->setAttrib('size', 50)->addValidator('StringLength', false, array(2, 30))
            ->setRequired(true);
    $telefoon = $this->createElement('text', 'telefoon');
    $telefoon->setLabel('Telefoon:')
            ->setAttrib('size', 50)->setAllowEmpty(false)
            ->addValidator(new Application_Form_Validate_ContactMethodSelected(), true)
            ->addValidator('StringLength', false, array(10, 10));
    $mobiel = $this->createElement('text', 'mobiel');
    $mobiel->setLabel('Mobiel:')
            ->setAttrib('size', 50)->setAllowEmpty(false)
            ->addValidator(new Application_Form_Validate_ContactMethodSelected(), true)
            ->addValidator('StringLength', false, array(10, 10));
    $mail = $this->createElement('text', 'mail');
    $mail->setLabel('E-mail:')
            ->setAttrib('size', 50)->setAllowEmpty(false)
            ->addValidator(new Application_Form_Validate_ContactMethodSelected(), true)
            ->addValidator('StringLength', false, array(6, 40))->addValidator('EmailAddress', true);
    $register = $this->createElement('submit', 'register');
    $register->setLabel("Verstuur")
            ->setIgnore(true);
    $reset = $this->createElement('reset', 'reset');
    $reset->setLabel("Reset")
            ->setIgnore(true);
    $this->addElements(array(
        $voornaam,
        $mobiel,
        $telefoon,
        $mail,
        $id,
    ));

    $this->addDisplayGroup(array(
        'voornaam',
        'mobiel',
        'telefoon',
        'mail',
        'telefoon'
            ), 'contacts', array('legend' => 'Contact Informatie'));

    $contacts = $this->getDisplayGroup('contacts');
    $contacts->setDecorators(array(
        'FormElements',
        'Fieldset',
        array('HtmlTag', array('tag' => 'div', 'style' => 'width:50%;;float:left;'))
    ));

    $this->setDecorators(array(
        'FormElements',
        array('HtmlTag', array('tag' => 'div', 'style' => 'width:98%')),
        'Form'
    ));

    $this->addElement($register);
}

}

如何添加一个表单元素,以便在其中选择文件并将其上传,因此它将被存储(例如,在/application/tmp中)

How can I add a form element where I can select a file and upload it, so it will be stored (let's say for example in /application/tmp)

如果我必须从控制器中放置代码,请也告诉我

If I have to place the code from my controller, please let me know as well

提前谢谢!

推荐答案

首先,您应该使用扩展 Zend_Form ,而不是扩展 Zend_Validate_Abstract init()方法,您可以在其中创建表单元素:

First of all, rather than extending Zend_Validate_Abstract, you should be extending Zend_Form with an init() method where you can create your form elements:

<?php

class Application_Form_ContactMethodSelected extends Zend_Form
{
    public function init()
    {
    }
}

现在您可以使用实际的表单了,您可以添加文件上传元素之类的内容:

Now that you have an actual form to work with, you can add things like your File upload element:

<?php

class Application_Form_ContactMethodSelected extends Zend_Form
{
    public function init()
    {
        $this->setAttrib('enctype', 'multipart/form-data');

        $file = new Zend_Form_Element_File('file');
        $file->setLabel('File')
            ->setDestination(APPLICATION_PATH . '/tmp')
            ->setRequired(true);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Upload');

        $this->addElements(array($file, $submit));
    }
}

现在您有了包含某些元素的表单,可以在控制器中使用它:

Now that you have a form with some elements, you can use it in your controller:

<?php

class IndexController extends Zend_Controller_Action 
{
    public function indexAction() 
    {
        $form = new Application_Form_ContactMethodSelected();

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

                // success - do something with the uploaded file
                $uploadedData = $form->getValues();
                $fullFilePath = $form->file->getFileName();

                Zend_Debug::dump($uploadedData, '$uploadedData');
                Zend_Debug::dump($fullFilePath, '$fullFilePath');

                echo "done";
                exit;

            } else {
                $form->populate($formData);
            }
        }

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

在查看脚本中,您可以显示表单:

And in your view script, you can display your form:

<h1>My upload form</h1>
<?php echo $this->form; ?>

这篇关于Zend Framework:使用Zend表单元素上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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