- How do I install padosoft/laravel-validable?
- Install the package with Composer by running composer require padosoft/laravel-validable in your Laravel project. Then add the Validable trait to the Eloquent model you want to validate. If your Laravel version does not use package auto-discovery, check the package README for any service provider registration steps.
- Which Laravel versions are supported by laravel-validable?
- The package is a Laravel trait for Eloquent models, so support depends on the version constraints declared in its composer.json. Check the repository's composer.json or Packagist page before installing in a specific Laravel application. That is the most reliable way to confirm compatibility with your Laravel and PHP versions.
- How do I define validation rules on a Laravel model with the Validable trait?
- Add the Validable trait to your Eloquent model and define the rules directly on the model, typically in a rules property or method as described in the package README. The trait then uses those rules when the model is saved. This keeps validation close to the data it protects instead of scattering it across controllers.
- Does laravel-validable validate automatically on save and update?
- Yes. The Validable trait is designed to auto-validate an Eloquent model on save, which covers both creating and updating records through Eloquent. When the model is saved, the defined rules are applied before the write proceeds. If validation fails, the save is stopped according to the package's validation behavior.
- How do I customize validation messages for a Validable model?
- Define your custom messages on the model alongside the validation rules, as shown in the package documentation. The trait uses those model-level messages when validation runs on save. This lets you keep field-specific wording close to the model instead of duplicating messages in controllers.
- What happens when validation fails on an Eloquent model using Validable?
- When the model's data does not pass the rules, the trait prevents the save and reports the validation failure. In a typical Laravel setup this surfaces as a validation exception that your application can handle. You should catch or convert that exception in your controller or request layer if you need a custom JSON or redirect response.
- Can I use Validable together with Laravel FormRequest validation?
- Yes, the two approaches can complement each other. FormRequest validation is useful for request-specific input checks, while the Validable trait enforces model-level rules whenever the model is saved. Using both gives you request validation and a data integrity safeguard close to the Eloquent model.
- Does laravel-validable support custom validation rules and Rule objects?
- The trait relies on Laravel's validation system for the rules defined on the model, so standard Laravel rule strings and custom rule classes can generally be used. Confirm the exact supported syntax in the package README or source code. If you rely on advanced rule objects, test them in your application before rolling out widely.
- How do I test a Laravel model that uses the Validable trait?
- Write feature or unit tests that create and save the model with both valid and invalid attribute sets. For invalid data, assert that the expected validation exception is thrown and that the record is not persisted. For valid data, assert that the model saves successfully and the attributes are stored as expected.
- Is padosoft/laravel-validable actively maintained, and what are alternatives?
- Check the GitHub repository for recent commits, releases, and open issues to judge current maintenance. Alternatives include Laravel FormRequest validation, manual Validator calls, or model event listeners that validate before saving. The Validable trait is a good fit when you want rules and messages defined directly on the Eloquent model.