Repository navigation
Expand file tree
/
Copy pathFormConfigurationException.php
More file actions
161 lines (146 loc) · 6.25 KB
/
Copy pathFormConfigurationException.php
File metadata and controls
161 lines (146 loc) · 6.25 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace NyonCode\WireForms\Exceptions;
use InvalidArgumentException;
use NyonCode\WireCore\Foundation\Contracts\WireException;
use NyonCode\WireForms\Components\DateTimePicker;
/**
* Thrown when a form is asked to do something its definition does not support.
*
* Always an authoring mistake, caught at the moment the form is used rather
* than left to fail obscurely mid-save.
*/
final class FormConfigurationException extends InvalidArgumentException implements WireException
{
public static function noModel(): self
{
return new self('Form has no model configured. Call ->model() or ->using() before save().');
}
/**
* A field in a native-submit schema cannot render for one.
*
* Named rather than generic because the fix is a choice between two things
* — drop the field, or submit through Livewire after all — and the message
* has to carry which field forced it. See ADR 0036 §2.
*/
public static function cannotSubmitNatively(string $field, string $type): self
{
return new self(
"Field [{$field}] of type [{$type}] cannot render for a native submit: it binds through "
.'wire:model and would post no value. Implement SupportsNativeSubmit on it, remove it from '
.'the schema, or drop ->nativeSubmit() and submit the form through Livewire.'
);
}
/**
* `unique()` had no table to check and no record to take one from — a form
* with no `->model()`, or a standalone field validated on its own.
*/
public static function uniqueWithoutTable(string $field): self
{
return new self(
"Field [{$field}] uses unique() but the form has no model to take a table ".
'from. Pass the table explicitly: ->unique(table: \'users\').'
);
}
public static function unknownFormMethod(string $method, string $component): self
{
return new self("Form method [{$method}()] does not exist on ".$component);
}
public static function builderHasNoTableLayout(string $name): self
{
return new self(
"Builder [{$name}] cannot use table(): the table layout gives one schema ".
'one column per field, and every builder item carries a different block\'s '.
'schema, so there are no shared columns to head.'
);
}
public static function blockIsNotRenderable(string $name): self
{
return new self(
"Block [{$name}] is a Builder block definition and cannot be placed in a ".
'schema directly. Pass it to Builder::make(…)->blocks([…]) instead.'
);
}
public static function unknownBlock(string $name, string $builder): self
{
return new self(
"Builder [{$builder}] has no block named [{$name}]. Check the stored ".
'item type against the blocks the builder declares.'
);
}
/**
* A field that exists to be one mode was asked to become another.
*
* Thrown by the mode-locked picker facades (TimePicker, …), whose class name
* is the promise the mode setter would otherwise break.
*/
public static function fixedPickerMode(string $component, string $mode, string $attempted): self
{
return new self(
"[{$component}] is locked to the [{$mode}] picker mode and cannot be switched to [{$attempted}]. "
.'Use '.DateTimePicker::class.' when the mode has to vary.'
);
}
/**
* A count, length or step that has to be at least one, given as zero or less.
*
* Refused rather than rendered, because every one of these has a shape that
* looks like a working field and is not: `Rating::max(0)` draws no stars,
* `OtpInput::length(0)` draws no boxes, `Slider::step(0)` is rejected by the
* browser and freezes the thumb. The field renders, submits nothing, and the
* developer is left looking at CSS.
*/
public static function boundMustBePositive(string $component, string $method, int|float $given): self
{
return new self(
"[{$component}] was given {$method}({$given}), but that value has to be at least 1. "
.'Zero or a negative count produces a field that renders and cannot be used.'
);
}
/**
* A length or count constraint given as a negative number.
*
* Distinct from the above because zero is meaningful here: `minItems(0)` and
* `maxItems(0)` both say something (no floor, and an empty-only field), while
* a negative bound becomes a validation rule — `min:-1` — that Laravel accepts
* and nothing can ever fail.
*/
public static function boundCannotBeNegative(string $component, string $method, int|float $given): self
{
return new self(
"[{$component}] was given {$method}({$given}), but a negative bound is not a "
.'constraint: it becomes a validation rule every value satisfies. Use 0 for '
.'"no limit at this end", or null to remove the rule entirely.'
);
}
/**
* A minimum above its maximum.
*
* The pair is checked from whichever setter is called second, so the order
* they are written in does not matter. Refused because the two become
* validation rules that contradict each other — `min:10|max:2` is a field no
* input can satisfy, and the first person to find out is whoever fills the
* form in.
*/
public static function invertedBounds(
string $component,
string $lowerMethod,
int|float $lower,
string $upperMethod,
int|float $upper,
): self {
return new self(
"[{$component}] was given {$lowerMethod}({$lower}) and {$upperMethod}({$upper}), "
."but {$lowerMethod} cannot exceed {$upperMethod}: together they describe a value "
.'that cannot exist, so every submission fails validation.'
);
}
public static function mixedFormMethods(string $component, string $method): self
{
return new self(
'Component ['.$component.'] cannot have both form() and '.$method.'() methods. '
.'Use either a single form() method or multiple *Form() methods, not both. '
.'See ADR 0009 for details.'
);
}
}