Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Make first & last name required for Craft CMS users
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
You can’t perform that action at this time.