PHPackages                             grandadevans/laravel-form-validator - 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. grandadevans/laravel-form-validator

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

grandadevans/laravel-form-validator
===================================

A package that will generate validation forms for Laravel 4.

v0.1.4(11y ago)6721PHPPHP &gt;=5.4.0

Since Sep 25Pushed 11y ago2 watchersCompare

[ Source](https://github.com/GrandadEvans/laravel-form-validator)[ Packagist](https://packagist.org/packages/grandadevans/laravel-form-validator)[ RSS](/packages/grandadevans-laravel-form-validator/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (10)Versions (6)Used By (0)

Laravel Form Validator
======================

[](#laravel-form-validator)

[![Build Status](https://camo.githubusercontent.com/3e03f7aaefd8c7ec9e6f05fb93a7c524f1c665e62dbf201e90acb6f6fd9b68ed/68747470733a2f2f7472617669732d63692e6f72672f4772616e6461644576616e732f6c61726176656c2d666f726d2d76616c696461746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/GrandadEvans/laravel-form-validator)

Contents
--------

[](#contents)

- [Introduction](#introduction)
- [Installation](#installation)
- [Usage](#usage)
- [Example](#example)
- [Tests](#tests)

Introduction
------------

[](#introduction)

After using [Jeffrey Way](https://github.com/JeffreyWay)'s [Generator](https://github.com/JeffreyWay/Laravel-Generators) and his [Validator](https://github.com/laracasts/Validation) package for a while now I got fed up of copying and pasting the contents of the form validation files so thought I'd create my own version.

### What will this do?

[](#what-will-this-do)

This package will create a form validation file containing a list or rules for the validation package to validate.

### Why not just folk the generators package?

[](#why-not-just-folk-the-generators-package)

Well I still only have 3 or 4 clients as a Freelance Web Developer and this gives me the perfect opportunity to show my coding skills off. That way when people ask me if I have any work I can show them it doesn't seem like I'm making excuses when I mention "white label" and "non disclosure agreements".

Installation
------------

[](#installation)

Install this package through [Composer](https://getcomposer.org).

```
"require-dev": {
    "grandadevans:laravel-form-validator": "~0.1.0"
}
```

Then include the service provider in your app/config/app.php by adding it to your "providers" array:

```
/*
 * app/config/app.php
 */
'providers' => array(
    #########
    'Grandadevans\GenerateForm\ServiceProvider\GenerateFormServiceProvider'
);
```

Don't forget that composer.json will need to know where to autoload the form from. So if the forms are kept in the default `app/Forms` directory you could just add it to the classMap

```
/*
 * composer.json
 */
"autoload": {
	"classmap": [
		"app/commands",
		"app/controllers",
		"app/models",
		"app/database/migrations",
		"app/database/seeds",

        "app/Forms"
    ]
}
```

Also: as you have changed the classMap you will have to run

```
composer dump-autoload
```

Usage
-----

[](#usage)

From the command line you can call the package with

```
php artisan generate:form
```

From the root directory of your Laravel installation.

### Required Argument

[](#required-argument)

There is only one required argument and that is the **name** of the form

### Options

[](#options)

- **--dir** This is the directory in which the form will be saved to *(defaults to app/Forms)*. Please make sure that the directory exists before executing the command.
- **--rules** This is a string representation of the rules to be used *(see below).*
- **--namespace** This is the namespace that will be given to the Form.

#### The Rules Option

[](#the-rules-option)

This is where the command really comes into it's own. The rules string should be made up of the following

1. The name of the input field to validate followed by a pipe ( **|** )
2. A list of the conditions that the input is to validate against. Each condition being separated by another colon ( *pipe*\* )
3. If you wish to validate another field then separate them with an ampersand ( **&amp;** ) and carry on.

**Example**: If I wanted to validate a typical login form containing a username and password field I would set the rules option as follows

```
php artisan generate:form Login --rules="username|required|between:6,50|alpha & password|required|min:8"
```

Each condition that is entered (*required*, *confirmed* etc) will be validated against the [available conditions in Laravel docs](http://laravel.com/docs/validation#available-validation-rules).

Once the command is executed a Form is generated and placed in the specified directory (or the default app/Forms).

Inject and Type hint the generated form into your controller (or where you wish to do your validation)

```
protected $loginForm;

public function __construct(LoginForm $loginForm)
{
	$this->loginForm = $loginForm;
}
```

#### Try to validate the input with

[](#try-to-validate-the-input-with)

```
$this->loginForm->validate(Input::only(['username','password']));
```

Example
-------

[](#example)

Let's say I want to create the above mentioned login form

### Step 1: Create the form

[](#step-1-create-the-form)

```
php artisan generate|form Login --rules="username|required|between:6,50|alpha & password|required|min:8"
```

I can then view the form at `app/Forms/FooForm.php`.

```
