Body: <p>I am creating a plugin with a number of custom elements. </p>
  
  <p>Being new to craft, I have come a long way using documentation and skimming existing code. I am stuck on the following issue. I added a field layout to a custom element following the instructions in the craft documentation (<a href="https://docs.craftcms.com/v3/extend/element-types.html#element-content" rel="nofollow noreferrer">https://docs.craftcms.com/v3/extend/element-types.html#element-content</a>). </p>
  
  <p>I created the cp templates to edit and save the layout. I made the cp templates to create/edit the custom element. The field layout comes up nice and I can edit and save all the fields except when I add an asset field. I my case I want to add a photo. The asset field comes up in the screen, I can select a photo, it will show on the screen. But when I save en go back to the edit , the image is not saved. </p>
  
  <p>I figured out that that field text content is saved in the content table and this is working fine (save, edit, delete ...) The assets seems to be handled in the relation database and I see nothing being saved in there. When I use my asset field for example in the user field layout , the data is saved. I double checked the docu and other code an do not seem to find anything else that explicitly saves this relation in the database. </p>
  
  <p>I am developing on the latest version of Craft. Any help would be appreciated</p>
  
  <p>Thank you</p>
  
  <p>Bart</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T16:57:02.017Z
Comments.Text: if you return to edit page, probably there is an error on saving element. so could you check this:  if (Craft::$app->elements->saveElement($element)){
  craft::dd($element->getErrors())
  } and see if there is any error?
Comments.UserDisplayName: user9975
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T18:00:31.347Z
Comments.Text: Hi, get an empty array back (ie no errors).... The element saves as expected , including any text fields from the field layout
Comments.UserDisplayName: Bart Roelands
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T18:15:02.427Z
Comments.Text: could you see posted asset param via browser console? it is something like fields[assetfieldhandle][]='asset id'. also make your asset field required and see if element saves again or not.
Comments.UserDisplayName: user9975
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T18:41:55.283Z
Comments.Text: also go to your field settings for that asset and make sure if Validate related assets is enabled to prevent element save on asset error.
Comments.UserDisplayName: user9975
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T18:46:17.560Z
Comments.Text: Strange this is when I make the field required, and leave it empty, no errors are generated when saving the element ...
Comments.UserDisplayName: Bart Roelands
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T18:52:25.063Z
Comments.Text: I get the fields from the post request using the function setFieldValuesFromRequest('fields'); When I dump the request and add an asset I get following array [
      'moduleDisplayName' => 'Test 1'
      'moduleDisplaySummary' => '<p>Test 1</p>'
      'moduleDisplayImage' => [
          0 => '998'
      ]
  ]   The text fields are saved (in content table), the Image not (in relations table)
Comments.UserDisplayName: Bart Roelands
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T18:55:53.680Z
Comments.Text: what about this?  "also go to your field settings for that asset and make sure if Validate related assets is enabled to prevent element save on asset error"
Comments.UserDisplayName: user9975
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T18:59:42.843Z
Comments.Text: That is set, also gives no errors when left empty. The strange this is, when I use teh same fields in a field layout for example with the standard craft user, the asset is save correctly. Does not seem to be a field problem. There must be something wrong in the way I use the field layout in my custom element, although I followed anything in the manual ...
Comments.UserDisplayName: Bart Roelands
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T19:06:32.380Z
Comments.Text: I found it ! Turned out I forgot to call  parent::afterSave($isNew); in the Element aftersave function ....
Comments.UserDisplayName: Bart Roelands
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-02T19:09:36.413Z
Comments.Text: glad you found it. you can post your answer now :)
Comments.UserDisplayName: user9975
Tags: <field-layout-designer>
Title: Asset field in custom element field layout not saved
ThreadText: # Asset field in custom element field layout not saved
  
  (Asked by: username_0 on 2020-01-01)
  
  I am creating a plugin with a number of custom elements.
  
  Being new to craft, I have come a long way using documentation and skimming existing code. I am stuck on the following issue. I added a field layout to a custom element following the instructions in the craft documentation (<https://docs.craftcms.com/v3/extend/element-types.html#element-content>).
  
  I created the cp templates to edit and save the layout. I made the cp templates to create/edit the custom element. The field layout comes up nice and I can edit and save all the fields except when I add an asset field. I my case I want to add a photo. The asset field comes up in the screen, I can select a photo, it will show on the screen. But when I save en go back to the edit , the image is not saved.
  
  I figured out that that field text content is saved in the content table and this is working fine (save, edit, delete ...) The assets seems to be handled in the relation database and I see nothing being saved in there. When I use my asset field for example in the user field layout , the data is saved. I double checked the docu and other code an do not seem to find anything else that explicitly saves this relation in the database.
  
  I am developing on the latest version of Craft. Any help would be appreciated
  
  Thank you
  
  username_0
  
  **Tags:** field-layout-designer
  
  > **Comments:**
  > **username_1**: if you return to edit page, probably there is an error on saving element. so could you check this:  if (Craft::$app->elements->saveElement($element)){ craft::dd($element->getErrors()) } and see if there is any error?  
  > **username_0**: Hi, get an empty array back (ie no errors).... The element saves as expected , including any text fields from the field layout  
  > **username_1**: could you see posted asset param via browser console? it is something like fields[assetfieldhandle][]='asset id'. also make your asset field required and see if element saves again or not.  
  > **username_1**: also go to your field settings for that asset and make sure if Validate related assets is enabled to prevent element save on asset error.  
  > **username_0**: Strange this is when I make the field required, and leave it empty, no errors are generated when saving the element ...  
  > **username_0**: I get the fields from the post request using the function setFieldValuesFromRequest('fields'); When I dump the request and add an asset I get following array [     'moduleDisplayName' => 'Test 1'     'moduleDisplaySummary' => '<p>Test 1</p>'     'moduleDisplayImage' => [         0 => '998'     ] ]   The text fields are saved (in content table), the Image not (in relations table)  
  > **username_1**: what about this?  "also go to your field settings for that asset and make sure if Validate related assets is enabled to prevent element save on asset error"  
  > **username_0**: That is set, also gives no errors when left empty. The strange this is, when I use teh same fields in a field layout for example with the standard craft user, the asset is save correctly. Does not seem to be a field problem. There must be something wrong in the way I use the field layout in my custom element, although I followed anything in the manual ...  
  > **username_0**: I found it ! Turned out I forgot to call  parent::afterSave($isNew); in the Element aftersave function ....  
  > **username_1**: glad you found it. you can post your answer now :)  
  
  ---
  
  ## 0 answers
  
  