<p>It seems like the problem is that you're checking for a duplicate field value without taking site propagation into account – i.e. when Craft tries to propagate the newly saved entry to the other sites, your <code>EVENT_BEFORE_SAVE</code> event handler kicks in for those propagated entries as well and the error is thrown because at that point there already is an entry (i.e. the &quot;original&quot; one) with that <code>businessNumber</code> field value.</p>
<p>The easiest solve here is probably to make sure the event handler logic doesn't execute if the entry being saved is being propagated:</p>
<pre class="lang-php prettyprint-override"><code>Event::on(
    Entry::class,
    Entry::EVENT_BEFORE_SAVE,
    function(ModelEvent $event) {
        $entry = $event-&gt;sender;
        if ($entry-&gt;propagating) {
            return;
        }
        ...(the rest of your event handler logic)
    }
);
</code></pre>

CC BY-SA 4.0
2022-02-28T10:28:37.440Z
Thank you :) @Mats Mikkel Rummelhoff  It is working now.
Apple
CC BY-SA 4.0
2022-02-28T09:48:22.373Z
2022-02-28T09:48:22.373Z
Mats Mikkel Rummelhoff
<p>I am working on the <a href="https://craftcms.com/knowledge-base/entry-form" rel="nofollow noreferrer">Entry Form</a> for a shop entry (Channel section type). For the shop entry, there is one custom field named &quot;businessNumber&quot; that must be unique. All shop entries in a single site must have a unique &quot;businessNumber&quot;. The site is a multi-site. One is Dutch and another in English. The new shop entry will save into both sites.</p>
<p>For the solution, I create a custom module. In the init function, I add the code below.</p>
<pre><code>if( !Craft::$app-&gt;getRequest()-&gt;getIsCpRequest() &amp;&amp; $GET_querystring == 'p=shop/register' ) {
     Event::on(
        Entry::class,
        Entry::EVENT_BEFORE_SAVE,
        function(ModelEvent  $event) { 
              $isShopSectionId = Craft::$app-&gt;getRequest()-&gt;post('sectionId');
              $businessNumber = Craft::$app-&gt;getRequest()-&gt;post('fields')['businessNumber'];
    
              if($isShopSectionId == Craft::$app-&gt;sections-&gt;getSectionByHandle('shops')-&gt;id) {
                  $businessNumberFound = Entry::find()
                   -&gt;section('shops')
                   -&gt;where(['and',&quot;`content`.`field_businessNumber_lbxjwvbf`=:businessNumber&quot;])
                   -&gt;addParams([':businessNumber'=&gt;$businessNumber])
                   -&gt;status(['live','pending','expired','disabled'])
                   -&gt;exists(); 

                  if($businessNumberFound){
                       $event-&gt;isValid = false;
                       $event-&gt;sender-&gt;addError('businessNumber', Craft::t('site','Business number is already exists'));
                  }
              }
}
</code></pre>
<p>The code above works when a user enters the duplicate business number on the front-end form. When the user enters the unique business number, it passes the duplicate validation and no validation error shows on the form. So the submit form is processed. Then it throws the  &quot;Couldn’t propagate element to other site.&quot; error on the screen. I check the log and see the message says &quot;Couldn’t propagate element to other site.&quot; on validation error and I see the message says &quot;Business number  already exists&quot; validation message in the log file.</p>
<p>I try other events such as EVENT_BEFORE_VALIDATE but it throws the same error.</p>
<p>I also try to set the unique rule on EVENT_DEFINE_RULES as below. But it doesn't work since the unique validator works with the model that accesses DB. So I am not sure which Class I need to set for 'targetClass' since 'businessNumber' is the custom field.</p>
<pre><code>$rules[] = ['businessNumber', 'unique', 'targetClass' =&gt; Entry::class, 'targetAttribute' =&gt; 'businessNumber'];
</code></pre>
<p>I am wondering if anyone experienced the same issue I have. If so, how to solve it. A sample of code would be helpful.</p>
<p>Thanks</p>

<custom-field><validation><entry-form>
How to validate custom field duplication on Front end form
# How to validate custom field duplication on Front end form

(Asked by: username_0 on 2022-02-26)

I am working on the Entry Form for a shop entry (Channel section type). For the shop entry, there is one custom field named "businessNumber" that must be unique. All shop entries in a single site must have a unique "businessNumber". The site is a multi-site. One is Dutch and another in English. The new shop entry will save into both sites.
For the solution, I create a custom module. In the init function, I add the code below.
if( !Craft::$app->getRequest()->getIsCpRequest() && $GET_querystring == 'p=shop/register' ) {
     Event::on(
        Entry::class,
        Entry::EVENT_BEFORE_SAVE,
        function(ModelEvent  $event) { 
              $isShopSectionId = Craft::$app->getRequest()->post('sectionId');
              $businessNumber = Craft::$app->getRequest()->post('fields')['businessNumber'];
    
              if($isShopSectionId == Craft::$app->sections->getSectionByHandle('shops')->id) {
                  $businessNumberFound = Entry::find()
                   ->section('shops')
                   ->where(['and',"`content`.`field_businessNumber_lbxjwvbf`=:businessNumber"])
                   ->addParams([':businessNumber'=>$businessNumber])
                   ->status(['live','pending','expired','disabled'])
                   ->exists(); 

                  if($businessNumberFound){
                       $event->isValid = false;
                       $event->sender->addError('businessNumber', Craft::t('site','Business number is already exists'));
                  }
              }
}

The code above works when a user enters the duplicate business number on the front-end form. When the user enters the unique business number, it passes the duplicate validation and no validation error shows on the form. So the submit form is processed. Then it throws the  "Couldn’t propagate element to other site." error on the screen. I check the log and see the message says "Couldn’t propagate element to other site." on validation error and I see the message says "Business number  already exists" validation message in the log file.
I try other events such as EVENT_BEFORE_VALIDATE but it throws the same error.
I also try to set the unique rule on EVENT_DEFINE_RULES as below. But it doesn't work since the unique validator works with the model that accesses DB. So I am not sure which Class I need to set for 'targetClass' since 'businessNumber' is the custom field.
$rules[] = ['businessNumber', 'unique', 'targetClass' => Entry::class, 'targetAttribute' => 'businessNumber'];

I am wondering if anyone experienced the same issue I have. If so, how to solve it. A sample of code would be helpful.
Thanks

**Tags:** custom-field, validation, entry-form


---

## 1 answers

### Answer by username_1 on 2022-02-28. Score: 1 | Accepted answer

It seems like the problem is that you're checking for a duplicate field value without taking site propagation into account – i.e. when Craft tries to propagate the newly saved entry to the other sites, your EVENT_BEFORE_SAVE event handler kicks in for those propagated entries as well and the error is thrown because at that point there already is an entry (i.e. the "original" one) with that businessNumber field value.
The easiest solve here is probably to make sure the event handler logic doesn't execute if the entry being saved is being propagated:
Event::on(
    Entry::class,
    Entry::EVENT_BEFORE_SAVE,
    function(ModelEvent $event) {
        $entry = $event->sender;
        if ($entry->propagating) {
            return;
        }
        ...(the rest of your event handler logic)
    }
);

> **Comments:**
> **username_0**: Thank you :) @username_1  It is working now.  

---

