Skip to content

Commit

Permalink
File support
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jan 8, 2021
1 parent c1a2983 commit 6f74be7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
22 changes: 21 additions & 1 deletion demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Packaged\Form\DataHandlers\BooleanDataHandler;
use Packaged\Form\DataHandlers\EmailDataHandler;
use Packaged\Form\DataHandlers\EnumDataHandler;
use Packaged\Form\DataHandlers\FileDataHandler;
use Packaged\Form\DataHandlers\HiddenDataHandler;
use Packaged\Form\DataHandlers\IntegerDataHandler;
use Packaged\Form\DataHandlers\MultiValueEnumDataHandler;
Expand Down Expand Up @@ -51,6 +52,9 @@ class DemoForm extends Form

/** @var IntegerDataHandler */
public $age;

/** @var FileDataHandler */
public $profilePicture;
/**
* @var BooleanDataHandler
*/
Expand All @@ -71,17 +75,19 @@ protected function _initDataHandlers()
$this->agree = BooleanDataHandler::i()->setPlaceholder('Do you agree?')->addValidator(new RequiredValidator());
$this->youCantTouchThis = ReadOnlyDataHandler::i()->setValue('Dare You');
$this->age = IntegerDataHandler::i()->setLabel('How old are you?');
$this->profilePicture = FileDataHandler::i()->setLabel("Profile Picture");
$this->setHandlerDecorator(new InputOnlyDataHandlerDecorator(), 'agree');
}
}

$data = $_POST;
$data = array_merge($_POST, $_FILES);
$form = new DemoForm();
if(!empty($data))
{
$form->hydrate($data);
$form->validate();
}

?>
<html>
<head>
Expand Down Expand Up @@ -135,6 +141,20 @@ protected function _initDataHandlers()
<td><?php var_dump($v); ?></td>
</tr>
<?php endforeach; ?>
<?php if($form->profilePicture->hasUpload()): ?>
<tr>
<th>File:</th>
<td>
<h4><?= $form->profilePicture->getFileName(); ?></h4>
<?php $content = file_get_contents($form->profilePicture->getFileLocation()); ?>
<?php if(substr($form->profilePicture->getFileType(), 0, 5) == 'image'): ?>
<img src="data:<?= $form->profilePicture->getFileType() ?>;base64,<?= base64_encode($content); ?>">
<?php else: ?>
<textarea><?= $content; ?></textarea>
<?php endif; ?>
</td>
</tr>
<?php endif; ?>
</table>
</div>
</div>
Expand Down
60 changes: 60 additions & 0 deletions src/DataHandlers/FileDataHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace Packaged\Form\DataHandlers;

use Packaged\Glimpse\Tags\Form\Input;
use Packaged\Ui\Html\HtmlElement;

class FileDataHandler extends AbstractDataHandler
{
public function getFileName()
{
return $this->_value['name'] ?? null;
}

public function getFileType()
{
return $this->_value['type'] ?? null;
}

public function getFileSize()
{
return $this->_value['size'] ?? null;
}

/**
* @link https://www.php.net/manual/en/features.file-upload.errors.php
*/
public function getErrorCode()
{
return $this->_value['error'] ?? null;
}

public function getFileLocation()
{
return $this->_value['tmp_name'] ?? null;
}

public function hasUpload()
{
return isset($this->_value['tmp_name']) && !empty($this->_value['tmp_name']);
}

protected function _createBaseElement(): HtmlElement
{
return Input::create()->setType(Input::TYPE_FILE);
}

protected function _generateInput(): HtmlElement
{
$ele = $this->_createBaseElement();
$ele->addAttributes(
[
'name' => $this->getName(),
'id' => $this->getId(),
'placeholder' => $this->getPlaceholder(),
]
);
return $ele;
}

}
7 changes: 7 additions & 0 deletions src/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Packaged\Form\Form;

use Exception;
use Packaged\Form\DataHandlers\FileDataHandler;
use Packaged\Form\DataHandlers\Interfaces\DataHandler;
use Packaged\Form\DataHandlers\ReadOnlyDataHandler;
use Packaged\Form\Decorators\DefaultDataHandlerDecorator;
Expand Down Expand Up @@ -68,6 +69,12 @@ public function addDataHandler($property, DataHandler $handler)
{
$this->_dataHandlers[$property]->setName($property);
}

if($handler instanceof FileDataHandler)
{
$this->setAttribute('enctype', 'multipart/form-data');
}

return $this;
}

Expand Down

0 comments on commit 6f74be7

Please sign in to comment.