Last active
June 28, 2023 13:37
-
-
Save janhenckens/f634c072d8e7512430281809f5154a5c to your computer and use it in GitHub Desktop.
Make first & last name required for Craft CMS users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use craft\base\Element; | |
use craft\elements\User; | |
use craft\events\ModelEvent; | |
use yii\base\Event; | |
Event::on(User::class, Element::EVENT_BEFORE_SAVE, function(ModelEvent $event) { | |
/** @var User $user */ | |
$user = $event->sender; | |
if(!$user->firstName || !$user->lastName) { | |
$event->isValid = false; | |
} | |
if(!$user->firstName ) { | |
$event->sender->addError('firstName', 'First name cannot be blank'); | |
} | |
if(!$user->lastName) { | |
$event->sender->addError('lastName', 'Last name cannot be blank'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment