PHPackages                             idmkr/form-validation - 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. idmkr/form-validation

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

idmkr/form-validation
=====================

PHP Form Validator using respect/validation

07PHP

Since Feb 9Pushed 10y ago3 watchersCompare

[ Source](https://github.com/idmkr/form-validation)[ Packagist](https://packagist.org/packages/idmkr/form-validation)[ RSS](/packages/idmkr-form-validation/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

\#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

- Ok to use (as is) within micro frameworks like Slim or Silex or no framework at all
- Define your form data and validation with a simple class method
- [Gettext translations for exception messages](https://github.com/idmkr/respect-validation-localization)

\#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 :

```
