GithubHelp home page GithubHelp logo

bdbch / vivalidator Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 46 KB

Simple and easy data validation for forms

License: MIT License

PHP 100.00%
flynt flyntwp feature php wordpress forms validation

vivalidator's Introduction

Screenshot of a form validated with Vivalidator

Vivalidator

A simple and easy input and data validation library

Why?

I dislike the way of using Wordpress Plugins or heavy PHP packages for form development when it comes to Wordpress forms. I hate when third party code adds markup to your page or narrows my possibilites because of some restrictions coming with the external code. Thats why I started looking for easy to use validation frameworks but didn't find anything that perfectly fits my needs. This is why I started development on Vivalidator.

Installation

The Composer Way

Run composer require bdbch/vivalidator in your project folder to install Vivalidator via Composer. It will be automatically placed into your autoload.

Manual Way

Make sure to have all necessary files copied manually into your project. Require the desired library from the src folder in your code to get access to a Vivalidator Class.

Usage

Using the Validator is simple. Check out this example code to get an idea on how to use this feature.

<?php

// Make sure to use the feature in your component
use Vivalidator\Validator;

// Data to validate. I left the job empty on purpose to demonstrate non-valid data
$data = [
  'name' => 'Peter',
  'age' => '23',
  'job' => ''
];

// Validator Options / Rules
// Each field can have multiple rules with their own error messages
$options = [
  'name' => [
    [
      'rule' => 'required',
      'error' => 'Please enter your name'
    ],
    [
      'rule' => 'minlength',
      'value' => 3,
      'error' => 'Your name needs to be longer than 3 characters.'
    ]
  ],
  'age' => [
    [
      'rule' => 'required',
      'error' => 'Please enter your age'
    ],
    [
      'rule' => 'min',
      'value' => 1,
      'error' => 'You need to be older than 1'
    ],
    [
      'rule' => 'max',
      'value' => 150,
      'error' => 'I would love to believe you, but I will not'
    ]
  ],
  'job' => [
    [
      'rule' => 'required',
      'error' => 'Please specify your job'
    ]
  ]
];

// Create a new validator instance and pass data and options in
$validator = new Validator($data, $options);

// Errors will be collected in an array.
// If there are no errors, it will be an empty string
if (count($validator->errors)) {
  echo 'You have ' . count($validator->errors) . ' errors in your submission.';
  foreach ($validator->errors as $error) {
    echo 'Error: ' . $error;
  }
} else {
  echo 'All set, lets submit!';
}

// Thats it
// You can now do what ever you want with the submitted data
// Make sure to escape the data. The validator doesn't do this right now
// TODO: This will be a feature so fields can be escaped if needed

Adding ReCaptcha

Vivalidator now has ReCaptcha Support! Just specify your Secret Key and the Error String as extra data to configure it right away! Make sure to have the ReCaptcha library installed manually or via composer require google/recaptcha.

// Check this URL if you need a Key
// https://www.google.com/recaptcha/admin
// Make sure you have a working ReCaptcha library installed,
// otherwise this will do nothing
$validator = new Validator($data, $options, [
  'recaptcha' => [
    'secret' => 'YOUR_SECRET_KEY_HERE',
    'error' => 'Please verify our captcha'
  ]
]);

Rules

  • required - Checks if the input is not empty
  • minlength - Checks if the input is longer than a specific value
  • maxlength - Checks if the input is lower than a specific value
  • email - Checks if the input is a valid email
  • url - Checks if the input is an URL
  • number - Checks if the input is a number
  • min - Checks if the input is higher than the defined value
  • max - Checks if the input is lower than the defined value
  • between - Checks if the input is between two numbers (combination of max and min)
  • regex - Checks if the input matches a specified regex

Planned Rules

See this issue to learn more

Contribution

I'm open for pull requests and would love to see some support. I'm not the 100% best PHP developer and would love to get this Flynt feature even better so we're not bound to damn Wordpress Plugins anymore.

License

This is licensed under the MIT license.

vivalidator's People

Contributors

bdbch avatar

Watchers

 avatar  avatar

vivalidator's Issues

Date Rule

Description

This rule should be able to check if an input is a date and (optionally) check if the date is between or later/earlier than other dates.

Example Code

[
  'date' => [
    [
      'rule' => 'date',
      'formats' => [ 'Y-m-d', 'd.m.Y' ],
      'message' => 'Please provide a real date'.
      'min' => [
        'date' => date('Y-m-d'),
        'message' => 'The selected date must be after today'
      ],
      'max' => [
        'date' => '2018-12-01',
        'message' => 'The selected date cant be next year'
      ]
    ]
  ]
]

Planned Rules

  • min - This checks if the submitted value is a number and higher than a specific value
  • max - This checks if the submitted value is a number and lower than a specific value
  • range - This checks if the submitted value is between two values
  • number - This checks if the submitted value is a number (int, float)
  • regex - Checks the value with a specific regex
  • url - Check the value against a URL - See #4

Update README

Describe the bug
In the README it still says "message" instead of "error"

Change "message" key to "error"

Describe the bug
Currently the output error messages key is "message" which doesn't explain that it's actually an error message.

Keep Labeling as close to vanilla HTML validation as possible

Describe the bug
Using different labels for the same validation method doesn't make sense. Currently some rules are called empty instead of required or mail instead of email. In the end it should be as easy to understand.

Expected behavior
All validation rules should be easy to understand.

Add ReCaptcha Support

Is your feature request related to a problem? Please describe.
Right now there is no specified way on how to use ReCaptcha with Vivalidator. A ReCaptcha Rule would be great.

Describe the solution you'd like
Simple: A ReCaptcha Rule, for example like this:

[
  "rule": "recaptcha",
  "message": "Please verify your submission via Captcha"
]

Describe alternatives you've considered
Maybe add custom captchas too (Honeypot, simple Calculation Captcha, etc.)

URL rule

This rule should check if an input is a valid URL

Possible regex:

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

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.