### Install Laminas Content Validation via Composer Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Installs the Laminas Content Validation module using the Composer package manager. This is the recommended method for adding the module to your project. ```bash $ composer require laminas-api-tools/api-tools-content-validation ``` -------------------------------- ### Listen to BEFORE_VALIDATE Event for Data Manipulation (PHP) Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Demonstrates how to listen to the ContentValidationListener::EVENT_BEFORE_VALIDATE event to manipulate input filters or the data being validated. This example modifies the parameter data to include an 'id' from the route match. ```php $events->listen(ContentValidationListener::EVENT_BEFORE_VALIDATE, function ($e) { if ($e->getController() !== MyRestController::class) { return; } $matches = $e->getRouteMatch(); $data = $e->getParam('Laminas\ApiTools\ContentValidation\ParameterData') ?: []; $data['id'] = $matches->getParam('id'); $e->setParam('Laminas\ApiTools\ContentValidation\ParameterData', $data); }); ``` -------------------------------- ### System Configuration for Laminas API Tools Content Validation (PHP) Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Defines the module's system configuration, including controller plugins, input filters, and service manager factories for Laminas applications. ```php namespace Laminas\ApiTools\ContentValidation; use Laminas\InputFiler\InputFilterAbstractServiceFactory; use Laminas\ServiceManager\Factory\InvokableFactory; return [ 'controller_plugins' => [ 'aliases' => [ 'getinputfilter' => InputFilter\InputFilterPlugin::class, 'getInputfilter' => InputFilter\InputFilterPlugin::class, 'getInputFilter' => InputFilter\InputFilterPlugin::class, ], 'factories' => [ InputFilter\InputFilterPlugin::class => InvokableFactory::class, ], ], 'input_filters' => [ 'abstract_factories' => [ InputFilterAbstractServiceFactory::class, ], ], 'service_manager' => [ 'factories' => [ ContentValidationListener::class => ContentValidationListenerFactory::class, ], ], 'validators' => [ 'factories' => [ 'Laminas\ApiTools\ContentValidation\Validator\DbRecordExists' => Validator\Db\RecordExistsFactory::class, 'Laminas\ApiTools\ContentValidation\Validator\DbNoRecordExists' => Validator\Db\NoRecordExistsFactory::class, ], ], ]; ``` -------------------------------- ### Configuring Content Validation Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Defines how to map controller service names to input filters for different HTTP methods, including default and method-specific validation. ```APIDOC ## Service Name Key Configuration ### Description Specifies the mapping between controller service names and input filters for API request validation. Keys can be HTTP methods (`POST`, `PUT`, `PATCH`, `DELETE`) or `input_filter` for a default filter. ### Method N/A (Configuration) ### Endpoint N/A (Configuration) ### Parameters #### Request Body - **api-tools-content-validation** (array) - An associative array where keys are controller service names. - **[ControllerServiceName]** (array) - Contains validation configurations for a specific controller. - **input_filter** (string) - The service name of the default input filter. - **POST** (string) - The service name of the input filter for POST requests. - **PUT** (string) - The service name of the input filter for PUT requests. - **PATCH** (string) - The service name of the input filter for PATCH requests. - **DELETE** (string) - The service name of the input filter for DELETE requests. - **GET** (string) - The service name of the input filter for GET requests (since 1.3.0). - **POST_COLLECTION** (string) - The service name of the input filter for POST collection requests (since 1.5.0). - **PUT_COLLECTION** (string) - The service name of the input filter for PUT collection requests (since 1.5.0). - **PATCH_COLLECTION** (string) - The service name of the input filter for PATCH collection requests (since 1.5.0). - **DELETE_COLLECTION** (string) - The service name of the input filter for DELETE collection requests (since 1.6.0). - **use_raw_data** (boolean) - If false, use filtered data; otherwise, use raw data (default). - **allows_only_fields_in_filter** (boolean) - If true and `use_raw_data` is false, merges only fields present in the filter. - **remove_empty_data** (boolean) - If true, removes empty data from the payload. ### Request Example ```php 'api-tools-content-validation' => [ 'Application\Controller\HelloWorld' => [ 'input_filter' => 'Application\Controller\HelloWorld\Validator', 'POST' => 'Application\Controller\HelloWorld\CreationValidator', ], ] ``` ### Response N/A (Configuration) ``` -------------------------------- ### Enable Laminas Content Validation Module in application.config.php Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Enables the Laminas Content Validation module by adding its name to the 'modules' array in your application's configuration file. This makes the module's features available to your application. ```php return [ /* ... */ 'modules' => [ /* ... */ 'Laminas\ApiTools\ContentValidation', ], /* ... */ ]; ``` -------------------------------- ### API Tools Content Validation - Input Filter Specification (PHP) Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Defines input filter specifications for configuration-driven creation of input filters. Each entry maps a service name to an input filter configuration array, detailing fields, filters, validators, and other properties. ```php 'input_filter_specs' => [ 'Application\Controller\HelloWorldGet' => [ 0 => [ 'name' => 'name', 'required' => true, 'filters' => [ 0 => [ 'name' => 'Laminas\Filter\StringTrim', 'options' => [], ], ], 'validators' => [], 'description' => 'Hello to name', 'allow_empty' => false, 'continue_if_empty' => false, ], ], ], ``` -------------------------------- ### API Tools Content Validation - Basic Configuration (PHP) Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Maps controller service names to input filters for different HTTP methods. The 'input_filter' key provides a default, while specific HTTP methods like 'POST' can override it for particular request types. ```php 'api-tools-content-validation' => [ 'Application\Controller\HelloWorld' => [ 'input_filter' => 'Application\Controller\HelloWorld\Validator', 'POST' => 'Application\Controller\HelloWorld\CreationValidator', ], ], ``` -------------------------------- ### Add Laminas Content Validation to Composer.json Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Manually adds the Laminas Content Validation module as a dependency in your project's composer.json file. After updating, run `composer update`. ```json "require": { "laminas-api-tools/api-tools-content-validation": "^1.4" } ``` -------------------------------- ### Input Filter Specification Source: https://github.com/laminas-api-tools/api-tools-content-validation/blob/1.13.x/README.md Details on configuring input filters using `input_filter_specs`, allowing for granular control over data validation and filtering. ```APIDOC ## Input Filter Specification (input_filter_specs) ### Description Configuration-driven creation of input filters. Keys are unique names (often based on service names), and values are input filter configuration arrays as described in the Laminas manual. ### Method N/A (Configuration) ### Endpoint N/A (Configuration) ### Parameters #### Request Body - **input_filter_specs** (array) - An associative array for defining input filter configurations. - **[FilterName]** (array) - Configuration for a specific input filter. - **name** (string) - The name of the field to validate. - **required** (boolean) - Whether the field is required. - **filters** (array) - An array of filters to apply to the field. - **name** (string) - The name of the filter service (e.g., `Laminas\Filter\StringTrim`). - **options** (array) - Options for the filter. - **validators** (array) - An array of validators to apply to the field. - **description** (string) - A description of the field. - **allow_empty** (boolean) - Whether empty values are allowed. - **continue_if_empty** (boolean) - Whether to continue processing if the value is empty. ### Request Example ```php 'input_filter_specs' => [ 'Application\Controller\HelloWorldGet' => [ 0 => [ 'name' => 'name', 'required' => true, 'filters' => [ 0 => [ 'name' => 'Laminas\Filter\StringTrim', 'options' => [], ], ], 'validators' => [], 'description' => 'Hello to name', 'allow_empty' => false, 'continue_if_empty' => false, ], ], ] ``` ### Response N/A (Configuration) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.