idmkr/form-validation

PHP Form Validator using respect/validation

dev-master 2016-02-09 18:57 UTC

This package is auto-updated.

Last update: 2024-04-11 19:07:43 UTC


README

#PHP Form validation, class based, intuitive. This package provide a very simple wrapper around the awesome respect/validation package. It's just an extendable class for all your needs

##Features

#Installation Use Composer to install this package.

composer require idmkr/form-validation

Extend this class and start using respect/validation validators as intuitive class methods.

#Full example Handling a classic web form is pretty straightforward. Start by extending ValidatableForm into a new form class. You can then use Respect\Validation\Validator ( v:: ) and define your form fields either with setValidation method or by camelizing each form data name. Form data which have not been defined by one of these methods will simply be ignored.

use Respect\Validation\Validator as v;
use Idmkr\FormValidation\ValidatableForm;

class ContactForm extends ValidatableForm
{
    // This generic method can wrap all of your form data
    public function setValidation() 
    {
        return [
            'name' => v::alpha("'\"&,")->length(1,100),
            'telephone' => v::phone(),
            'email' => v::email(),
            'subject' => v::length(3,300)
        ];
    }

    // This dynamic method will override any validation associated with setValidation()
    public function validateMessage()
    {
        return v::length(30,1500);
    }
}

In your POST route/controller function, you would have :

$form = new ContactForm('fr_FR');

$success = $form->validate($_POST);

if(!$success)
    echo json_encode($form->errors());

You also need to sanitize the user input. Mostly to display the data after bad validation :

<form>
    <input type="text" name="name" value="<?=$form->sanitized('name')?>">
</form>