2

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

1 Answer 1

1

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)
    }
);
1
  • 1
    Thank you :) @Mats Mikkel Rummelhoff It is working now.
    – Apple
    Feb 28 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.