-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFormControl.php
98 lines (75 loc) · 1.76 KB
/
FormControl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace Laravolt\SemanticForm\Elements;
abstract class FormControl extends Element
{
protected $hasError = false;
protected $value;
public function __construct($name)
{
$this->setName($name);
}
protected function setName($name)
{
$this->setAttribute('name', $name);
}
public function readonly($readonly = true)
{
if ($readonly) {
$this->setAttribute('readonly', 'readonly');
} else {
$this->removeAttribute('readonly');
}
return $this;
}
public function required($required = true)
{
if ($required) {
$this->setAttribute('required', 'required');
} else {
$this->removeAttribute('required');
}
return $this;
}
public function optional()
{
$this->removeAttribute('required');
return $this;
}
public function disable($disable = true)
{
if ($disable) {
$this->setAttribute('disabled', 'disabled');
} else {
$this->removeAttribute('disabled');
}
return $this;
}
public function enable($enable = true)
{
$this->disable(!$enable);
return $this;
}
public function autofocus()
{
$this->setAttribute('autofocus', 'autofocus');
return $this;
}
public function unfocus()
{
$this->removeAttribute('autofocus');
return $this;
}
public function setError($error = true)
{
$this->hasError = $error;
return $this;
}
public function hasError()
{
return $this->hasError;
}
public function getValue()
{
return $this->value ?? $this->getAttribute('value');
}
}