Skip to content

Commit 994615b

Browse files
authored
Merge pull request #14 from vint47/master
add nullable to checkbox
2 parents 09f5716 + 1f1d09b commit 994615b

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/Form/Element/Checkbox.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
class Checkbox extends Element
1414
{
15-
protected $options = [];
15+
protected $options = [];
16+
protected $nullable = false;
1617

1718
public function view()
1819
{
@@ -25,12 +26,28 @@ public function view()
2526
'checked' => $this->value,
2627
'options' => $this->options,
2728
'class' => $this->class,
28-
'attributes' => $this->attributes
29+
'attributes' => $this->attributes,
30+
'nullable' => $this->nullable,
2931
]);
3032
}
3133

3234
public function value()
3335
{
34-
return $this->value ? 1 : 0;
36+
$value = null;
37+
38+
if ($this->value) {
39+
$value = 1;
40+
} elseif (!$this->nullable) {
41+
$value = 0;
42+
}
43+
44+
return $value;
45+
}
46+
47+
public function nullable($nullable = true)
48+
{
49+
$this->nullable = $nullable;
50+
51+
return $this;
3552
}
3653
}

src/resources/views/default/element/checkbox.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<div class="form__element form__element_type_checkbox @if($error) form__element_error @endif {{$class}}">
22
<label class="form__label">
3-
<input type="hidden" value="0" name="{{ $elementName }}">
3+
@if (!$nullable)
4+
<input type="hidden" value="0" name="{{ $elementName }}">
5+
@endif
46
<input @foreach($attributes as $attributeName => $attributeValue) {{ $attributeName }}="{{ $attributeValue }}" @endforeach type="checkbox" value="1" @if($checked) checked="checked" @endif name="{{ $elementName }}"> <span>{{ $label }}</span>
57
<div class="form__help">{{ $help }}</div>
68
<div class="form__feedback">{{ $error }}</div>

0 commit comments

Comments
 (0)