PHPackages                             laxity7/yii2-trim-behavior - 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. laxity7/yii2-trim-behavior

ActiveYii2-extension[Validation &amp; Sanitization](/categories/validation)

laxity7/yii2-trim-behavior
==========================

Behavior that truncates all spaces and other characters in all attributes in a model before validate

1.0.2(1y ago)14.4k↓50%MITPHPPHP &gt;=7.4|&gt;=8.0

Since Jul 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/laxity7/yii2-trim-behavior)[ Packagist](https://packagist.org/packages/laxity7/yii2-trim-behavior)[ Docs](https://github.com/laxity7/yii2-trim-behavior)[ RSS](/packages/laxity7-yii2-trim-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

Yii2 TrimBehavior
=================

[](#yii2-trimbehavior)

[![License](https://camo.githubusercontent.com/9c447cb017f9ecdfb1233007c8761cca3272d3219d373f95872455654a524559/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c6178697479372f796969322d7472696d2d6265686176696f722e737667)](https://github.com/laxity7/yii2-trim-behavior/blob/master/LICENSE)[![Latest Stable Version](https://camo.githubusercontent.com/f45dff5c0f43c9d601a32aff10e63ea8a0d315279792434000d5e682571741c0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6178697479372f796969322d7472696d2d6265686176696f722e737667)](https://packagist.org/packages/laxity7/yii2-trim-behavior)[![Total Downloads](https://camo.githubusercontent.com/727d2fdb389fb90ff67e9da256032355a37e0dd651eb83f6fd2bbe13e07c8fd5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6178697479372f796969322d7472696d2d6265686176696f722e737667)](https://packagist.org/packages/laxity7/yii2-trim-behavior)

This behavior automatically truncates all spaces and other characters in all attributes in a model before validate. The easiest way is to add this behavior to the parent class so that all attributes are always trimmed.

> Note: Behavior automatically skips any non-string and empty values.

Install
-------

[](#install)

Install via composer

```
composer require laxity7/yii2-trim-behavior
```

Or you may add dependency manually in composer.json:

```
 "laxity7/yii2-trim-behavior": "*"

```

How to use
----------

[](#how-to-use)

To use TrimBehavior, insert the following code to your Model/ActiveRecord class:

```
/** @inheritdoc */
public function behaviors(): array
{
   return [
       'trimAttributes' => \laxity7\yii2\behaviors\TrimBehavior::class, // trim all attributes
       // ... other behaviors
   ];
}
```

You can also pass the following parameters:

- **fields** `string[]` (by default `[]`) List of fields to process. By default all fields.
- **mask** `string` (by default `' \t\n\r �'`) Simply list all characters that you want to be stripped. You can use TrimBehavior::DEFAULT\_MASK to combine your characters and defaults
- **once** `string[]` (by default `true`) Run the behavior once and detach after.

So, the complete list of settings will look like this:

```
/** @inheritdoc */
public function behaviors(): array
{
    return [
        [
            'class'  => \laxity7\yii2\behaviors\TrimBehavior::class,
            'fields' => ['foo', 'bar'],
            'mask'   => '_' . TrimBehavior::DEFAULT_MASK,
            'once'   => false,
        ],
    ];
}
```

If you want to trim only certain fields in a certain class, then better to use the filter in rules of validation

```
/** @inheritdoc */
public function rules()
{
    return [
        [['filedName1', 'filedName1'], 'filter', 'filter' => 'trim'],
        // ... other rules
    ];
}
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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.

###  Release Activity

Cadence

Every ~897 days

Total

3

Last Release

710d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.5

1.0.2PHP &gt;=7.4|&gt;=8.0

### Community

Maintainers

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

---

Top Contributors

[![laxity7](https://avatars.githubusercontent.com/u/6792144?v=4)](https://github.com/laxity7 "laxity7 (3 commits)")

---

Tags

phpyii2yii2-extensionbehaviorsyii2trim

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/laxity7-yii2-trim-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/laxity7-yii2-trim-behavior/health.svg)](https://phpackages.com/packages/laxity7-yii2-trim-behavior)
```

###  Alternatives

[borales/yii2-phone-input

Yii2 International telephone numbers - Asset Bundle, Behavior, Validator, Widget

1341.6M6](/packages/borales-yii2-phone-input)[codeonyii/yii2-at-least-validator

Validates at least one (or more) attributes.

28253.5k1](/packages/codeonyii-yii2-at-least-validator)[arogachev/yii2-many-to-many

Many-to-many ActiveRecord relation for Yii 2 framework

3541.2k4](/packages/arogachev-yii2-many-to-many)[yii2mod/yii2-validators

Collection of useful validators for Yii Framework 2.0

1816.8k](/packages/yii2mod-yii2-validators)

PHPackages © 2026

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