1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
4
|
|
|
|
5
|
|
|
use Sco\Admin\Contracts\Validatable; |
6
|
|
|
|
7
|
|
|
class NamedElement extends Element implements Validatable |
8
|
|
|
{ |
9
|
|
|
protected $defaultValue = ''; |
10
|
|
|
|
11
|
|
|
protected $validationRules = []; |
12
|
|
|
|
13
|
|
|
protected $validationMessages = []; |
14
|
|
|
|
15
|
|
|
public function required($message = null) |
16
|
|
|
{ |
17
|
|
|
$this->addValidationRule('required', $message); |
18
|
|
|
return $this; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function unique($message = null) |
22
|
|
|
{ |
23
|
|
|
$this->addValidationRule('unique', $message); |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getValidationRules() |
29
|
|
|
{ |
30
|
|
|
$rules = array_merge( |
31
|
|
|
$this->getDefaultValidationRules(), |
32
|
|
|
$this->validationRules |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
if (isset($rules['unique']) && $rules['unique'] == 'unique') { |
36
|
|
|
$model = $this->getModel(); |
37
|
|
|
$table = $model->getTable(); |
38
|
|
|
|
39
|
|
|
$rule = 'unique:' . $table . ',' . $this->getName(); |
40
|
|
|
if ($model->exists) { |
41
|
|
|
$rule .= ',' . $model->getKey(); |
42
|
|
|
} |
43
|
|
|
$rules['unique'] = $rule; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return [$this->getName() => array_values($rules)]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function getDefaultValidationRules() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
'bail' => 'bail', |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getValidationMessages() |
60
|
|
|
{ |
61
|
|
|
return $this->validationMessages; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getValidationTitles() |
65
|
|
|
{ |
66
|
|
|
return [$this->getName() => $this->getTitle()]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function addValidationRule($rule, $message = null) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->validationRules[$this->getValidationRuleName($rule)] = $rule; |
72
|
|
|
|
73
|
|
|
if (is_null($message)) { |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->addValidationMessage($rule, $message); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function addValidationMessage($rule, $message) |
81
|
|
|
{ |
82
|
|
|
$key = $this->getName() . '.' . $this->getValidationRuleName($rule); |
83
|
|
|
|
84
|
|
|
$this->validationMessages[$key] = $message; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getValidationRuleName($rule) |
90
|
|
|
{ |
91
|
|
|
list($name,) = explode(':', (string)$rule, 2); |
92
|
|
|
return $name; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.