cnaebadi/null-replacer

A Laravel validation rule to remove or replace null fields before validation.

v1.0.0 2025-05-06 07:49 UTC

This package is auto-updated.

Last update: 2025-06-16 14:23:24 UTC


README

Null Replacer Logo

Null Replacer

A Laravel validation rule to automatically remove, replace, or convert null values in request data before validation.

Latest Version on Packagist Total Downloads License: MIT

โœจ Features

  • Replace null values with a custom value
  • Convert 'true' and 'false' strings to actual boolean values
  • Remove null fields entirely from the request
  • Can be used directly in Form Request rules

๐Ÿš€ Installation

composer require cnaebadi/null-replacer

๐Ÿงช Usage

โš ๏ธ Note: When using null_replacer, you do not need to use nullable. This rule handles null values entirely on its own and using nullable before it may interfere with its functionality.

In your Laravel Form Request:

public function rules()
{
    return [
        'optional_note'  => ['null_replacer'], // Removes key if null
        'is_active'      => ['null_replacer:true'], // Converts to boolean true
        'email'          => ['null_replacer:default@example.com'], // Replaces null with given default
    ];
}

๐Ÿ›  How It Works

  • null-replacer:true: 'true'/'false' โ†’ true/false
  • null-replacer:something: replaces null with "something"
  • null-replacer (no value): removes the key from the request if it's null

๐Ÿ“ฆ Publish & Extend

If you want to customize the behavior, you can publish the config (if added later) or extend the core rule logic in your own classes.

๐Ÿง‘โ€๐Ÿ’ป Author

Sina Ebadi

๐Ÿ“„ License

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