PHPackages                             yangmls/laravel-validation-trait - 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. yangmls/laravel-validation-trait

ActiveLibrary

yangmls/laravel-validation-trait
================================

Provide a powerful trait for saving model easily

7112PHP

Since Jun 15Pushed 10y ago1 watchersCompare

[ Source](https://github.com/yangmls/laravel-validation-trait)[ Packagist](https://packagist.org/packages/yangmls/laravel-validation-trait)[ RSS](/packages/yangmls-laravel-validation-trait/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Validation Trait
========================

[](#laravel-validation-trait)

Provide a powerful trait for laravel 4 model

### Use it in Eloquent

[](#use-it-in-eloquent)

create a model and use it

```
use Yangmls\ValidationTrait;

class User extends Eloquent
{
    use ValidationTrait;

    public function rules()
    {
        return [
            'email' => ['required', 'email'],
        ];
    }

    public function ruleMessages()
    {
        return [
            'email.required' => ':attribute is required',
        ];
    }

    public function customAttributes()
    {
        return [
            'email' => 'E-Mail',
        ];
    }
}
```

create a new model and save

```
$model = new User();
$result = $model->saveFromRequest(Input::all()); // true or false
return Response::json($model->getErrors());
```

create a new model without errors

```
$model = User::createFromRequest(Input::all()); // User instance or null
```

save a existing model

```
$model = User::find(1);
$result = $model->saveFromRequest(Input::all()); // true or false
return Response::json($model->getErrors());
```

### Use it in other models

[](#use-it-in-other-models)

sometimes you may process a form without Eloquent, you can do like this

```
use Yangmls\ValidationTrait;

class Login
{
    use ValidationTrait;

    public $attributes;

    public function __construct($input = [])
    {
        $this->attributes = $input;
    }

    public function validate($options = [])
    {
        return $this->validateRequest($this->attributes, $options);
    }

    public function rules()
    {
        return [
            'email' => ['required', 'email'],
        ];
    }

    public function ruleMessages()
    {
        return [
            'email.required' => ':attribute is required',
        ];
    }

    public function customAttributes()
    {
        return [
            'email' => 'E-Mail',
        ];
    }
}
```

then call it in controller

```
$model = new Login(Input::all());
$result = $model->validate(); // true or false
return Response::json($model->getErrors());
```

### Inline Validators

[](#inline-validators)

validator can be defined in the class and will be called automatically

```
use Yangmls\ValidationTrait;

class User extends Eloquent
{
    use ValidationTrait;

    protected function validatorEmail($value, $input, $options)
    {
        // $value is attribute value
        // $input is whole input
        // $options is the config you pass to saveFromRequest

        // Note:
        // 1. you must use addError to stop saving
        // 2. you must return true if you think the validator is passed
    }
}
```

### Built-in hooks

[](#built-in-hooks)

below methods are called automatically when you do validating or saving

`beforeSave`, `afterSave`, `beforeValidate`, `afterValidate`

however you can also use laravel built-in events for saving/creating/updating

### License

[](#license)

under the [MIT license](http://opensource.org/licenses/MIT)

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 72.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/df45c645cf2efb3d25277810fd2b0b5ba2fdbd90a5d2df16acc44f79dbc391ac?d=identicon)[yangmls](/maintainers/yangmls)

---

Top Contributors

[![yangmls](https://avatars.githubusercontent.com/u/1272678?v=4)](https://github.com/yangmls "yangmls (8 commits)")[![kisexu](https://avatars.githubusercontent.com/u/1040190?v=4)](https://github.com/kisexu "kisexu (3 commits)")

### Embed Badge

![Health badge](/badges/yangmls-laravel-validation-trait/health.svg)

```
[![Health](https://phpackages.com/badges/yangmls-laravel-validation-trait/health.svg)](https://phpackages.com/packages/yangmls-laravel-validation-trait)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
