<p>First, you should test the <code>sender</code> property pointing to the <code>User</code> element. </p>

<p>Second, there's no such property as <code>isNew</code> in that case.</p>

<p>However, you could test that user is new given that its ID won't be set in case the user has not been saved yet.</p>

<p>Something like:</p>

<pre class="lang-php prettyprint-override"><code>Event::on(
    User::class, // \craft\elements\User
    User::EVENT_BEFORE_VALIDATE,
    function (\yii\base\ModelEvent $event) {
        $sender = $event-&gt;sender;
        Craft::debug("User " . ($sender-&gt;id ? "already exists" : "is new"), __METHOD__);
    }
);
</code></pre>

CC BY-SA 4.0
2019-07-08T08:25:49.223Z
2019-07-08T08:25:49.223Z
Alban Jubert
<p>Thank you for answering my question.
Since I would like to use if user is new or not later, I edited the code like this.</p>

<pre><code>$sender_id = $event-&gt;sender-&gt;id;
if ($sender_id) $is_new = false; else $is_new = true;
</code></pre>

CC BY-SA 4.0
2019-07-09T18:30:35.520Z
2019-07-09T18:30:35.520Z
Kota.M
<p>I have a registration form and its custom validation in PHP plugin code.</p>

<p>However, the validation works when user set a new password.
I supposed that $event->isNew is good to detect if it is a new user registration or not, but the element does not work.
Craft says it is unknown property.</p>

<p>Could you tell me how to detect the kinds of submission?</p>

<pre><code>// Registration Form
$request = Craft::$app-&gt;getRequest();
Event::on(
    User::class,
    Element::EVENT_BEFORE_VALIDATE,
    function (Event $event) use($request) {
        // If a user already logged in we are not on the public registration page
        if (Craft::$app-&gt;getUser()-&gt;getIdentity()) return;

        if (!$event-&gt;isNew) return;

        // Do the custom validation
        if (!$event-&gt;sender-&gt;firstName) {
            $event-&gt;sender-&gt;addError('firstName', Craft::t('yii', '{attribute} cannot be blank.', [ 'attribute' =&gt; Craft::t('app', 'First Name') ]));
            $event-&gt;isValid = false;
        }
...
</code></pre>

<craft3><php><password>
Prevent EVENT_BEFORE_VALIDATE in specific case
# Prevent EVENT_BEFORE_VALIDATE in specific case

(Asked by: username_0 on 2019-07-02)

I have a registration form and its custom validation in PHP plugin code.
However, the validation works when user set a new password.
I supposed that $event->isNew is good to detect if it is a new user registration or not, but the element does not work.
Craft says it is unknown property.
Could you tell me how to detect the kinds of submission?
// Registration Form
$request = Craft::$app->getRequest();
Event::on(
    User::class,
    Element::EVENT_BEFORE_VALIDATE,
    function (Event $event) use($request) {
        // If a user already logged in we are not on the public registration page
        if (Craft::$app->getUser()->getIdentity()) return;

        if (!$event->isNew) return;

        // Do the custom validation
        if (!$event->sender->firstName) {
            $event->sender->addError('firstName', Craft::t('yii', '{attribute} cannot be blank.', [ 'attribute' => Craft::t('app', 'First Name') ]));
            $event->isValid = false;
        }
...

**Tags:** craft3, php, password

---

## 2 answers

### Answer by username_1 on 2019-07-08. Score: 2 | Accepted answer

First, you should test the sender property pointing to the User element. 
Second, there's no such property as isNew in that case.
However, you could test that user is new given that its ID won't be set in case the user has not been saved yet.
Something like:
Event::on(
    User::class, // \craft\elements\User
    User::EVENT_BEFORE_VALIDATE,
    function (\yii\base\ModelEvent $event) {
        $sender = $event->sender;
        Craft::debug("User " . ($sender->id ? "already exists" : "is new"), __METHOD__);
    }
);


---

### Answer by username_0 on 2019-07-09. Score: 0

Thank you for answering my question.
Since I would like to use if user is new or not later, I edited the code like this.
$sender_id = $event->sender->id;
if ($sender_id) $is_new = false; else $is_new = true;


---

