quantumtecnology/validate-trait
quantumtecnology/validate-trait is a small Laravel/PHP package that adds a reusable validation trait, helping you keep validation rules close to your classes and call them consistently across controllers, models, or services. It reduces duplicated rule arrays and makes common validation easy to shar...
Install the package with Composer:
composer require quantumtecnology/validate-trait
There is no published documentation beyond the README, so the first place to look is the trait source itself:
vendor/quantumtecnology/validate-trait/src/
Open the trait file and identify the public methods it exposes and the properties/config it expects. Treat this file as the spec — for a package this small, the source is the documentation.
Your first use case is almost certainly a controller that currently duplicates validation logic. Add the trait to that controller:
use QuantumTecnology\ValidateTrait\ValidateTrait;
class PostController extends Controller
{
use ValidateTrait;
}
Then convert one inline $request->validate([...]) call to whichever method the trait exposes, and verify the happy path and one failure path (expect a standard ValidationException / 422 response) before rolling it out further. If the package ships a config file, publish it with:
php artisan vendor:publish --tag=validate-trait
Attach at the base controller level. If the trait is generic, put it on App\Http\Controllers\Controller so every child controller inherits the helper. If the package's validation method name risks colliding with Laravel's own ValidatesRequests::validate(), prefer attaching it only to the controllers that need it.
Wrap existing rule arrays, don't rewrite them. The value of a trait like this is convenience, not a new rule syntax. Keep your rules as plain Laravel arrays and pass them through the trait's helper so you retain compatibility with Rule::unique(), Password::defaults(), etc.
Compose with FormRequests rather than replacing them. A healthy split is: FormRequests for complex, reusable validation (multi-step, withValidator() customization); the trait for one-off validation inside a controller action. Mixing both is fine and keeps the trait from becoming a dumping ground.
Centralize shared rule sets. If the trait exposes reusable validation helpers, define the common rule arrays as constants or private methods on a base controller, then call the trait helper with those arrays across index/store/update actions.
API vs. web controllers. For JSON endpoints, confirm the trait surfaces validation failures as ValidationException so Laravel's exception handler returns a 422 with the errors object. If it returns something custom, add a small adapter or register a handler in app/Exceptions/Handler.php.
Test the integration, not the framework. Add one feature test per controller action that hits the endpoint with invalid data and asserts assertSessionHasErrors() (web) or assertJsonValidationErrors() (API). This protects you if the trait's behavior changes on a minor version bump.
ValidatesRequests trait already defines validate(). If this package defines the same method, PHP will fatal with a trait collision. Resolve explicitly with insteadof / as in the use statement, or scope the trait to controllers that don't use ValidatesRequests.composer.json for the illuminate/* version range. On a newer Laravel than the package targets, traits referencing removed helpers (e.g. str_* helpers, older Validator facades) will break silently or throw BadMethodCallException."quantumtecnology/validate-trait": "1.0.x") or vendor the trait file into app/Support/ and drop the dependency. For a 100-line trait, copying it is often cheaper than carrying an unmaintained package.dd() the resolved rule array right before calling the trait — a common cause is the trait merging rules with a config default and overriding your keys.config/validate-trait.php over subclassing the trait; subclassing a trait isn't possible, and aliasing it (use MyValidateTrait { ... }) gets brittle fast.Illuminate\Validation\ValidationException and not a bare \Exception. If it's the latter, wrap calls in a try/catch and rethrow as a ValidationException so Laravel's handler formats it correctly.$this are checked, otherwise typos in method names only surface at runtime.How can I help you explore Laravel packages today?