Code Monkey home page Code Monkey logo

bumbon-validation's Introduction

Bumbon/Validation

PHP Validation.

Installation

composer require bumbon/validation

Usage

<?php

$validation = Validation::create(
    [
        'foo' => NotBlank(),
        'baz' => [
            new NotBlank(),
            new Length([
                'min' => 5,
            ])
        ],
    ],
    [
        'foo' => 'bar',
        'baz' => 'qux'
    ]
);
$violation = $validation->validate();

// or
// $validation->validate(['foo' => 'bar','baz' => 'qux']);

if ($violation->hasViolation()) {
    echo 'Invalid data';
} else {
    echo 'Data valid';
}

// or
if ($violation->noViolation()) {
    echo 'Data valid';
} else {
    echo 'Invalid data';
}

// get violation message
var_dump($violation->all());

// [
//   'foo' => ['Nilai ini tidak boleh kosong']
// ]

Available Constraints

All option can be passed in each constraint via associative array. All constraint has parent option. Default message is Bahasa Indonesia.

<?php

// default parent option
$option = [
    // message
    'message' => 'Custom message',
    // group validate
    'groups' => ['Default'],
    // trim value
    'trim' => true,
    // callback for normalize data (before validate)
    'normalizer' => null,
];
  • Between

    <?php
    $option = [
      'min' => 5,
      'max' => 5,
    ];
  • Blank

  • Boolean

  • Callback

    <?php
    $option = [
      // callback (return bool)
      'callback' => null,
    ];
  • Choice

    <?php
    $option = [
      // array of valid choices
      'choices' => [],
    ];
  • Email

  • Equal

    <?php
    $option = [
      // equal to this value
      'value' => null,
    ];
  • GreaterThanEqual

    <?php
    $option = [
      // greater than or equal to this value
      'value' => null,
    ];
  • GreaterThan

    <?php
    $option = [
      // greater than to this value
      'value' => null,
    ];
  • Identical

    <?php
    $option = [
      // identical to this value
      'value' => null,
    ];
  • InTable

    <?php
    $option = [
      // pdo connection
      'pdo' => null,
      // table to lookup
      'table' => null,
      // field to find
      'field' => 'ID',
    ];
  • Ip

  • IsFalse

  • IsTrue

  • Length

    <?php
    $option = [
      'min' => 5,
      'max' => 5,
    ];
  • LessThanEqual

    <?php
    $option = [
      // lest than or equal to this value
      'value' => null,
    ];
  • LessThan

    <?php
    $option = [
      // less than to this value
      'value' => null,
    ];
  • NotBlank

  • NotEqual

    <?php
    $option = [
      // not equal to this value
      'value' => null,
    ];
  • NotIdentical

    <?php
    $option = [
      // not identical to this value
      'value' => null,
    ];
  • NotInTable

    <?php
    $option = [
      // pdo connection
      'pdo' => null,
      // table to lookup
      'table' => null,
      // field to find
      'field' => 'ID',
      // primary key, can be array
      'id' => 'ID',
      // current primary key value, can be array
      'current_id' => null,
    ];
  • Numeric

  • PhoneNumber

  • Regex

    <?php
    $option = [
      // match to this pattern
      'pattern' => null,
    ];
  • Url

Custom Constraints

Implements Constraint Interface.

<?php

namespace Bumbon\Validation\Constraint;

interface ConstraintInterface
{
    /**
     * Get violation message
     * @return array|string
     */
    public function getMessages();

    /**
     * Get groups
     * @return array
     */
    public function getGroups();

    /**
     * Set value to check
     * @param mixed $value
     * @return  $this
     */
    public function setValue($value);

    /**
     * Get value
     * @return  mixed
     */
    public function getValue();

    /**
     * Validate value
     * @return $this
     */
    public function validate();

    /**
     * Get constraint validity
     * @return boolean
     */
    public function isValid();
}

Or extends AbstractConstraint

<?php

use Bumbon\Validation\Constraint;

class CustomConstraint extends AbstractContraint
{
    public function validate()
    {
        $this->valid = $this->value == 'true';

        return $this;
    }
}

Inspiration

This library was inspired by Symfony/Validation. I created this for my own project.

bumbon-validation's People

Contributors

eghojansu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.