Skip to content

Commit 63a1a8c

Browse files
committed
only show relevant validation rule options
1 parent 2935b04 commit 63a1a8c

18 files changed

+252
-81
lines changed

src/Contracts/FieldContract.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public function getForm(): array;
1111
public static function make(string $name, Field $field);
1212

1313
public static function getDefaultConfig(): array;
14+
15+
public function getFieldType(): ?string;
1416
}

src/Fields/Base.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ public function getRulesForm(): array
2525
return [
2626
Forms\Components\Grid::make(2)
2727
->schema([
28-
...ValidationRulesSchema::make(),
28+
...ValidationRulesSchema::make($this->getFieldType()),
2929
...VisibilityRulesSchema::make(),
3030
]),
3131
];
3232
}
3333

34+
public function getFieldType(): ?string
35+
{
36+
// This method should be overridden by specific field classes
37+
// to return their field type
38+
return null;
39+
}
40+
3441
public static function getDefaultConfig(): array
3542
{
3643
return [

src/Fields/Checkbox.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
class Checkbox extends Base implements FieldContract
1111
{
12+
public function getFieldType(): ?string
13+
{
14+
return 'checkbox';
15+
}
16+
1217
public static function getDefaultConfig(): array
1318
{
1419
return [

src/Fields/CheckboxList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class CheckboxList extends Base implements FieldContract
1212
{
1313
use HasOptions;
1414

15+
public function getFieldType(): ?string
16+
{
17+
return 'checkbox-list';
18+
}
19+
1520
public static function getDefaultConfig(): array
1621
{
1722
return [

src/Fields/Color.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
class Color extends Base implements FieldContract
1717
{
18+
public function getFieldType(): ?string
19+
{
20+
return 'color';
21+
}
22+
1823
public static function getDefaultConfig(): array
1924
{
2025
return [

src/Fields/DateTime.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class DateTime extends Base implements FieldContract
1313
{
1414
use HasAffixes;
1515

16+
public function getFieldType(): ?string
17+
{
18+
return 'date-time';
19+
}
20+
1621
public static function getDefaultConfig(): array
1722
{
1823
return [

src/Fields/FormSchemas/ValidationRulesSchema.php

Lines changed: 127 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
class ValidationRulesSchema
1010
{
11-
public static function make(): array
11+
public static function make(?string $fieldType = null): array
1212
{
13+
$validationOptions = self::getValidationOptionsForFieldType($fieldType);
14+
1315
return [
1416
Forms\Components\Section::make('Validation rules')
1517
->collapsible()
@@ -24,68 +26,7 @@ public static function make(): array
2426
->label(__('Rule type'))
2527
->searchable()
2628
->preload()
27-
->options([
28-
'active_url' => __('Active URL'),
29-
'after' => __('After date'),
30-
'after_or_equal' => __('After or equal to date'),
31-
'alpha' => __('Alphabetic'),
32-
'alpha_dash' => __('Alphanumeric with dashes'),
33-
'alpha_num' => __('Alphanumeric'),
34-
'ascii' => __('ASCII'),
35-
'before' => __('Before date'),
36-
'before_or_equal' => __('Before or equal to date'),
37-
'confirmed' => __('Confirmed'),
38-
'date' => __('Date'),
39-
'date_equals' => __('Date equals'),
40-
'date_format' => __('Date format'),
41-
'decimal' => __('Decimal'),
42-
'different' => __('Different from field'),
43-
'email' => __('Email'),
44-
'ends_with' => __('Ends with'),
45-
'enum' => __('Enum'),
46-
'exists' => __('Exists in database'),
47-
'filled' => __('Filled'),
48-
'greater_than' => __('Greater than field'),
49-
'greater_than_or_equal' => __('Greater than or equal to field'),
50-
'hex_color' => __('Hex color'),
51-
'in' => __('In list'),
52-
'integer' => __('Integer'),
53-
'ip' => __('IP Address'),
54-
'ipv4' => __('IPv4 Address'),
55-
'ipv6' => __('IPv6 Address'),
56-
'json' => __('JSON'),
57-
'less_than' => __('Less than field'),
58-
'less_than_or_equal' => __('Less than or equal to field'),
59-
'mac_address' => __('MAC Address'),
60-
'max' => __('Maximum value'),
61-
'max_length' => __('Maximum length'),
62-
'min' => __('Minimum value'),
63-
'min_length' => __('Minimum length'),
64-
'multiple_of' => __('Multiple of'),
65-
'not_in' => __('Not in list'),
66-
'not_regex' => __('Not regex pattern'),
67-
'nullable' => __('Nullable'),
68-
'numeric' => __('Numeric'),
69-
'prohibited' => __('Prohibited'),
70-
'prohibited_if' => __('Prohibited if'),
71-
'prohibited_unless' => __('Prohibited unless'),
72-
'prohibits' => __('Prohibits'),
73-
'regex' => __('Regex pattern'),
74-
'required_if' => __('Required if'),
75-
'required_if_accepted' => __('Required if accepted'),
76-
'required_unless' => __('Required unless'),
77-
'required_with' => __('Required with'),
78-
'required_with_all' => __('Required with all'),
79-
'required_without' => __('Required without'),
80-
'required_without_all' => __('Required without all'),
81-
'same' => __('Same as field'),
82-
'starts_with' => __('Starts with'),
83-
'string' => __('String'),
84-
'ulid' => __('ULID'),
85-
'unique' => __('Unique in database'),
86-
'url' => __('URL'),
87-
'uuid' => __('UUID'),
88-
])
29+
->options($validationOptions)
8930
->reactive()
9031
->required(),
9132
Forms\Components\TextInput::make('parameters.value')
@@ -98,7 +39,7 @@ public static function make(): array
9839
->visible(fn (Forms\Get $get): bool => in_array($get('type'), ['regex', 'not_regex'])),
9940
Forms\Components\TextInput::make('parameters.values')
10041
->label(__('Values (comma-separated)'))
101-
->visible(fn (Forms\Get $get): bool => in_array($get('type'), ['starts_with', 'ends_with', 'in', 'not_in'])),
42+
->visible(fn (Forms\Get $get): bool => in_array($get('type'), ['starts_with', 'ends_with', 'doesnt_start_with', 'doesnt_end_with', 'in', 'not_in'])),
10243
Forms\Components\TextInput::make('parameters.table')
10344
->label(__('Table'))
10445
->required(fn (Forms\Get $get): bool => in_array($get('type'), ['exists', 'unique']))
@@ -168,4 +109,126 @@ public static function make(): array
168109
]),
169110
];
170111
}
112+
113+
private static function getValidationOptionsForFieldType(?string $fieldType): array
114+
{
115+
$allOptions = [
116+
'active_url' => __('Active URL'),
117+
'after' => __('After date'),
118+
'after_or_equal' => __('After or equal to date'),
119+
'alpha' => __('Alphabetic'),
120+
'alpha_dash' => __('Alphanumeric with dashes'),
121+
'alpha_num' => __('Alphanumeric'),
122+
'ascii' => __('ASCII'),
123+
'before' => __('Before date'),
124+
'before_or_equal' => __('Before or equal to date'),
125+
'confirmed' => __('Confirmed'),
126+
'date' => __('Date'),
127+
'date_equals' => __('Date equals'),
128+
'date_format' => __('Date format'),
129+
'decimal' => __('Decimal'),
130+
'different' => __('Different from field'),
131+
'doesnt_start_with' => __('Doesn\'t start with'),
132+
'doesnt_end_with' => __('Doesn\'t end with'),
133+
'email' => __('Email'),
134+
'ends_with' => __('Ends with'),
135+
'enum' => __('Enum'),
136+
'exists' => __('Exists in database'),
137+
'filled' => __('Filled'),
138+
'greater_than' => __('Greater than field'),
139+
'greater_than_or_equal' => __('Greater than or equal to field'),
140+
'hex_color' => __('Hex color'),
141+
'in' => __('In list'),
142+
'integer' => __('Integer'),
143+
'ip' => __('IP Address'),
144+
'ipv4' => __('IPv4 Address'),
145+
'ipv6' => __('IPv6 Address'),
146+
'json' => __('JSON'),
147+
'less_than' => __('Less than field'),
148+
'less_than_or_equal' => __('Less than or equal to field'),
149+
'mac_address' => __('MAC Address'),
150+
'max' => __('Maximum value'),
151+
'max_length' => __('Maximum length'),
152+
'min' => __('Minimum value'),
153+
'min_length' => __('Minimum length'),
154+
'multiple_of' => __('Multiple of'),
155+
'not_in' => __('Not in list'),
156+
'not_regex' => __('Not regex pattern'),
157+
'nullable' => __('Nullable'),
158+
'numeric' => __('Numeric'),
159+
'prohibited' => __('Prohibited'),
160+
'prohibited_if' => __('Prohibited if'),
161+
'prohibited_unless' => __('Prohibited unless'),
162+
'prohibits' => __('Prohibits'),
163+
'regex' => __('Regex pattern'),
164+
'required' => __('Required'),
165+
'required_if' => __('Required if'),
166+
'required_if_accepted' => __('Required if accepted'),
167+
'required_unless' => __('Required unless'),
168+
'required_with' => __('Required with'),
169+
'required_with_all' => __('Required with all'),
170+
'required_without' => __('Required without'),
171+
'required_without_all' => __('Required without all'),
172+
'same' => __('Same as field'),
173+
'starts_with' => __('Starts with'),
174+
'string' => __('String'),
175+
'ulid' => __('ULID'),
176+
'unique' => __('Unique in database'),
177+
'url' => __('URL'),
178+
'uuid' => __('UUID'),
179+
];
180+
181+
if (!$fieldType) {
182+
return $allOptions;
183+
}
184+
185+
$fieldTypeOptions = [
186+
'text' => [
187+
'active_url', 'alpha', 'alpha_dash', 'alpha_num', 'ascii', 'confirmed', 'different', 'doesnt_start_with', 'doesnt_end_with', 'email', 'ends_with', 'filled', 'greater_than', 'greater_than_or_equal', 'in', 'less_than', 'less_than_or_equal', 'max_length', 'min_length', 'not_in', 'nullable', 'numeric', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'regex', 'not_regex', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'starts_with', 'string', 'unique', 'url'
188+
],
189+
'textarea' => [
190+
'alpha', 'alpha_dash', 'alpha_num', 'ascii', 'different', 'doesnt_start_with', 'doesnt_end_with', 'ends_with', 'filled', 'greater_than', 'greater_than_or_equal', 'in', 'less_than', 'less_than_or_equal', 'max_length', 'min_length', 'not_in', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'regex', 'not_regex', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'starts_with', 'string', 'unique'
191+
],
192+
'rich-editor' => [
193+
'different', 'doesnt_start_with', 'doesnt_end_with', 'filled', 'greater_than', 'greater_than_or_equal', 'in', 'less_than', 'less_than_or_equal', 'max_length', 'min_length', 'not_in', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'string', 'unique'
194+
],
195+
'markdown-editor' => [
196+
'different', 'doesnt_start_with', 'doesnt_end_with', 'filled', 'greater_than', 'greater_than_or_equal', 'in', 'less_than', 'less_than_or_equal', 'max_length', 'min_length', 'not_in', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'string', 'unique'
197+
],
198+
'select' => [
199+
'different', 'filled', 'greater_than', 'greater_than_or_equal', 'in', 'less_than', 'less_than_or_equal', 'not_in', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'unique'
200+
],
201+
'radio' => [
202+
'different', 'filled', 'greater_than', 'greater_than_or_equal', 'in', 'less_than', 'less_than_or_equal', 'not_in', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'unique'
203+
],
204+
'checkbox' => [
205+
'different', 'filled', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same'
206+
],
207+
'checkbox-list' => [
208+
'different', 'filled', 'greater_than', 'greater_than_or_equal', 'in', 'less_than', 'less_than_or_equal', 'not_in', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'unique'
209+
],
210+
'toggle' => [
211+
'different', 'filled', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same'
212+
],
213+
'color' => [
214+
'different', 'filled', 'hex_color', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'unique'
215+
],
216+
'date-time' => [
217+
'after', 'after_or_equal', 'before', 'before_or_equal', 'date', 'date_equals', 'date_format', 'different', 'filled', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'unique'
218+
],
219+
'key-value' => [
220+
'different', 'filled', 'json', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'unique'
221+
],
222+
'tags' => [
223+
'different', 'filled', 'greater_than', 'greater_than_or_equal', 'in', 'less_than', 'less_than_or_equal', 'not_in', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same', 'unique'
224+
],
225+
'repeater' => [
226+
'different', 'filled', 'nullable', 'prohibited', 'prohibited_if', 'prohibited_unless', 'prohibits', 'required', 'required_if', 'required_if_accepted', 'required_unless', 'required_with', 'required_with_all', 'required_without', 'required_without_all', 'same'
227+
],
228+
];
229+
230+
$allowedRules = $fieldTypeOptions[$fieldType] ?? array_keys($allOptions);
231+
232+
return array_intersect_key($allOptions, array_flip($allowedRules));
233+
}
171234
}

src/Fields/KeyValue.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
class KeyValue extends Base implements FieldContract
1111
{
12+
public function getFieldType(): ?string
13+
{
14+
return 'key-value';
15+
}
16+
1217
public static function getDefaultConfig(): array
1318
{
1419
return [

src/Fields/MarkdownEditor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
class MarkdownEditor extends Base implements FieldContract
1212
{
13+
public function getFieldType(): ?string
14+
{
15+
return 'markdown-editor';
16+
}
17+
1318
public static function getDefaultConfig(): array
1419
{
1520
return [

src/Fields/Radio.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class Radio extends Base implements FieldContract
1212
{
1313
use HasOptions;
1414

15+
public function getFieldType(): ?string
16+
{
17+
return 'radio';
18+
}
19+
1520
public static function getDefaultConfig(): array
1621
{
1722
return [

0 commit comments

Comments
 (0)