teepluss/harvey

Laravel4 separate validation

v1.0 2014-08-04 17:51 UTC

README

This is my internal project, not yet complete.

Installation

To get the lastest version of Theme simply require it in your composer.json file.

"teepluss/harvey": "dev-master"

You'll then need to run composer install to download it and have the autoloader updated.

Usage

class Blog extends \Teepluss\Harvey\Harvey {

    /**
     * Define rules.
     *
     * @type array
     */
    public static $rules = array(
        'description' => 'min:10|max:500',
        'onCreate'    => array(
            'title' => 'required',
            'url' => 'active_url'
        ),
        'onUpdate'    => array(
            'title' => 'required'
        )
    );

    /**
     * Custom validation messages.
     *
     * @type array
     */
    public static $messages = array(
        'title.required' => 'Please fill title before submitting.'
    );

    /**
     * Custom validation labels.

     * @type array
     */
    public static $lables = array(
        'title' => 'Title'
    );

    /**
     * Construct.

     * @param   array $attributes
     * @return void
     */
    public function __construct(array $attributes = array())
    {
        parent::__construct($attributes);

        // Custom label.
        $this->setLabelNames(array(
            'title'  => trans('labels.product'),
        ));
    }

    /**
     * Event before validate.
     *
     * @return voide
     */
    protected function beforeValidate()
    {
        $validator->sometimes('description', 'numeric', function($input)
        {
            return $input->title == 'tee';
        });
    }

}

This code for creating a new content.

$blog = new Blog;

$blog->title = 'New blog';
$blog->description = 'This is my first entry';
$blog->url = 'http://www.domain.com';

// Addition rule for another input.
$blog->addValidate(
    array('other' => Input::get('other')),
    array('other' => 'required|email'),
    array('other.required' => 'sss')
);

if ( ! $blog->save())
{
    $errors = $blog->errors();

    return Redirect::back()->withErrors($errors)->withInput();
}

Validation rules for creating.

array(3) [
    'description' => array(2) [
        string (6) "min:10"
        string (7) "max:500"
    ]
    'title' => array(1) [
        string (8) "required"
    ]
    'url' => array(1) [
        string (10) "active_url"
    ]
]

This code for updating an exists content.

$blog = Blog::find(1);

$blog->title = 'New blog';
$blog->description = 'This is my first entry';
$blog->url = 'http://www.domain.com';

$blog->save();

if ( ! $blog->save())
{
    $errors = $blog->errors();

    return Redirect::back()->withErrors($errors)->withInput();
}

Validation rules for updating.

array(2) [
    'description' => array(2) [
        string (6) "min:10"
        string (7) "max:500"
    ]
    'title' => array(1) [
        string (8) "required"
    ]
]

Support or Contact

If you have some problem, Contact teepluss@gmail.com

Support via PayPal