Git Product home page Git Product logo

shield's Introduction

Artesaos - Shield

Artesãos Shield provides you a simple way to centralize your validation rules. It was mainly designed to solve the FormRequest rules practice, that is valid outside HTTP requests.

Installing

The package installation can be done with composer by the following command:

composer require artesaos/shield

Shield does not provides Facades or ServiceProviders, they aren't needed.

Usage

1 - Defining Rules

Operating Shield is simple. It starts by defining a rules class for your model or other kind of entity. Take a look on the following example:

<?php

namespace App\Domains\Users\Rules;

use Artesaos\Shield\Rules

class UserRules extends Rules
{
	public function defaultRules()
	{
		return [
			'name' => 'required|min:6',
		];
	}

	public function creating()
	{
		// returnRules method should be used
		// whenever the rules should be merged
		// with the default ones.
		return $this->returnRules([
			'email' => 'required|email',
		]);
	}

	// any other methods / actions that needs rules

}

2 - Enabling Rules

You could instantiate the rules by hand, but the recommended way of doing it is setting a static property into the class that owns the rules and using the proper trait

<?php

// some other use statements here
use Artesaos\Shield\HasRules;
use App\Domains\Users\Rules\UserRules;

class User extends Model
{
	// using the rules trait
	use HasRules;

	// setting the rules class
	protected static $rulesFrom = UserRules::class

	// some model stuff here
}

3 - Usage

Whenever you need to access the rules, you can do it by creating a new instance of the rules class, or just using the classes (mainly models) you enabled.

User::rules()->creating();

User::rules()->updating();

User::rules()->yourCustomMethodForACustomAction();

User::rules()->whatever();

A really nice way of using it inside FormRequests are by passing the current HTTP method, that will be translated to the corresponding rules method (that's a convention)

// inside a form request
User::rules()->byRequestType($this->getMethod());

// wherever you have a request instance
User::rules()->byRequestType($request->getMethod());

Credits

shield's People

Contributors

vinicius73 avatar

Watchers

James Cloos avatar Gabriel Pedro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.