forked from livewire/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-validation.blade.php
executable file
·336 lines (267 loc) · 9.9 KB
/
input-validation.blade.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
Validation in Livewire should feel similar to standard form validation in Laravel. In short, Livewire provides a `$rules` property for setting validation rules on a per-component basis, and a `$this->validate()` method for validating a component's properties using those rules.
Here's a simple example of a form in Livewire being validated.
@component('components.code-component')
@slot('class')
@verbatim
class ContactForm extends Component
{
public $name;
public $email;
protected $rules = [
'name' => 'required|min:6',
'email' => 'required|email',
];
public function submit()
{
$this->validate();
// Execution doesn't reach here if validation fails.
Contact::create([
'name' => $this->name,
'email' => $this->email,
]);
}
}
@endverbatim
@endslot
@slot('view')
@verbatim
<form wire:submit.prevent="submit">
<input type="text" wire:model="name">
@error('name') <span class="error">{{ $message }}</span> @enderror
<input type="text" wire:model="email">
@error('email') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Save Contact</button>
</form>
@endverbatim
@endslot
@endcomponent
If validation fails, a standard `ValidationException` is thrown (and caught by Livewire), and the standard `$errors` object is available inside the component's view. Because of this, any existing code you have, likely a Blade include, for handling validation in the rest of your application will apply here as well.
You can also add custom key/message pairs to the error bag.
@component('components.code', ['lang' => 'php'])
@verbatim
$this->addError('key', 'message')
@endverbatim
@endcomponent
## Real-time Validation {#real-time-validation}
Sometimes it's useful to validate a form field as a user types into it. Livewire makes "real-time" validation simple with the `$this->validateOnly()` method.
To validate an input field after every update, we can use Livewire's `updated` hook:
@component('components.code-component')
@slot('class')
@verbatim
class ContactForm extends Component
{
public $name;
public $email;
protected $rules = [
'name' => 'required|min:6',
'email' => 'required|email',
];
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
public function saveContact()
{
$validatedData = $this->validate();
Contact::create($validatedData);
}
}
@endverbatim
@endslot
@slot('view')
@verbatim
<form wire:submit.prevent="saveContact">
<input type="text" wire:model="name">
@error('name') <span class="error">{{ $message }}</span> @enderror
<input type="text" wire:model="email">
@error('email') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Save Contact</button>
</form>
@endverbatim
@endslot
@endcomponent
Let's break down exactly what is happening in this example:
* The user types into the "name" field
* As the user types in their name, a validation message is shown if it's less than 6 characters
* The user can switch to entering their email, and the validation message for the name still shows
* When the user submits the form, there is a final validation check, and the data is persisted.
If you are wondering, "why do I need `validateOnly`? Can't I just use `validate`?". The reason is because otherwise, every single update to any field would validate ALL of the fields. This can be a jarring user experience. Imagine if you typed one character into the first field of a form, and all of a sudden every single field had a validation message. `validateOnly` prevents that, and only validates the current field being updated.
## Validating with rules outside of the `$rules` property
If for whatever reason you want to validate using rules other than the ones defined in the `$rules` property, you can always do this by passing the rules directly into the `validate()` and `validateOnly()` methods.
@component('components.code-component')
@slot('class')
@verbatim
class ContactForm extends Component
{
public $name;
public $email;
public function updated($propertyName)
{
$this->validateOnly($propertyName, [
'name' => 'min:6',
'email' => 'email',
]);
}
public function saveContact()
{
$validatedData = $this->validate([
'name' => 'required|min:6',
'email' => 'required|email',
]);
Contact::create($validatedData);
}
}
@endverbatim
@endslot
@endcomponent
## Customize Error Message & Attributes
If you wish to customize the validation messages used by a Livewire component, you can do so with the `$messages` property.
If you want to keep the default Laravel validation messages, but just customize the `:attribute` portion of the message, you can specify custom attribute names using the `$validationAttributes` property.
@component('components.code-component')
@slot('class')
@verbatim
class ContactForm extends Component
{
public $email;
protected $rules = [
'email' => 'required|email',
];
protected $messages = [
'email.required' => 'The Email Address cannot be empty.',
'email.email' => 'The Email Address format is not valid.',
];
protected $validationAttributes = [
'email' => 'email address'
];
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
public function saveContact()
{
$validatedData = $this->validate();
Contact::create($validatedData);
}
}
@endverbatim
@endslot
@endcomponent
If you are not using the global `$rules` validation property, then you can pass custom messages and attributes directly into `validate()`.
@component('components.code-component')
@slot('class')
@verbatim
class ContactForm extends Component
{
public $email;
public function saveContact()
{
$validatedData = $this->validate(
['email' => 'required|email'],
[
'email.required' => 'The :attribute cannot be empty.',
'email.email' => 'The :attribute format is not valid.',
],
['email' => 'Email Address']
);
Contact::create($validatedData);
}
}
@endverbatim
@endslot
@endcomponent
## Direct Error Message Manipulation {#error-bag-manipulation}
The `validate()` and `validateOnly()` method should handle most cases, but sometimes you may want direct control over Livewire's internal ErrorBag.
Livewire provides a handful of methods for you to directly manipulate the ErrorBag.
From anywhere inside a Livewire component class, you can call the following methods:
@component('components.code', ['lang' => 'php'])
@verbatim
// Quickly add a validation message to the error bag.
$this->addError('email', 'The email field is invalid.');
// These two methods do the same thing, they clear the error bag.
$this->resetErrorBag();
$this->resetValidation();
// If you only want to clear errors for one key, you can use:
$this->resetValidation('email');
$this->resetErrorBag('email');
// This will give you full access to the error bag.
$errors = $this->getErrorBag();
// With this error bag instance, you can do things like this:
$errors->add('some-key', 'Some message');
@endverbatim
@endcomponent
## Testing Validation {#testing-validation}
Livewire provides useful testing utilities for validation scenarios. Let's a write a simple test for the original "Contact Form" component.
@component('components.code', ['lang' => 'php'])
/** @test */
public function name_and_email_fields_are_required_for_saving_a_contact()
{
Livewire::test('contact-form')
->set('name', '')
->set('email', '')
->assertHasErrors(['name', 'email']);
}
@endcomponent
This is useful, but we can take it one step further and actually test against specific validation rules:
@component('components.code', ['lang' => 'php'])
/** @test */
public function name_and_email_fields_are_required_for_saving_a_contact()
{
Livewire::test('contact-form')
->set('name', '')
->set('email', '')
->assertHasErrors([
'name' => 'required',
'email' => 'required',
]);
}
@endcomponent
Livewire also offers the inverse of `assertHasErrors` -> `assertHasNoErrors()`:
@component('components.code', ['lang' => 'php'])
/** @test */
public function name_field_is_required_for_saving_a_contact()
{
Livewire::test('contact-form')
->set('name', '')
->set('email', 'foo')
->assertHasErrors(['name' => 'required'])
->assertHasNoErrors(['email' => 'required']);
}
@endcomponent
For more examples of supported syntax for these two methods, take a look at the [Testing Docs](testing).
## Custom validators {#custom-validators}
If you wish to use your own validation system in Livewire, that isn't a problem. Livewire will catch `ValidationException` and provide the errors to the view just like using `$this->validate()`.
For example:
@component('components.code-component')
@slot('class')
@verbatim
use Illuminate\Support\Facades\Validator;
class ContactForm extends Component
{
public $email;
public function saveContact()
{
$validatedData = Validator::make(
['email' => $this->email],
['email' => 'required|email'],
['required' => 'The :attribute field is required'],
)->validate();
Contact::create($validatedData);
}
}
@endverbatim
@endslot
@slot('view')
@verbatim
<div>
Email: <input wire:model.lazy="email">
@if($errors->has('email'))
<span>{{ $errors->first('email') }}</span>
@endif
<button wire:click="saveContact">Save Contact</button>
</div>
@endverbatim
@endslot
@endcomponent
@component('components.warning')
You might be wondering if you can use Laravel's "FormRequest"s. Due to the nature of Livewire, hooking into the http request wouldn't make sense. For now, this functionality is not possible or recommended.
@endcomponent