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
23 changes: 20 additions & 3 deletions src/Form/Element/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

class Checkbox extends Element
{
protected $options = [];
protected $options = [];
protected $nullable = false;

public function view()
{
Expand All @@ -25,12 +26,28 @@ public function view()
'checked' => $this->value,
'options' => $this->options,
'class' => $this->class,
'attributes' => $this->attributes
'attributes' => $this->attributes,
'nullable' => $this->nullable,
]);
}

public function value()
{
return $this->value ? 1 : 0;
$value = null;

if ($this->value) {
$value = 1;
} elseif (!$this->nullable) {
$value = 0;
}

return $value;
}

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

return $this;
}
}
4 changes: 3 additions & 1 deletion src/resources/views/default/element/checkbox.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div class="form__element form__element_type_checkbox @if($error) form__element_error @endif {{$class}}">
<label class="form__label">
<input type="hidden" value="0" name="{{ $elementName }}">
@if (!$nullable)
<input type="hidden" value="0" name="{{ $elementName }}">
@endif
<input @foreach($attributes as $attributeName => $attributeValue) {{ $attributeName }}="{{ $attributeValue }}" @endforeach type="checkbox" value="1" @if($checked) checked="checked" @endif name="{{ $elementName }}"> <span>{{ $label }}</span>
<div class="form__help">{{ $help }}</div>
<div class="form__feedback">{{ $error }}</div>
Expand Down