Download the PHP package steven-fox/laravel-model-validation without Composer

On this page you can find all versions of the php package steven-fox/laravel-model-validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-model-validation

Salvation for your model validation.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package makes it easy to add validation helpers to your Eloquent models. Some similar packages already exist, but this package aims to achieve flawless functionality in a customizable, Laravel-esque way.

The purpose of this package is to:

  1. Reduce code duplication by providing a single location where the baseline validation configuration is defined for your models.
  2. Make it easy to retrieve that configuration to supplement specific validation scenarios (like FormRequests, admin forms/actions, console input, etc.).
  3. Provide data integrity for applications by ensuring models adhere to a particular set of rules that is independent of UI.

Key Features

Installation

You can install the package via composer:

Usage

The ValidatesAttributes Trait

Add validation functionality to a Model by:

  1. Adding the StevenFox\LaravelModelValidation\ValidatesAttributes trait to the model.
  2. Defining the rules on the model via one or more of the available methods: baseValidationRules(), validationRulesUniqueToCreating(), validationRulesUniqueToUpdating(), validationRulesForCreating(), validationRulesForUpdating().

The ValidatesWhenSaving Interface

You can make a model automatically perform validation when saving by adding the \StevenFox\LaravelModelValiation\Contracts\ValidatesWhenSaving interface. This is an opt-in feature. Without implementing this interface on your individual models, you can still perform validation on command; it simply won't be performed during the save() process automatically.

Validation Listeners

By default, this package will register an event listener for the creating and updating model events that performs the validation prior to saving the model. You can customize this behavior by overloading the static validatingListeners() method on your models. Here is the default implementation that you can adjust to your needs.

Note: We specifically use the creating and updating events over the more general saving event so that we don't redundantly validate a model that is "saved" without any changed attributes (which does NOT fire an updating event, saving us from redundancy).

Note: Keep in mind that the automatic validation process is implemented with Laravel's model event system. Thus, if you perform a saveQuietly() or do something else that disables/halts the model's event chain, you will disable the automatic validation as a consequence.

More Control Over Validation Rules

You can use the following methods to gain finer control over the validation rules used in particular situations.

Unique Columns

Specifying an attribute as unique is a common validation need. Therefore, this package provides a shortcut that you can use in the baseValidationRules() method for your unique columns. The helper function will simply define a Unique rule for the attribute, and when the model record already exists in the database, the rule will automatically invoke the ignoreModel($this) method.

Runtime Customization for Rules

Superseding Rules

You can use the setSupersedingValidationRules() method to set temporary rules that will replace all other rules defined on the model.

Note: You can temporarily disable a specific model instance's validation by setting the supersedingValidationRules to an empty array. The validation process will still run, but with no rules to validate against, the model will automatically pass.

Mixin Rules

You can use the addMixinValidationRules() and setMixinValidationRules() methods to define rules that will be merged with the other rules defined on the model. The rules you mixin for a particular attribute will replace any existing rules for that attribute.

For example, suppose your model specifies that a dateTime column must simply be a date by default, but for a particular situation, you want to ensure that the attribute's value is a date after a particular moment. You can do this by mixing in this custom ruleset for this attribute at runtime.

Validation Data

By default, this package will use the model's getAttributes() method as the data to pass to the validator instance. Normally, the array returned from the getAttributes() method represents the raw values that will be stored within the database. This means attributes with casting will be mutated into the format used for storage, making the validation logic as seamless as possible. For example, most date attributes on models are cast to Carbon instances, but when validating dates, the validator needs to receive the string representation of the date, not a Carbon instance.

If you need to customize the attributes used as data for validation, you can do so in two ways:

  1. Overload the rawAttributesForValidation() method and return what you need.
  2. Overload the prepareAttributesForValidation($attributes) method to transform the default attribute values into a validation-ready state.

Accessing Validation Configuration

You can access a model's validation rules, data, messages, and attribute names using the following methods.

Globally Disabling Validation When Saving

It is possible to disable the automatic validation during the save process for models that implement the ValidatesOnSave interface. This can be helpful when setting up a particular test, for example.

Option 1

Call the static disableValidationWhenSaving() on a validating model class. This will disable validation until you explicitly activate it again. This is similar to the Model::unguard() concept, and like unguarding, you would likely do the disabling of validation in the boot method of a ServiceProvider.

Option 2

Call the static whileValidationDisables() method, passing in a callback that executes the logic you would like to perform while automatic validation is globally disabled. This is similar to the Model::unguarded($callback) concept.

Validation Model Events

This package adds validating and validated model events. It also registers these as "observable events", which means you can listen for them within your model observer classes, like you would for saving, deleting, etc.

When implementing a listener for this event, the model record emitting the event and the related validator instance will be supplied to the callback.

Similar to the other observable model events, this package provides static validating($callback) and validated($callback) methods that you can use to register listeners for these events.

The Validator Instance

You can access the Validator instance that was last used to perform the validate() process with the validator() method.

Note: A new validator instance is instantiated and stored on the model instance each time the validate() method is invoked.

Customizing the Validator

You can customize the validator instance with the beforeMakingValidator() and afterMakingValidator($validator) methods on a model.

Note: The afterMakingValidator() method can be a great place to specify after hooks for your validation process.

You can pass custom validation messages and custom attribute names to the validator via the customValidationMessages() and customValidationAttributeNames() methods respectively.

Validation Exception

When the validate() method is invoked and validation fails, a ModelValidationException is thrown by default. This exception extends Laravel's ValidationException, but stores a reference to the model record that failed validation to make debugging or error messages easier to handle.

You can provide your own validation exception by overloading the validationExceptionClass() or throwValidationException($validator) methods.

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-model-validation with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
spatie/laravel-package-tools Version ^1.16
illuminate/collections Version ^10.0||^11.0
illuminate/contracts Version ^10.0||^11.0
illuminate/database Version ^10.0||^11.0
illuminate/events Version ^10.0||^11.0
illuminate/support Version ^10.0||^11.0
illuminate/validation Version ^10.0||^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package steven-fox/laravel-model-validation contains the following files

Loading the files please wait ....