Skip to content

Commit

Permalink
FileElement: Add getValue(), hasValue() and related code
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Sep 8, 2022
1 parent d5f9167 commit cb3b085
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/FormElement/FileElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,4 +24,33 @@ 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()
{
if (empty($this->value)) {
return false;
}

$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;
}
}

0 comments on commit cb3b085

Please sign in to comment.