Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 113 additions & 35 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ class Form
protected $action;
protected $theme;
protected $class;
protected $elements = [];
protected $errors = [];
protected $elements = [];
protected $errors = [];
protected $validated = false;
protected $name = false;
protected $isAjax = false;
protected $name = false;
protected $isAjax = false;
protected $enctype;
protected $disabled = false;

public function __construct()
{
Expand All @@ -44,47 +45,81 @@ public static function form()
public function method($method = null)
{
$this->method = $method;

return $this;
}

public function isAjax($isAjax)
{
$this->isAjax = $isAjax;

return $this;
}

public function route($routeName, $parameters = [])
{
$this->action = route($routeName, $parameters);

return $this;
}

public function action($action) {
public function action($action)
{
$this->action = $action;

return $this;
}

public function view()
{
return view('form::' . $this->theme . '.form', [
'elements' => $this->elements,
'method' => $this->method,
'action' => $this->action,
'name' => $this->name,
'class' => $this->class,
'isAjax' => $this->isAjax,
'enctype' => $this->enctype,
'method' => $this->method,
'action' => $this->action,
'name' => $this->name,
'class' => $this->class,
'isAjax' => $this->isAjax,
'enctype' => $this->enctype,
]);
}

public function disable()
{
$this->disabled = true;

return $this;
}

public function isDisabled()
{
return $this->disabled;
}

public function render()
{
$this->setupFormName();
$this->addElementHidden($this->name)->setValue($this->name);
return new HtmlString($this->view()->render());
$this->disableIfNeeded();
$this->addElementHidden($this->name)
->setValue($this->name);

return new HtmlString($this->view()
->render());
}

public function isSubmitted() {
public function disableIfNeeded()
{
if ($this->disabled)
{
foreach ($this->elements as $element)
{
$element->setDisabled(true)
->setIgnored(true);
}
}
}

public function isSubmitted()
{
$this->setupFormName();

return request()->has($this->name) || old($this->name);
Expand All @@ -93,30 +128,35 @@ public function isSubmitted() {
public function setTranslationMask($translationMask = '')
{
$this->translationMask = $translationMask;

return $this;
}

public function setPlaceholderTranslationMask($placeholderTranslationMask = '')
{
$this->placeholderTranslationMask = $placeholderTranslationMask;

return $this;
}

public function setName($name = '')
{
$this->name = $name;

return $this;
}

public function setClass($class = '')
{
$this->class = $class;

return $this;
}

public function addClass($class)
{
$this->class .= ' ' . $class;

return $this;
}

Expand All @@ -131,6 +171,7 @@ public function addElementText($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -145,6 +186,7 @@ public function addElementDateTime($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -159,6 +201,7 @@ public function addElementDate($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -173,6 +216,7 @@ public function addElementRange($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -187,8 +231,8 @@ public function addElementFile($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;
$this->enctype = 'multipart/form-data';
$this->enctype = 'multipart/form-data';

return $this->elements[$name];
}

Expand All @@ -202,15 +246,17 @@ public function addElementDelimiter($name, $view = '', $data = [])
{
$element = new Element\Delimiter($name, '');
$element->setTheme($this->theme)
->setContent($view, $data);
->setContent($view, $data);

$this->elements[$name] = $element;

return $this->elements[$name];
}

public function addElement(Element $element)
{
$this->elements[$element->getName()] = $element;

return $this->elements[$element->getName()];
}

Expand All @@ -227,6 +273,7 @@ public function addElementHtml($name, $content = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -241,6 +288,7 @@ public function addElementTextarea($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -255,6 +303,7 @@ public function addElementHidden($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -269,6 +318,7 @@ public function addElementCheckbox($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -283,6 +333,7 @@ public function addElementCheckboxGroup($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -297,6 +348,7 @@ public function addElementPassword($name, $validators = '')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand Down Expand Up @@ -340,6 +392,7 @@ public function addElementSubmit($name = 'submit')
$element->setTheme($this->theme);

$this->elements[$name] = $element;

return $this->elements[$name];
}

Expand All @@ -351,26 +404,34 @@ public function getElement($name)

public function getElements($names = null)
{
if ($names === null) {
if ($names === null)
{
return $this->elements;
}
if (!is_array($names)) {
if (!is_array($names))
{
$names = func_get_args();
}

return array_only($this->elements, $names);
}

public function validate($force = false)
{
if(!$this->isSubmitted() && !$force) {
if (!$this->isSubmitted() && !$force)
{
return false;
}

if (!$this->validated) {
if (!$this->validated)
{
$keys = array_keys($this->elements);
foreach ($this->elements as $name => $element) {
if(!$element->isIgnored()) {
if (!$element->validate($keys)) {
foreach ($this->elements as $name => $element)
{
if (!$element->isIgnored())
{
if (!$element->validate($keys))
{
array_set($this->errors, $name, $element->error());
}
}
Expand All @@ -383,19 +444,24 @@ public function validate($force = false)

public function errors()
{
if (!$this->validated) {
if (!$this->validated)
{
$this->validate();
}

return $this->errors;
}

public function values($skipEmptyString = false)
{
$data = [];
foreach ($this->elements as $name => $element) {
if(!$element->isIgnored()) {
foreach ($this->elements as $name => $element)
{
if (!$element->isIgnored())
{
$value = $element->value();
if (!$skipEmptyString || $skipEmptyString && $value !== '') {
if (!$skipEmptyString || $skipEmptyString && $value !== '')
{
array_set($data, $name, $value);
}
}
Expand All @@ -411,10 +477,12 @@ public function value($name, $default = null)

public function setValues($values = [])
{
foreach ($this->elements as $name => $element) {
foreach ($this->elements as $name => $element)
{
$value = array_get($values, $name);

if ($value !== null) {
if ($value !== null)
{
$element->setValue($value);
}
}
Expand All @@ -425,22 +493,32 @@ public function setValues($values = [])
public function setTheme($theme = '')
{
$this->theme = $theme;

return $this;
}

public function redirectToRoute($route, $attributes = [])
{
return redirect()->route($route, $attributes)->withErrors($this->errors)->withInput();
return redirect()
->route($route, $attributes)
->withErrors($this->errors)
->withInput();
}

public function redirectBack($additionalErrors = []) {
public function redirectBack($additionalErrors = [])
{
$errors = array_merge($additionalErrors, $this->errors());

return redirect()->back()->withErrors($errors)->withInput();
return redirect()
->back()
->withErrors($errors)
->withInput();
}

private function setupFormName() {
if (!$this->name) {
private function setupFormName()
{
if (!$this->name)
{
$this->name = md5(get_class($this));
}
}
Expand Down
Loading