Download the PHP package mamadali/laravel-validation-models without Composer
On this page you can find all versions of the php package mamadali/laravel-validation-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mamadali/laravel-validation-models
More information about mamadali/laravel-validation-models
Files in mamadali/laravel-validation-models
Package laravel-validation-models
Short Description Helps you easily validate your models and eloquent models with specific scenarios and use them in api
License
Informations about the package laravel-validation-models
Laravel Validation Models
Helps you easily validate your models and eloquent models with specific scenarios and use them in api
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the "require" section of your composer.json
file.
- Basic Usage
- Advanced Usage
- Multiple Models
- Inline Validation
- Scenarios
- Api Response
- Get fields in api
-
Events
Basic Usage
First use trait in your model or eloquent model
in your form use like this
in your eloquent models use like this
then write your validation rules in validateRules() function in your model
and you can load data received from end user to your model and validate data \ ->validate() function return true or false
in eloquent models you can just call $model->save()
in save function call validate function by default \ if you need not call validate function
Advanced Usage
Multiple Models
you can new multiple model from your request data if your data list of models
Inline Validation
you can write your custom validation in your model \ write function name as validate{Attribute}() in your model
Scenarios
you can set multi scenarios for your model like this
and you can pass scenario when create your model
also you can set scenario on your model manually
when load attributes in your model only attributes in scenario set to your model
and you can get scenario currently your model on this
you can handle your validation rules with scenarios like this
Api Response
you can return your model data as response in your api \ in your api controller
in this order if your model has errors your response status code set to 422 and your error message in body \ else return your model attributes with value in body
in some api if you need No Content with 204 status code response you can use this function
you can customize your fields in api response with overwrite fields() function in your model like this
now supported cast type: int, string, float, bool
Get fields in api
you can write extra fields in your model like this
extra fields are not included in the api response by default \ and to get extra fields in the api response you need to add 'expand' parameter to your query params in request
in fields and extraFields method you can return recursively you model fields
\ in this request get all fields in fields() function and fields defined in 'expand' parameter
and you can get specific fields in your response by add 'fields' parameter in query params
\ in this request only get id,title and extraField1
Events
beforeValidate method is invoked before validation starts. \
You may override this method in your model \
If false
is returned, the validation will stop and the model is considered invalid.
afterValidate method is invoked after validation ends. \ You may override this method in your model
beforeSave method is called at the beginning of inserting or updating a record in eloquent models.
Override this method in your eloquent model \
If false
, the insertion or updating will be cancelled.
This method is called at the end of inserting or updating a record in eloquent models. \ Override this method in your eloquent model: