ValidatesRequests mixin
The ValidatesRequests Mixin.
Provides Laravel-style validation capabilities for controllers.
Note: The recommended approach for form validation is to use
FormValidator.rules()in Views withWFormInput. This mixin is useful for API-level validation or when you need controller-side validation for other purposes.
Recommended Pattern (View-side validation)
// In View - use FormValidator.rules() with WFormInput
WFormInput(
controller: _email,
validator: FormValidator.rules([Required(), Email()], field: 'email'),
)
// In Controller - receive clean, typed data
Future<void> attemptLogin({
required String email,
required String password,
}) async {
// No validation needed - data is pre-validated
await Auth.attempt(email, password);
}
Alternative: Controller-side validation
For cases where you need controller-side validation:
class AuthController extends MagicController with ValidatesRequests {
void login(Map<String, dynamic> requestData) {
try {
final data = validate(requestData, {
'email': [Required(), Email()],
'password': [Required(), Min(8)],
});
// Use validated data...
} on ValidationException catch (_) {
// Errors in validationErrors, UI auto-rebuilds
}
}
}
Server-Side Validation Errors
Populate errors from API 422 responses:
final response = await Http.post('/register', data: data);
if (response.isValidationError) {
setErrorsFromResponse(response);
return;
}
Error Display in View
WInput(
states: controller.hasError('email') ? {'error'} : {},
className: 'border error:border-red-500',
),
if (controller.hasError('email'))
WText(controller.getError('email')!, className: 'text-red-500 text-xs'),
- Superclass constraints
- Implemented types
- Mixin applications
Properties
- firstError → String?
-
Get the first validation error message.
no setter
- hasErrors → bool
-
Check if there are any validation errors.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- initialized → bool
-
Whether the controller has been initialized.
no setterinherited
- isDisposed → bool
-
Whether the controller has been disposed.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
validationErrors
↔ Map<
String, String> -
Current validation errors keyed by field name.
getter/setter pair
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
Authorize an action against the current Auth.user via the Gate.
inherited
-
clearErrors(
) → void -
Clear all validation errors.
override
-
clearFieldError(
String field) → void - Clear a specific field's validation error.
-
dispose(
) → void -
Runs onClose if it has not run yet, then releases the notifier.
inherited
-
errorFieldFor(
String wireKey) → String - Map a raw wire validation key to the form field it should report on.
-
getError(
String field) → String? - Get the validation error message for a field.
-
handleApiError(
MagicResponse response, {String? fallback}) → void - Handle API error response automatically.
-
hasError(
String field) → bool - Check if a field has a validation error.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
onClose(
) → void -
Called when the controller is about to be disposed.
inherited
-
onInit(
) → void -
Called when the controller is first created.
inherited
-
refreshUI(
) → void -
Refresh the UI by notifying listeners.
inherited
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
setErrorsFromMap(
Map< String, List< errors) → voidString> > - Set validation errors from a raw wire error map, keyed by errorFieldFor.
-
setErrorsFromResponse(
MagicResponse response) → void - Set validation errors from an API response.
-
toString(
) → String -
A string representation of this object.
inherited
-
validate(
Map< String, dynamic> data, Map<String, List< rules) → Map<Rule> >String, dynamic> - Validate the given data against the rules.
-
validateRequest(
FormRequest request, Map< String, dynamic> data) → Map<String, dynamic> -
Run a FormRequest through this mixin's error bag instead of
FormRequest.validate(). -
validateRequestAsync(
FormRequest request, Map< String, dynamic> data) → Future<Map< String, dynamic> > -
Same contract as validateRequest, but validates through
Validator.validateAsync so any AsyncRule in
request's rules actually runs.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited