Answers.Body: <p>You could hook into the <code>entries.beforeSaveEntry</code> event like this:</p>
  
  <pre><code>craft()-&gt;on('entries.beforeSaveEntry', function (Event $e) {
  
      // Get the entry we're saving, and it's title
      $entry = $e-&gt;params['entry'];
      $title = $entry-&gt;getContent()-&gt;title;
  
      // Look for existing entries with the same title
      $existingEntry = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry, [
        'title' =&gt; $title,
        'id' =&gt; $entry-&gt;id ? 'not '.$entry-&gt;id : null
      ])-&gt;first();
  
      // If an existing entry is found, prevent saving and pass an error message
      if ($existingEntry) {
        $entry-&gt;addErrors([
          'title' =&gt; Craft::t("An entry titled $title already exists"),
        ]);
        $e-&gt;performAction = false;
      }
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-19T19:28:22.020Z
Answers.LastActivityDate: 2016-11-19T19:28:22.020Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>The example below will continue to save entries with the same title and slug.</p>
  
  <p>Is there a built in validation when using <code>craft()-&gt;entries-&gt;saveEntry</code>?</p>
  
  <pre><code>$entry = new EntryModel();
  $entry-&gt;sectionId = $sectionId;
  $entry-&gt;getContent()-&gt;setAttributes(array(
              'title' =&gt; '123',
          ));
  $re = craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
Tags: <plugin-development><saveentry>
Title: Built in validate of title when using EntryModel?
ThreadText: # Built in validate of title when using EntryModel?
  
  (Asked by: username_0 on 2016-11-19)
  
  The example below will continue to save entries with the same title and slug.
  
  Is there a built in validation when using `craft()->entries->saveEntry`?
  
  ```
  $entry = new EntryModel();
  $entry->sectionId = $sectionId;
  $entry->getContent()->setAttributes(array(
              'title' => '123',
          ));
  $re = craft()->entries->saveEntry($entry);
  
  ```
  
  **Tags:** plugin-development, saveentry
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-11-19. Score: 2 | Accepted answer
  
  You could hook into the `entries.beforeSaveEntry` event like this:
  
  ```
  craft()->on('entries.beforeSaveEntry', function (Event $e) {
  
      // Get the entry we're saving, and it's title
      $entry = $e->params['entry'];
      $title = $entry->getContent()->title;
  
      // Look for existing entries with the same title
      $existingEntry = craft()->elements->getCriteria(ElementType::Entry, [
        'title' => $title,
        'id' => $entry->id ? 'not '.$entry->id : null
      ])->first();
  
      // If an existing entry is found, prevent saving and pass an error message
      if ($existingEntry) {
        $entry->addErrors([
          'title' => Craft::t("An entry titled $title already exists"),
        ]);
        $e->performAction = false;
      }
  });
  
  ```
  
  
  ---
  
  