diff --git a/src/FormElement/FileElement.php b/src/FormElement/FileElement.php index 72879af5..abab6993 100644 --- a/src/FormElement/FileElement.php +++ b/src/FormElement/FileElement.php @@ -2,10 +2,15 @@ namespace ipl\Html\FormElement; +use ipl\Html\Form; + class FileElement extends InputElement { protected $type = 'file'; + /** @var array of \GuzzleHttp\Psr7\UploadedFile objects */ + protected $value = []; + public function setValue($value) { return $this; @@ -19,4 +24,29 @@ public function getNameAttribute() return parent::getNameAttribute(); } + + public function getValueAttribute() + { + return $this->getName(); + } + + public function onRegistered(Form $form) + { + $this->value = $form->getRequest()->getUploadedFiles(); + } + + public function hasValue() + { + $file = $this->value[$this->getName()]; + if ($this->getAttributes()->has('multiple')) { + return $file[0]->getError() !== UPLOAD_ERR_NO_FILE; + } + + return $file->getError() !== UPLOAD_ERR_NO_FILE; + } + + public function getValue() + { + return $this->hasValue() ? $this->value[$this->getName()] : null; + } }