<p>You can create a custom validation at the <code>EVENT_BEFORE_SAVE</code> event</p>

<pre><code>Event::on(Entry::class,
    Entry::EVENT_BEFORE_SAVE,
    function(ModelEvent $event){
        /** @var Entry $entry */
        $entry = $event-&gt;sender;
        // you can check for the section and the type

        $model = DynamicModel::validateData(['title' =&gt; $entry-&gt;title], [
            [['title'], 'string', 'max' =&gt; 2, 'message'=&gt;'{attribute} should not be longer than 2 characters']
        ]);
        if($model-&gt;hasErrors()){
            $entry-&gt;addErrors($model-&gt;getErrors());
            $event-&gt;isValid = false;
        }
});
</code></pre>

<p>You can leave remove the <code>message</code> attribute if you don't want to use a custom error message, I just wanted to provide a full example how to validate data in Yii2. That way you can define whatever rule you want</p>

CC BY-SA 3.0
2018-04-27T21:20:53.810Z
Thanks Robin, that is above my dev threshold level :) . I took a look in SavableComponent.php but not sure if that is where I make the change.
Vin
CC BY-SA 3.0
2018-04-27T21:25:39.143Z
It's actually not that hard. If you are going to create a more complex site you'll need to create plugins. You should read the docs https://github.com/craftcms/docs/blob/v3/en/plugin-intro.md this change belongs into a module or plugin. There is not really another way to achieve it
Robin Schambach
CC BY-SA 3.0
2018-04-27T20:37:04.927Z
2018-04-27T20:37:04.927Z
Robin Schambach
<p>Is it possible to limit the title field in an entry?  As with other fields I can adjust so that it only takes a certain amount of characters.</p>

<entry><validation>
Title field character length
# Title field character length

(Asked by: username_0 on 2018-04-27)

Is it possible to limit the title field in an entry? As with other fields I can adjust so that it only takes a certain amount of characters.

**Tags:** entry, validation

---

## 1 answers

### Answer by username_1 on 2018-04-27. Score: 4 | Accepted answer

You can create a custom validation at the EVENT_BEFORE_SAVE event
Event::on(Entry::class,
    Entry::EVENT_BEFORE_SAVE,
    function(ModelEvent $event){
        /** @var Entry $entry */
        $entry = $event->sender;
        // you can check for the section and the type

        $model = DynamicModel::validateData(['title' => $entry->title], [
            [['title'], 'string', 'max' => 2, 'message'=>'{attribute} should not be longer than 2 characters']
        ]);
        if($model->hasErrors()){
            $entry->addErrors($model->getErrors());
            $event->isValid = false;
        }
});

You can leave remove the message attribute if you don't want to use a custom error message, I just wanted to provide a full example how to validate data in Yii2. That way you can define whatever rule you want

> **Comments:**
> **username_0**: Thanks Robin, that is above my dev threshold level :) . I took a look in SavableComponent.php but not sure if that is where I make the change.  
> **username_1**: It's actually not that hard. If you are going to create a more complex site you'll need to create plugins. You should read the docs https://github.com/craftcms/docs/blob/v3/en/plugin-intro.md this change belongs into a module or plugin. There is not really another way to achieve it  

---

