2017 © Pedro Peláez
 

library validation

A validations package built for PHP 5.4 and easy validation for framworks.

image

robclancy/validation

A validations package built for PHP 5.4 and easy validation for framworks.

  • Friday, March 29, 2013
  • by Robbo
  • Repository
  • 1 Watchers
  • 0 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

validation

This isn't ready for use., (*1)

The following is a dump to explain things for feedback., (*2)

First we have an alias, Validatable will alias RobClancy\Validation\Illuminate., (*3)

To use this for model validation you would do..., (*4)

// Note most of this would be in a base class
class Post extends Eloquent {

    // We import the validator methods
    use Validatable;

    // Define the input and rules to validate, basic post validation
    public function defineInput()
    {
        $this->input('user_id')->required()->integer();
        $this->input('post')->required();
    }

    // Then we have our save method
    public function save()
    {
        if ( ! $this->validate($this->attributes))
        {
            return false;
            // User can now call $this->getErrors() for a list of errors
        }

        // Doing this means we can push Input::all() into the model and the validator will filter out what we need
        $this->attributes = $this->getValidatedInput();

        return parent::save();
    }
}

So the above in a controller would be..., (*5)

class PostController extends Controller {

    ...

    public function postIndex()
    {
        $post = new Post(Input::all());
        if ( ! $post->save())
        {
            return Redirect::back()->withErrors($post->getErrors(), $post->getInput());
        }

        return Redirect::to('success/page');
    }
}```

Then I had a use case for logging in, didn't want to validate input before sending to authentication on a model so can do this instead...
```php
class LoginController extends Controller {

    use Validatable;

    // getLogin method etc here

    // Define the input to validate against
    public function defineInput()
    {
        $this->input('email')->required()->email();
        $this->input('password')->required();
    }

    // And once again use the new methods here
    public function postIndex()
    {
        if ( ! $this->validate())
        {
            return Redirect::back()->withErrors($this->getErrors());
        }

        // run authentication
    }
}

In the controller example you can also skip the defineInput method and do this instead..., (*6)

class LoginController extends Controller {

    use Validatable;

    ...


    public function postIndex()
    {
        $validate = $this->validate(function($add)
        {
            $add->input('email')->required()->email();
            $add->input('password')->required();
        });

        if ( ! $validate)
        {
            return Redirect::back()->withErrors($this->getErrors());
        }

        // authenticate
    }
}

The Versions

29/03 2013

dev-master

9999999-dev

A validations package built for PHP 5.4 and easy validation for framworks.

  Sources   Download

The Requires

 

by Avatar Robbo