<p><code>isValid</code> should be set on the <code>$event</code> variable, not on <code>$event-&gt;element</code>:</p>
<pre><code>Event::on(Process::class, Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, function (FeedProcessEvent $event) {
    $myField=$event-&gt;feedData['myfieldname'];
    if ($myField == null){
        $event-&gt;isValid=false;
    }
});
</code></pre>
<p>I haven't tested but looking at the <a href="https://github.com/craftcms/feed-me/blob/develop/src/services/Process.php#L420" rel="nofollow noreferrer">feed-me source code</a>, this should work.</p>

CC BY-SA 4.0
2020-11-26T15:10:43.070Z
Thanks for the answer. I got it working, but I'm looking to access the field's in FeedMe. I have a field in FeedMe called "Merchants" but some of imported entries won't get matched up to anything, so I'm trying to stop them being saved if they don't match up.
JMKelley
CC BY-SA 4.0
2020-11-26T15:17:25.207Z
Updated the question above.
JMKelley
CC BY-SA 4.0
2020-11-25T16:51:11.917Z
2020-11-25T16:51:11.917Z
nstCactus
<p><strong>Updated</strong></p>
<p>After a lot of tinkering - I finally found out how to check if the field is matching/existing and if nothing matches - Skip adding this element.</p>
<pre><code>Event::on(
Process::class, 
Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, 
function (craft\feedme\events\FeedProcessEvent $event) {
  switch($event-&gt;feed['name']){
    case 'offers':

      $merchants = $event-&gt;element-&gt;merchants;
      if (sizeof($merchants) === 0) {
          $event-&gt;isValid=false;
      }
    break;
  }
});
</code></pre>

CC BY-SA 4.0
2020-11-30T16:40:06.227Z
Looks like the only thing that differs from nstCactus' answer is your additional check code?  IMO that doesn't really improve the answer as the key is in the ``$event->isValid`` line which was pointed out in that answer. (You also didn't mention in the question that ``$merchants`` is supposed to be an array?)
RitterKnight
CC BY-SA 4.0
2020-11-30T18:42:16.420Z
Sure, I've reimbursed the internet points.
JMKelley
CC BY-SA 4.0
2020-11-27T12:56:56.103Z
2020-11-27T12:56:56.103Z
JMKelley
<p>Is there a way on Feedme to not import an entry if it doesn't have a match on a specific field?</p>
<blockquote>
<p>e.g. If there is no entry that it matches up to, don't import it.</p>
</blockquote>
<p>Here's the current code I have:</p>
<pre><code>Event::on(Process::class, Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, function (FeedProcessEvent $event) {
    $myField=$event-&gt;feedData['myfieldname'];
    if ($myField == null){
        $event-&gt;element-&gt;isValid=false;
    }
});
</code></pre>
<p>The above processes the data getting loaded in. Here's code for processing data after it's parsed.</p>
<pre><code>Event::on(Fields::class, Fields::EVENT_AFTER_PARSE_FIELD, function(FieldEvent $event) {
    if ($event-&gt;fieldHandle['merchants'] == null) {
    $event-&gt;isValid=false;
    }
});
</code></pre>

<plugin-development><plugin-feedme>
Feedme Skip Entry if No Matching Elements on Field
# Feedme Skip Entry if No Matching Elements on Field

(Asked by: username_0 on 2020-11-22)

Is there a way on Feedme to not import an entry if it doesn't have a match on a specific field?

e.g. If there is no entry that it matches up to, don't import it.

Here's the current code I have:
Event::on(Process::class, Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, function (FeedProcessEvent $event) {
    $myField=$event->feedData['myfieldname'];
    if ($myField == null){
        $event->element->isValid=false;
    }
});

The above processes the data getting loaded in. Here's code for processing data after it's parsed.
Event::on(Fields::class, Fields::EVENT_AFTER_PARSE_FIELD, function(FieldEvent $event) {
    if ($event->fieldHandle['merchants'] == null) {
    $event->isValid=false;
    }
});

**Tags:** plugin-development, plugin-feedme

---

## 2 answers

### Answer by username_1 on 2020-11-25. Score: 2 | Accepted answer

isValid should be set on the $event variable, not on $event->element:
Event::on(Process::class, Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, function (FeedProcessEvent $event) {
    $myField=$event->feedData['myfieldname'];
    if ($myField == null){
        $event->isValid=false;
    }
});

I haven't tested but looking at the feed-me source code, this should work.

> **Comments:**
> **username_0**: Thanks for the answer. I got it working, but I'm looking to access the field's in FeedMe. I have a field in FeedMe called "Merchants" but some of imported entries won't get matched up to anything, so I'm trying to stop them being saved if they don't match up.  
> **username_0**: Updated the question above.  

---

### Answer by username_0 on 2020-11-27. Score: 0

Updated
After a lot of tinkering - I finally found out how to check if the field is matching/existing and if nothing matches - Skip adding this element.
Event::on(
Process::class, 
Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, 
function (craft\feedme\events\FeedProcessEvent $event) {
  switch($event->feed['name']){
    case 'offers':

      $merchants = $event->element->merchants;
      if (sizeof($merchants) === 0) {
          $event->isValid=false;
      }
    break;
  }
});

> **Comments:**
> **username_2**: Looks like the only thing that differs from nstCactus' answer is your additional check code?  IMO that doesn't really improve the answer as the key is in the ``$event->isValid`` line which was pointed out in that answer. (You also didn't mention in the question that ``$merchants`` is supposed to be an array?)  
> **username_0**: Sure, I've reimbursed the internet points.  

---

