Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Validation Shorthand Laravel Package

digitalrevolution/symfony-validation-shorthand

View on GitHub
Deep Wiki
Context7
Index
Shorthands
Array data Validation
Traversable data validation
Examples

Array data validation

Basic example

Validating a key/value-pair data array.

Rules:

[
    'productId'    =>  'required|int|min:1',                          
    'name'         =>  'required|string|filled|max:255',             
    'description'  =>  'string|filled|nullable|max:10000',           
    'active'       =>  'required|bool',
    'price'        =>  'required|float|min:0',
    'discount'     =>  'float|nullable'     
]

Explanation:

Column Description
productId required and must be integer of 1 or higher
name required and must be non empty string less or equal than 255 characters
description optional nullable or non empty string less or equal than 10000 characters
active required true/false
price required non nullable float greater or equal than zero
discount optional nullable float

Successfully validates:

[
    'productId'   => '12345',
    'name'        => 'Laser printer',
    'description' => null,
    'active'      => '1',
    'price'       => '4.99',
    'discount'    => '-1.00'
]

Advanced example

[
    'contact.first_name'      => 'required|string|filled',
    'contact.last_name'       => 'required|string|between:10,100',
    'contact.phone_number'    => 'required|regex:/^\d{10}$/',
    'contact.email'           => 'required|string|email', 
    'contact.birth_day'       => ['required|string', new \Symfony\Component\Validator\Constraints\Date()],    
    'address.street_name'     => 'required|string',
    'address.house_number'    => 'required|int|between:0,999',
    'address.addition'        => 'string|nullable|max:3',
    'address.city'            => 'required|string|filled',        
    'address.country_code'    => 'required|string|between:2,2',    
    'interests?.*.interestId' => 'required:int:min:1',
    'interests?.*.label'      => 'required|string|filled',                    
    'tags?.*'                 => 'required|string|filled'                   
]

Explanation:

The keys contact and address are mandatory, while interests and tags are optional.
If there's no suitable shorthand, the rules can be supplemented with Symfony Constraint's.
If interested is given, it must be an array of [interestId, label] elements
If tags is given, it must be a non-empty array (required) of strings with minimum length of 1 (filled)

Successfully validates

[
    'contact' => [
        'first_name'   => 'Peter',
        'last_name'    => 'Parker',  
        'phone_number' => '0123456789',
        'email'        => '[email protected]',
    ],
    'address' => [
        'street_name'  => '15th Street',
        'house_number' => '24',
        'city'         => 'New York',
        'country_code' => 'US',
    ],
    'interests' => [
        ['interestId' => 5, 'label' => 'Movies'],
        ['interestId' => 7, 'label' => 'Photography'],        
    ],
    'tags' => ['customer', 'student', 'new']       
]
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
mydnic/volet-feature-board
wmde/fundraising-phpcs
friendlylabs/surveyr
mg-techlegend/laravel-notify-africa
islamabdurahman/laravel-sms-service
dmhendricks/b2-sdk-php
aplicativa/laravel-admin
ezavalishin/vkma
jackardios/laravel-json-api-paginate
viral/laravel-adminpanel
dieppk/log-viewer
daimakuai-ext/helpers
codexshaper/php-blade-view
abraxas81/charts
adamlien/laravel-cas
quimgc/tasks
sistem-atc/bank-sdk
core45/laravel-cookieconsent
projectsend/community-modules
ovac/laravel-hubtel-payment