FormRequest class abstract

Laravel-style request object — collapses the "authorize, normalize, validate" ceremony every controller hand-rolls into a single class.

Subclass FormRequest, declare rules, optionally override authorize for access checks and prepared for input normalization (trim, slugify, merge defaults). Pass the raw form payload to validate and it returns the prepared, rule-approved data:

class StoreMonitorRequest extends FormRequest {
  StoreMonitorRequest(this.actor);

  final User actor;

  @override
  bool authorize() => actor.can('monitor.create');

  @override
  Map<String, dynamic> prepared(Map<String, dynamic> data) => {
    ...data,
    'slug': slugify(data['name'] as String? ?? ''),
  };

  @override
  Map<String, List<Rule>> rules() => {
    'name': [Required(), Max(120)],
    'slug': [Required()],
  };
}

// In the controller:
final payload = StoreMonitorRequest(Auth.user()!).validate(form.data);
await Http.post('/monitors', data: payload);

Failure modes:

Constructors

FormRequest()
const

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

authorize() bool
Authorization gate — override to return false to block the request.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
prepared(Map<String, dynamic> data) Map<String, dynamic>
Normalize the incoming payload before validation runs. Default is a pass-through. Use this for trim/slugify/inject-defaults work so rules always see consistent input.
rules() Map<String, List<Rule>>
The rules to apply to the prepared payload.
toString() String
A string representation of this object.
inherited
validate(Map<String, dynamic> data) Map<String, dynamic>
Run authorize → prepare → validate. Returns the prepared payload, filtered to the keys declared in rules (same contract as Validator.validate).

Operators

operator ==(Object other) bool
The equality operator.
inherited