我遇到了ZendFramework 2,annotationbuilder和fileupload的问题。 对于联系表格,我希望用户选择上传文件。
除了上传文件外,我得到了一切工作,如果没有文件可以获得 错误: 文件未上传
我正在使用annotationbuilder来创建表单。一些注释被切除,有一个测试空间。但没有帮助。
Annotation类:
<?php
/**
* @Annotation\Name("message")
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
* @ORM\Entity
* @ORM\Table(name="contact_message")
*/
class Message {
/**
* @Annotation\Exclude()
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @var integer
*/
private $id;
/**
* @Annotation\Type("Zend\Form\Element\Select")
* @Annotation\Flags({"priority": 600})
* @Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StringTrim"})
* @ Annotation\Validator({"name":"StringLength"})
* @Annotation\Options({"label":"About:"})
* @Annotation\Attributes({"options":{"1":"PlaceHolder","2":"Test"}})
*
* @ORM\Column(type="string")
* @var String
*/
private $about;
/**
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Flags({"priority": 500})
* @Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StripTags"})
* @ Annotation\Validator({"name":"EmailAddress"})
* @Annotation\Options({"label":"Name:"})
* @Annotation\Attributes({"required": true,"placeholder": "Your name ... "})
*
* @ORM\Column(type="string")
* @var String
*/
private $name;
/**
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Flags({"priority": 500})
* @Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StripTags"})
* @ Annotation\Validator({"name":"EmailAddress"})
* @Annotation\Options({"label":"Subject:"})
* @Annotation\Attributes({"required": true,"placeholder": "Subject ... "})
*
* @ORM\Column(type="string")
* @var String
*/
private $subject;
/**
* @Annotation\Type("Zend\Form\Element\File")
* @Annotation\Flags({"priority": 500})
* @ Annotation\Required({"required":false })
* @ Annotation\Filter({"name":"StringTrim","filerenameupload":{"target": "./img","randomize":true}})
* @ Annotation\Filter({"name":"StripTags"})
* @ Annotation\Validator({"name":"StringLength"})
* @Annotation\Options({"label":"File:"})
* @Annotation\Attributes({"required": false})
*
* @ORM\Column(type="string")
* @var String
*/
private $file;
/**
* @Annotation\Type("Zend\Form\Element\Textarea")
* @Annotation\Flags({"priority": 500})
* @ Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StripTags"})
* @ Annotation\Validator({"name":"EmailAddress"})
* @Annotation\Options({"label":"Message:"})
* @Annotation\Attributes({"required": true,"placeholder": "Message ... "})
*
* @ORM\Column(type="string")
* @var String
*/
private $message;
/**
* WARNING USING THESE IS NOT SAFE. there is no checking on the data and you need to know what
* you are doing when using these.
* But it a great function for lazy people ;)
*
* @param ANY $value
* @param ANY $key
* @return $value
*/
public function __set($value,$key){
return $this->$key = $value;
}
/**
* WARNING USING THESE IS NOT SAFE. there is no checking on the data and you need to know what
* you are doing when using these.
* But it a great function for lazy people ;)
*
* @param ANY $value
* @param ANY $key
* @return $value
*/
public function __get($key){
return $this->$key;
}
/**
* WARNING USING THESE IS NOT SAFE. there is no checking on the data and you need to know what
* you are doing when using these.
* This is used to exchange data from form and more when need to store data in the database.
* and again ist made lazy, by using foreach without data checks
*
* @param ANY $value
* @param ANY $key
* @return $value
*/
public function populate($array){
foreach ($array as $key => $var){
$this->$key = $var;
}
}
/**
* Get an array copy of object
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars($this);
}
}
?>
并在我的控制器中添加Action:
<?php
use Zend\View\Model\ViewModel;
use Zend\Form\Annotation\AnnotationBuilder;
use Contact\Entity\Contact;
use Contact\Entity\Company;
use Contact\Entity\Message;
use Contact\Controller\EntityUsingController;
class MessageController extends EntityUsingController {
public function addAction(){
$message = new Message();
$builder = new AnnotationBuilder();
$form = $builder->createForm($message);
$form->bind($message);
$request = $this->getRequest();
if ($request->isPost()) {
$form->bind($message);
$requestData = array_merge_recursive((array) $request->getPost(),(array) $request->getFiles());
$form->setData($requestData);
var_dump($request->getFiles());
if ($form->isValid()) {
$em = $this->getEntityManager();
$em->persist($message);
$em->flush();
$this->flashMessenger()->addMessage('Contact Saved');
return $this->redirect()->toRoute('contact');
}
}
return new ViewModel(array(
'form' => $form
));
}
private function storeFile($file){
if (!$this->getConfiguration('fileupload')){
return null;
}
$fileBank = $this->getServiceLocator()->get('FileRepository');
$entity = $fileBank->save('/tmp/myfile.jpg');
}
}
?>
我希望有人能帮我解决这个问题。
答案 0 :(得分:1)
这似乎是注释构建器中的错误。 我发现网上有很多地方证实我应该是正确的。 所以我最后通过在构建表单后添加文件输入来破解它。
if ($this->getConfiguration('fileupload')){
$companyForm->add(array(
'name' => 'file',
'priority' => 300,
'type' => 'file',
'options' => array(
'label' => 'File:',
),
),array('priority' => 300));
}
检查配置是否允许文件上传。我现在添加了一个额外的功能,我无法将文件上传设置为注释
答案 1 :(得分:0)
如果您仍然对另一种方式感兴趣:
您可以尝试AllowEmpty注释:
* @Annotation\AllowEmpty(true)
或者您可以尝试在控制器中禁用该字段的验证器:
$form->getInputFilter()->remove('file');