Answers.Body: <p>Found a solution by how <a href="http://buildwithcraft.com/docs/templating/entry-form" rel="nofollow">Entry Form</a> handles it.</p>
  
  <p>Assuming you've posted fields with names such as jobData[reference]:</p>
  
  <pre><code>$fieldsLocation = craft()-&gt;request-&gt;getParam('fieldsLocation', 'jobData');
  
  $this-&gt;entry-&gt;setContentFromPost($fieldsLocation);
  </code></pre>
  
  <p>The setContentFromPost() method of the EntriesController class sets the ElementType.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-27T20:24:56.397Z
Answers.LastActivityDate: 2014-11-27T20:24:56.397Z
Answers.UserDisplayName: Russ Back
Body: <p>I am trying to create a new entry via a form post that will come from a third party, by doing this in my plugin's service class:</p>
  
  <pre><code>$entry = EntryModel::populateModel(array(
      'sectionId' =&gt; 1, 
      'typeId' =&gt; 2
  ));
  
  $entry-&gt;getContent()-&gt;setAttribute('reference', craft()-&gt;request-&gt;getPost('someReferenceField'));
  
  craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
  <p>The reference field is a custom FieldType (<a href="https://github.com/fuel-digital/Craft-Unique-Text-FieldType/blob/develop/uniquetextfield/fieldtypes/UniqueTextField_UniqueTextFieldType.php" rel="nofollow">https://github.com/fuel-digital/Craft-Unique-Text-FieldType/blob/develop/uniquetextfield/fieldtypes/UniqueTextField_UniqueTextFieldType.php</a>) where I am validating the submitted value is unique for this EntryType (in this case an Entry rather than a User/Asset/etc).</p>
  
  <p>That FieldType works fine in the control panel but I'm getting an error when submitting via my plugin as $this->element is null within the FieldType's valueExists() method.</p>
  
  <p>I can get around this by creating a new EntryModel like so:</p>
  
  <pre><code>/**
   * Checks if another entry exists with the given value
   * 
   * @param  string $value The value to check
   * @return bool
   */
  private function valueExists($value)
  {
      if ( ! $this-&gt;element)
      {
          $this-&gt;element = new EntryModel();
      }
  
      $existing = craft()-&gt;elements-&gt;getCriteria($this-&gt;element-&gt;elementType)-&gt;first(array(
          $this-&gt;model-&gt;handle =&gt; $value
      ));
  
      return ($existing &amp;&amp; $existing-&gt;id != $this-&gt;element-&gt;id);
  }
  </code></pre>
  
  <p>But that means the FieldType will only work with Entry elements, and not Users, Assets etc.</p>
  
  <p>As the plugin works fine in the control panel, I assume I'm missing something in my plugin service?</p>
  
Tags: <plugin-development><fieldtypes><validation>
Title: FieldType's element attribute is null
ThreadText: # FieldType's element attribute is null
  
  (Asked by: username_0 on 2014-11-27)
  
  I am trying to create a new entry via a form post that will come from a third party, by doing this in my plugin's service class:
  
  ```
  $entry = EntryModel::populateModel(array(
      'sectionId' => 1, 
      'typeId' => 2
  ));
  
  $entry->getContent()->setAttribute('reference', craft()->request->getPost('someReferenceField'));
  
  craft()->entries->saveEntry($entry);
  
  ```
  
  The reference field is a custom FieldType (<https://github.com/fuel-digital/Craft-Unique-Text-FieldType/blob/develop/uniquetextfield/fieldtypes/UniqueTextField_UniqueTextFieldType.php>) where I am validating the submitted value is unique for this EntryType (in this case an Entry rather than a User/Asset/etc).
  
  That FieldType works fine in the control panel but I'm getting an error when submitting via my plugin as $this->element is null within the FieldType's valueExists() method.
  
  I can get around this by creating a new EntryModel like so:
  
  ```
  /**
   * Checks if another entry exists with the given value
   * 
   * @param  string $value The value to check
   * @return bool
   */
  private function valueExists($value)
  {
      if ( ! $this->element)
      {
          $this->element = new EntryModel();
      }
  
      $existing = craft()->elements->getCriteria($this->element->elementType)->first(array(
          $this->model->handle => $value
      ));
  
      return ($existing && $existing->id != $this->element->id);
  }
  
  ```
  
  But that means the FieldType will only work with Entry elements, and not Users, Assets etc.
  
  As the plugin works fine in the control panel, I assume I'm missing something in my plugin service?
  
  **Tags:** plugin-development, fieldtypes, validation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2014-11-27. Score: 1
  
  Found a solution by how [Entry Form](http://buildwithcraft.com/docs/templating/entry-form) handles it.
  
  Assuming you've posted fields with names such as jobData[reference]:
  
  ```
  $fieldsLocation = craft()->request->getParam('fieldsLocation', 'jobData');
  
  $this->entry->setContentFromPost($fieldsLocation);
  
  ```
  
  The setContentFromPost() method of the EntriesController class sets the ElementType.
  
  
  ---
  
  