Permalink
Browse files

WIP on File Upload refactor

  • Loading branch information...
1 parent af1a19b commit efb051e0dfc87d54ea729aee69c81628a84e478d @cgmartin committed Sep 28, 2012
@@ -21,9 +21,12 @@
namespace Zend\Form\Element;
+use Zend\Form\Form;
use Zend\Form\Element;
use Zend\Form\ElementPrepareAwareInterface;
-use Zend\Form\Form;
+use Zend\Form\Exception;
+use Zend\InputFilter\InputProviderInterface;
+use Zend\Stdlib\ErrorHandler;
/**
* @category Zend
@@ -32,7 +35,7 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
-class File extends Element implements ElementPrepareAwareInterface
+class File extends Element implements InputProviderInterface, ElementPrepareAwareInterface
{
/**
* Seed attributes
@@ -54,4 +57,37 @@ public function prepareElement(Form $form)
// Ensure the form is using correct enctype
$form->setAttribute('enctype', 'multipart/form-data');
}
+
+ /**
+ * @return boolean|string
+ */
+ public function getFileName()
+ {
+ $value = $this->getValue();
+ if (!isset($value['error']) || $value['error'] !== UPLOAD_ERR_OK
+ || !isset($value['tmp_name']) || !is_uploaded_file($value['tmp_name'])
+ ) {
+ return false;
+ }
+
+ return $value['tmp_name'];
+ }
+
+ /**
+ * Should return an array specification compatible with
+ * {@link Zend\InputFilter\Factory::createInput()}.
+ *
+ * @return array
+ */
+ public function getInputSpecification()
+ {
+ return array(
+ 'type' => 'Zend\InputFilter\FileInput',
+ 'name' => $this->getName(),
+ 'required' => false,
+ 'validators' => array(
+ array('name' => 'fileupload'),
+ ),
+ );
+ }
}
View
@@ -587,15 +587,17 @@ public function setInputFilter(InputFilterInterface $inputFilter)
*/
public function getInputFilter()
{
- if ($this->object instanceof InputFilterAwareInterface) {
- if (null == $this->baseFieldset) {
- $this->filter = $this->object->getInputFilter();
- } else {
- $name = $this->baseFieldset->getName();
- if (!$this->filter instanceof InputFilterInterface || !$this->filter->has($name)) {
- $filter = new InputFilter();
- $filter->add($this->object->getInputFilter(), $name);
- $this->filter = $filter;
+ if (null === $this->filter) {
+ if ($this->object instanceof InputFilterAwareInterface) {
+ if (null == $this->baseFieldset) {
+ $this->filter = $this->object->getInputFilter();
+ } else {
+ $name = $this->baseFieldset->getName();
+ if (!$this->filter instanceof InputFilterInterface || !$this->filter->has($name)) {
+ $filter = new InputFilter();
+ $filter->add($this->object->getInputFilter(), $name);
+ $this->filter = $filter;
+ }
}
}
}
@@ -11,6 +11,7 @@
namespace Zend\Form\View\Helper;
use Zend\Form\ElementInterface;
+use Zend\Form\Exception;
/**
* @category Zend
@@ -46,4 +47,20 @@ protected function getType(ElementInterface $element)
{
return 'file';
}
+
+ /**
+ * Render a form <input> element from the provided $element
+ *
+ * @param ElementInterface $element
+ * @throws Exception\DomainException
+ * @return string
+ */
+ public function render(ElementInterface $element)
+ {
+ $value = $element->getValue();
+ if (is_array($value) && isset($value['name'])) {
+ $element->setValue($value['name']);
+ }
+ return parent::render($element);
+ }
}
@@ -165,6 +165,7 @@ public function isValid()
if (!array_key_exists($name, $this->data)
|| (null === $this->data[$name])
|| (is_string($this->data[$name]) && strlen($this->data[$name]) === 0)
+ || (isset($this->data[$name]['error']) && $this->data[$name]['error'] === UPLOAD_ERR_NO_FILE)
) {
if ($input instanceof InputInterface) {
// - test if input is required
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_InputFilter
+ */
+
+namespace Zend\InputFilter;
+
+use Zend\Filter\FilterChain;
+use Zend\Validator\ValidatorChain;
+use Zend\Validator\NotEmpty;
+
+/**
+ * @category Zend
+ * @package Zend_InputFilter
+ */
+class FileInput extends Input
+{
+ /**
+ * @var boolean
+ */
+ protected $isValid = false;
+
+ /**
+ * @return mixed
+ */
+ public function getValue()
+ {
+ $filter = $this->getFilterChain();
+ $value = (isset($this->value['tmp_name']))
+ ? $this->value['tmp_name'] : $this->value;
+ if ($this->isValid) {
+ $value = $filter->filter($value);
+ }
+ return $value;
+ }
+
+ /**
+ * @param mixed $context Extra "context" to provide the validator
+ * @return boolean
+ */
+ public function isValid($context = null)
+ {
+ $this->injectNotEmptyValidator();
+ $validator = $this->getValidatorChain();
+ //$value = $this->getValue(); // Do not run the filters yet for File uploads
+ $this->isValid = $validator->isValid($this->getRawValue(), $context);
+ return $this->isValid;
+ }
+
+ protected function injectNotEmptyValidator()
+ {
+ $this->notEmptyValidator = true;
+ // TODO: Could do something like automatically add the Upload validator here
+ }
+}
@@ -33,9 +33,9 @@ class Size extends AbstractValidator
* @var array Error message templates
*/
protected $messageTemplates = array(
- self::TOO_BIG => "Maximum allowed size for file '%value%' is '%max%' but '%size%' detected",
- self::TOO_SMALL => "Minimum expected size for file '%value%' is '%min%' but '%size%' detected",
- self::NOT_FOUND => "File '%value%' is not readable or does not exist",
+ self::TOO_BIG => "Maximum allowed size for file is '%max%' but '%size%' detected",
+ self::TOO_SMALL => "Minimum expected size for file is '%min%' but '%size%' detected",
+ self::NOT_FOUND => "File is not readable or does not exist",
);
/**
@@ -232,24 +232,24 @@ protected function setSize($size)
* Returns true if and only if the file size of $value is at least min and
* not bigger than max (when max is not null).
*
- * @param string $value Real file to check for size
- * @param array $file File data from \Zend\File\Transfer\Transfer
+ * @param string|array $value File to check for size
* @return boolean
*/
- public function isValid($value, $file = null)
+ public function isValid($value)
{
- if ($file === null) {
- $file = array('name' => basename($value));
- }
+ $file = (isset($value['tmp_name'])) ? $value['tmp_name'] : $value;
+ $this->setValue(
+ (isset($value['name'])) ? $value['name'] : basename($file)
+ );
// Is file readable ?
- if (false === stream_resolve_include_path($value)) {
- return $this->throwError($file, self::NOT_FOUND);
+ if (false === stream_resolve_include_path($file)) {
+ $this->error(self::NOT_FOUND);
}
// limited to 4GB files
ErrorHandler::start();
- $size = sprintf("%u", filesize($value));
+ $size = sprintf("%u", filesize($file));
ErrorHandler::stop();
$this->size = $size;
@@ -260,11 +260,11 @@ public function isValid($value, $file = null)
if ($this->getByteString()) {
$this->options['min'] = $this->toByteString($min);
$this->size = $this->toByteString($size);
- $this->throwError($file, self::TOO_SMALL);
+ $this->error(self::TOO_SMALL);
$this->options['min'] = $min;
$this->size = $size;
} else {
- $this->throwError($file, self::TOO_SMALL);
+ $this->error(self::TOO_SMALL);
}
}
@@ -273,11 +273,11 @@ public function isValid($value, $file = null)
if ($this->getByteString()) {
$this->options['max'] = $this->toByteString($max);
$this->size = $this->toByteString($size);
- $this->throwError($file, self::TOO_BIG);
+ $this->error(self::TOO_BIG);
$this->options['max'] = $max;
$this->size = $size;
} else {
- $this->throwError($file, self::TOO_BIG);
+ $this->error(self::TOO_BIG);
}
}
@@ -355,26 +355,5 @@ protected function fromByteString($size)
return $value;
}
- /**
- * Throws an error of the given type
- *
- * @param string $file
- * @param string $errorType
- * @return false
- */
- protected function throwError($file, $errorType)
- {
- if ($file !== null) {
- if (is_array($file)) {
- if (array_key_exists('name', $file)) {
- $this->value = $file['name'];
- }
- } elseif (is_string($file)) {
- $this->value = $file;
- }
- }
- $this->error($errorType);
- return false;
- }
}
Oops, something went wrong.

0 comments on commit efb051e

Please sign in to comment.