PHPackages                             ndaedzo/violin - 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. ndaedzo/violin

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

ndaedzo/violin
==============

Violin is an easy to use, highly customisable PHP validator.

v1.0.1(3mo ago)01↓92.9%MITPHP

Since Apr 4Pushed 3mo agoCompare

[ Source](https://github.com/kxngHADES/violin)[ Packagist](https://packagist.org/packages/ndaedzo/violin)[ Docs](https://github.com/kxngHADES/violin)[ RSS](/packages/ndaedzo-violin/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

violin
======

[](#violin)

Violin is an easy to use, highly customisable PHP validator.

**Note: This package is under heavy development and is not recommended for production.**

Installing
----------

[](#installing)

Install using Composer.

```
{
    "require": {
        "ndaedzo/violin": "1.*"
    }
}
```

Basic usage
-----------

[](#basic-usage)

```
use Violin\Violin;

$v = new Violin;

$v->validate([
    'name' => 'billy',
    'age' => 20
], [
    'name' => 'required',
    'age' => 'required|int'
]);

if($v->valid()) {
    echo 'Valid!';
} else {
    echo '', var_dump($v->errors()), '';
}
```

Adding custom rules
-------------------

[](#adding-custom-rules)

Adding custom rules is simple. If the closure returns false, the rule fails.

```
$v->addRuleMessage('isBanana', '%s expects banana, found "%s" instead.');

$v->addRule('isBanana', function($field, $value) {
    return $value === 'banana';
});
```

Adding custom error messages
----------------------------

[](#adding-custom-error-messages)

You can add rule messages, or field messages for total flexibility.

### Adding a rule message

[](#adding-a-rule-message)

```
$v->addRuleMessage('required', 'You better fill in the %s field, or else.');
```

### Adding rule messages in bulk

[](#adding-rule-messages-in-bulk)

```
$v->addRuleMessages([
    'required' => 'You better fill in the %s field, or else.',
    'int' => 'The %s needs to be an integer, but I found %s.',
]);
```

### Adding a field message

[](#adding-a-field-message)

Any field messages you add are preferred over any default or custom rule messages.

```
$v->addFieldMessage('username', 'required', 'You need to enter a username to sign up.');
```

### Adding field messages in bulk

[](#adding-field-messages-in-bulk)

```
$v->addFieldMessages([
    'username' => [
        'required' => 'You need to enter a username to sign up.'
    ],
    'age' => [
        'required' => 'I need your age.',
        'int' => 'Your age needs to be an integer.',
    ]
]);
```

### Error output

[](#error-output)

Errors are output categorised by field, so you're free to play around with them in whatever way you need them output.

```
array(2) {
  ["name"]=>
  array(1) {
    [0]=>
    string(16) "name is required"
  }
  ["age"]=>
  array(2) {
    [0]=>
    string(15) "age is required"
    [1]=>
    string(20) "age must be a number"
  }
}

```

Extending the Violin class
--------------------------

[](#extending-the-violin-class)

You can extend Violin to implement your own validation class and add rules, custom rule messages and custom field messages.

```
class Validator extends Violin\Violin
{
    protected $db;

    protected function __construct(Database $db)
    {
        $this->db = $db; // Some database dependency

        // You can add a custom rule message here if you like, or, you
        // could add it outside of this validation class when you
        // make use of your new Validator object.
        $this->addRuleMessage('usernameDoesNotExist', 'That username is taken');
    }

    // Prepend your validation rule name with validate_
    public function validate_usernameDoesNotExist($field, $value)
    {
        if($db->where('username', '=', $value)->count()) {
            return false;
        }
    }
}

$v = new Validator;

// ... and so on.
```

Rules
-----

[](#rules)

This list of rules are **in progress**. Of course, you can always contribute to the project if you'd like to add more to the base ruleset.

#### activeUrl

[](#activeurl)

If the URL provided is an active URL using checkdnsrr().

#### alnum

[](#alnum)

If the value is alphanumeric.

#### alnumDash

[](#alnumdash)

If the value is alphanumeric. Dashes and underscores are permitted.

#### alpha

[](#alpha)

If the value is alphabetic letters only.

#### alphaDash

[](#alphadash)

If the value is alphabetic letters only. Dashes and underscores are permitted.

#### bool

[](#bool)

If the value is a boolean.

#### email

[](#email)

If the value is a valid email.

#### int

[](#int)

If the value is an integer, including integers within strings. 1 and '1' are both classed as integers.

#### required

[](#required)

If the value is present.

Contributing
------------

[](#contributing)

Please file issues under GitHub, or submit a pull request if you'd like to directly contribute.

Todo
----

[](#todo)

- Allow parameter based rules. For example:

```
$v->validate([
    'name' => 'billy',
    'age' => '20'
], [
    'name' => 'required',
    'age' => 'required|int|max:21'
]);
```

- Improve error storage so `errors[]` doesn't contain a list of strings, but a list of messages to look up. Rough example:

```
protected $errors = [
    'name' => [ // field
        'args' => [
            'FIELD_NAME', 'FIELD_VALUE'
        ],
        'errors' => [
            'required', 'alpha' // errors that have occured
        ]
    ]
];
```

Then when calling `errors()`, this should collect up errors for that field, build the string and return them.

- Allow errors to be defined with {field} and {value} rather than %s's for greater flexibility.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance82

Actively maintained with recent releases

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

91d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f2579c46c44d6dbbcca30cc51201e1b92fc89fabea4b8a868a51dc24767119dd?d=identicon)[kxngHADES](/maintainers/kxngHADES)

---

Top Contributors

[![kxngHADES](https://avatars.githubusercontent.com/u/132647693?v=4)](https://github.com/kxngHADES "kxngHADES (3 commits)")

---

Tags

validation

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/ndaedzo-violin/health.svg)

```
[![Health](https://phpackages.com/badges/ndaedzo-violin/health.svg)](https://phpackages.com/packages/ndaedzo-violin)
```

###  Alternatives

[composer/semver

Version comparison library that offers utilities, version constraint parsing and validation.

3.3k522.3M992](/packages/composer-semver)[giggsey/libphonenumber-for-php

A library for parsing, formatting, storing and validating international phone numbers, a PHP Port of Google's libphonenumber.

5.0k159.6M527](/packages/giggsey-libphonenumber-for-php)[respect/validation

The most awesome validation engine ever created for PHP

6.0k39.9M415](/packages/respect-validation)[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k39.7M146](/packages/propaganistas-laravel-phone)[opis/json-schema

Json Schema Validator for PHP

65243.6M302](/packages/opis-json-schema)[giggsey/libphonenumber-for-php-lite

A lite version of giggsey/libphonenumber-for-php, which is a PHP Port of Google's libphonenumber

9216.6M76](/packages/giggsey-libphonenumber-for-php-lite)

PHPackages © 2026

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