GithubHelp home page GithubHelp logo

izica / phalcon-validation Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 13 KB

Handy interface for Phalcon\Validation, controller request validation tool

License: MIT License

PHP 100.00%
validation validator validate validation-library validations validators phalcon phalcon-php phalcon-framework interface

phalcon-validation's Introduction

Phalcon validation

Useful tool for Phalcon\Validation for easiest way to validate your data

Installation

composer require izica/phalcon-validation

Api

Izica\Validation

  • __construct($arOptions) -- example below
  • validate($arData)
  • static make($arData, $arOptions)

Izica\ValidationRule

  • static required($arOptions: optional) -- Phalcon\Validation\Validator\PresenceOf
  • static numeric($arOptions: optional) -- Phalcon\Validation\Validator\Numericality
  • static email($arOptions: optional) -- Phalcon\Validation\Validator\Email
  • static url($arOptions: optional) -- Phalcon\Validation\Validator\Url
  • static callback($arOptions) -- Phalcon\Validation\Validator\Callback
  • static length($arOptions) -- Phalcon\Validation\Validator\StringLength
  • static unique($arOptions) -- Phalcon\Validation\Validator\UniquenessValidator
  • static between($arOptions) -- Phalcon\Validation\Validator\Between
  • static file($arOptions) -- Phalcon\Validation\Validator\File
  • static date($arOptions) -- Phalcon\Validation\Validator\Date
  • static regex($arOptions) -- Phalcon\Validation\Validator\Regex

Notice

[https://docs.phalconphp.com/3.4/en/api/phalcon_validation_validator_numericality] $arOptions which passed in static functions, for example static numeric($arOptions), used as params for new Numericality($arOptions);

You can use it like this

$validation = new Validation([
    'num' => [ValidationRule::numeric(['message' => ':field is not numeric')],
]);

Usage

use Izica\Validation;
use Izica\ValidationRule;
use Phalcon\Mvc\Controller;

class ExampleController extends Controller {
    public function indexAction() {
        $validation = new Validation([
            'email' => [ValidationRule::required(), ValidationRule::email()],
            'num' => [ValidationRule::required(), ValidationRule::numeric()],
        ]);
        $arMessages = $validation->validate($_POST);
        if ($arMessages) {
            // validation error
        }
        // validation success
    }
    // or short
    public function indexAction() {
        $arMessages = Validation::make($_POST, [
            'email' => [ValidationRule::required(), ValidationRule::email()],
            'num' => [ValidationRule::required(), ValidationRule::numeric()],
        ]);
        if ($arMessages) {
            // validation error
        }
        // validation success
    }
}
/*
    $_POST = [
        'email' => 'qwe'
    ];
*/
$arMessages = $validation->validate($_POST);

/* 
$arMessages
Array
(
    [0] => Array
        (
            [field] => email
            [type] => email
            [message] => email is not valid
        )

    [1] => Array
        (
            [field] => num
            [type] => required
            [message] => num is required
        )

    [2] => Array
        (
            [field] => num
            [type] => numeric
            [message] => num is not numeric
        )

)
*/

phalcon-validation's People

Contributors

izica avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.