PHPackages                             fiqhidayat/wp-validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. fiqhidayat/wp-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

fiqhidayat/wp-validator
=======================

Laravel-style validation library for WordPress

1.3.1(11mo ago)08GPL-2.0-or-laterPHPPHP &gt;=8.2

Since Jun 3Pushed 11mo agoCompare

[ Source](https://github.com/fiqhidayat/wp-validator)[ Packagist](https://packagist.org/packages/fiqhidayat/wp-validator)[ RSS](/packages/fiqhidayat-wp-validator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (1)Versions (8)Used By (0)

WP Validator
============

[](#wp-validator)

A Laravel-style validation library for WordPress forms.

Installation
------------

[](#installation)

```
composer require fiqhidayat/wp-validator

```

Usage
-----

[](#usage)

```
use Fiqhidayat\WPValidator\Validator;

// Create a validator instance
$validator = new Validator([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'age' => 25,
], [
    'name' => 'required|min:3',
    'email' => 'required|email',
    'age' => 'required|numeric|min:18',
]);

// Check if validation passes
if ($validator->passes()) {
    // Validation passed
    $validatedData = $validator->validated();
} else {
    // Validation failed
    $errors = $validator->errors();
}
```

Nested Validation with Dot Notation
-----------------------------------

[](#nested-validation-with-dot-notation)

The library supports validating nested arrays and objects using dot notation:

```
// Complex data structure with nested properties
$data = [
    'user' => [
        'name' => 'John Doe',
        'email' => 'john@example.com'
    ],
    'product' => [
        'price' => [
            'regular' => 100,
            'sale' => 80
        ]
    ]
];

// Use dot notation to access nested fields
$rules = [
    'user.name' => 'required|min:3',
    'user.email' => 'required|email',
    'product.price.sale' => 'required|numeric|lt:product.price.regular', // Compare with another nested field
    'product.price.regular' => 'required|numeric'
];

$validator = new Validator($data, $rules);
```

This feature works with all validation rules including the comparison rules.

Array Validation Example
------------------------

[](#array-validation-example)

The library also supports validating nested arrays using wildcards:

```
// Complex data structure with nested arrays
$data = [
    'user' => [
        'name' => 'John Doe',
        'email' => 'john@example.com'
    ],
    'skills' => ['PHP', 'JavaScript', 'HTML', 'CSS'],
    'works' => [
        [
            'company_name' => 'Acme Inc',
            'role' => 'Developer',
            'start_date' => '2020-01-01'
        ],
        [
            'company_name' => 'XYZ Corp',
            'role' => 'Senior Developer',
            'start_date' => '2022-05-01'
        ]
    ]
];

// Define validation rules
$rules = [
    'user' => 'required|array',
    'user.name' => 'required|string|min:3',
    'user.email' => 'required|email',
    'skills' => 'nullable|array',
    'skills.*' => 'string',
    'works' => 'nullable|array',
    'works.*.company_name' => 'required|string',
    'works.*.role' => 'required|string',
    'works.*.start_date' => 'nullable|date'
];

$validator = new Validator($data, $rules);

if ($validator->passes()) {
    // All data is valid
} else {
    // Get validation errors
    $errors = $validator->errors();
}
```

WordPress Form Example
----------------------

[](#wordpress-form-example)

Here's an example of validating a contact form submission in WordPress:

```
function process_contact_form() {
    // Check nonce for security
    check_ajax_referer('contact_form_nonce', 'security');

    // Get form data
    $data = [
        'name' => sanitize_text_field($_POST['name']),
        'email' => sanitize_email($_POST['email']),
        'message' => sanitize_textarea_field($_POST['message']),
    ];

    // Set validation rules
    $rules = [
        'name' => 'required|min:3',
        'email' => 'required|email',
        'message' => 'required|min:10',
    ];

    // Create validator
    $validator = new Fiqhidayat\WPValidator\Validator($data, $rules);

    // Check if validation fails
    if ($validator->fails()) {
        wp_send_json_error([
            'success' => false,
            'errors' => $validator->errors(),
        ]);
    }

    // Process the form (save to database, send email, etc.)
    // ...

    wp_send_json_success([
        'success' => true,
        'message' => 'Form submitted successfully!'
    ]);
}
add_action('wp_ajax_contact_form', 'process_contact_form');
add_action('wp_ajax_nopriv_contact_form', 'process_contact_form');
```

Available Rules
---------------

[](#available-rules)

This library supports most Laravel validation rules including:

### Core Validation Rules

[](#core-validation-rules)

- `required`: The field must be present and not empty
- `filled`: The field must not be empty when it is present (but can be absent)
- `present`: The field must be present in the input data (but may be empty)
- `nullable`: The field can be null or empty string

### Data Type Rules

[](#data-type-rules)

- `string`: The field must be a string
- `numeric`: The field must be numeric
- `integer`: The field must be an integer
- `boolean`: The field must be a boolean value (true, false, 0, 1, '0', '1')
- `array`: The field must be a PHP array
- `date`: The field must be a valid date
- `json`: The field must be a valid JSON string
- `timezone`: The field must be a valid timezone

### String Rules

[](#string-rules)

- `email`: The field must be a valid email address
- `url`: The field must be a valid URL
- `ip`: The field must be a valid IP address
- `alpha`: The field must contain only alphabetic characters
- `alpha_num`: The field must contain only alpha-numeric characters
- `alpha_dash`: The field may contain alpha-numeric characters, dashes, and underscores
- `regex:pattern`: The field must match the given regular expression

### Size Validation Rules

[](#size-validation-rules)

- `min:value`: String/numeric minimum length or value
- `max:value`: String/numeric maximum length or value
- `size:value`: The field must match the specified size (string length, numeric value, array count, or file size in KB)
- `digits:value`: The field must be a numeric value with the exact length specified
- `digits_between:min,max`: The field must be a numeric value with a length between the specified min and max

### Comparison Rules

[](#comparison-rules)

- `same:field`: The field must match the specified field
- `different:field`: The field must be different from the specified field
- `confirmed`: The field must have a matching field of {field}\_confirmation
- `gt:field`: The field must be greater than the specified field (numeric)
- `gte:field`: The field must be greater than or equal to the specified field (numeric)
- `lt:field`: The field must be less than the specified field (numeric)
- `lte:field`: The field must be less than or equal to the specified field (numeric)
- `in:foo,bar,...`: The field must be included in the given list of values
- `not_in:foo,bar,...`: The field must not be included in the given list of values
- `unique:table,column,except,idColumn`: The field must be unique in a database table

### Database Rules

[](#database-rules)

- `exists:table,column,where_column,where_value`: The field must exist in the specified database table
    - `table`: The database table name (without wp\_ prefix)
    - `column`: The column to check (optional, defaults to the field name)
    - `where_column`: Additional where condition column (optional)
    - `where_value`: Additional where condition value (optional)

Examples:

```
// Check if user ID exists in users table
'user_id' => 'exists:users,id'

// Check if email exists in users table
'email' => 'exists:users,email'

// Check if category exists and is active
'category_id' => 'exists:categories,id,status,active'
```

### File Validation Rules

[](#file-validation-rules)

- `file`: The field must be a successfully uploaded file
- `image`: The field must be an image file (jpeg, png, bmp, gif, svg, webp)
- `mimes:jpg,png,...`: The field must be a file with one of the specified extensions
- `mimetypes:image/jpeg,image/png,...`: The field must be a file with one of the specified MIME types
- `dimensions:width=value,height=value,...`: The field must be an image that meets the specified dimensions constraints

Custom Implementation
---------------------

[](#custom-implementation)

You can extend this validator by creating your own rules. Here's how to create and register a custom validation rule:

```
// Create a class that implements the Rule interface
class PalindromeRule implements Fiqhidayat\WPValidator\Rule
{
    public function passes($attribute, $value, array $parameters, $validator) {
        $cleanStr = preg_replace('/[^a-z0-9]/i', '', strtolower($value));
        return $cleanStr === strrev($cleanStr);
    }

    public function message($attribute, $value, array $parameters) {
        return "The {$attribute} must be a palindrome.";
    }
}

// Register the custom rule
use Fiqhidayat\WPValidator\ValidationExtender;
ValidationExtender::extend('palindrome', PalindromeRule::class);

// Now use it in your validations
$validator = new Validator([
    'name' => 'Anna'
], [
    'name' => 'required|palindrome'
]);
```

License
-------

[](#license)

GPL-2.0-or-later

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance51

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

7

Last Release

340d ago

PHP version history (2 changes)1.0.0PHP &gt;=8.3

1.0.1PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b3a9de1e333a5c955fb56a90a075c06aeef04c9d34c043b3b2cfec24987684d?d=identicon)[fiqhidayat](/maintainers/fiqhidayat)

---

Top Contributors

[![fiqhidayat](https://avatars.githubusercontent.com/u/31733931?v=4)](https://github.com/fiqhidayat "fiqhidayat (21 commits)")

---

Tags

laravelvalidatorvalidationwordpressform

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fiqhidayat-wp-validator/health.svg)

```
[![Health](https://phpackages.com/badges/fiqhidayat-wp-validator/health.svg)](https://phpackages.com/packages/fiqhidayat-wp-validator)
```

###  Alternatives

[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[illuminatech/validation-composite

Allows uniting several validation rules into a single one for easy re-usage

184485.5k](/packages/illuminatech-validation-composite)[stuyam/laravel-phone-validator

A phone validator for Laravel using the free Twilio phone lookup service.

2861.3k](/packages/stuyam-laravel-phone-validator)[laravel-validation-rules/timezone

Validate that a given timezone is valid.

2119.0k](/packages/laravel-validation-rules-timezone)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
