liam_davison
I'm new here

Understanding ValidationErrors in scripts - creating list of links

Jump to solution

Good morning,

FirstSpirit 5.1.605.

I am writing a script to copy and update some pages. As part of the script, I need to add a Section which contains an FS_LIST. The list contains links. This is entirely scripted with no user intervention.

I don't understand the ValidationError I receive when the script tries to save the new page:

ValidationError[element='Previous_guidance' (ID=547055, project=466725), msg={MultiFormValidationReport{(EN:{st_related_content_links:[Validation{SAVE,false:Text},Validation{SAVE,false:Title},Valida...}]

It doesn't help that the error message is truncated!

Here is the script I am using:

Body relatedContent = page.getBodyByName("pt_related_topics_list");

          if(relatedContent.getChildCount() == 0) {

                    SectionTemplate st_related_content = templateStore.getStoreElement("st_related_content",IDProvider.UidType.TEMPLATESTORE);

                    Section guidanceSection = relatedContent.createSection("Previous_guidance",st_related_content);

                    guidanceSection.setLock(true,false);

                    FormData rcFormData = guidanceSection.getFormData();

                    FormField linksField = rcFormData.get(lang,"st_related_content_links");

 

                    FormDataList dataList = linksField.get();

                    producer = dataList.getProducer();

                    lt_internal_link = templateStore.getStoreElement("lt_internal_link",IDProvider.UidType.TEMPLATESTORE_LINKTEMPLATE);

                    formData = producer.create(lt_internal_link,lang);

                    linkT = formData.getTemplate();

                    link = linkT.createLink(lang);

                    linkFD = link.getFormData();

                    lt_link_type = linkFD.get(lang,"lt_link_type");

                    lt_link_type.set(true);

                    lt_title = linkFD.get(lang,"lt_title");

                    lt_title.set("Link Title Bananas");

                    lt_text = linkFD.get(lang,"lt_text");

                    lt_text.set("Link Text Fruit Salad");

                    lt_reference = linkFD.get(lang,"lt_reference");

                    // this is how you set an FS_REFERENCE value

                    originalRef = TargetReference.TargetReferences.newInstance(lang, original, null);

                    lt_reference.set(originalRef);

                    link.setFormData(linkFD);

 

// *** I think the error might be around here? Have I missed a step?

 

                    dataList.add(formData);

                    linksField.set(dataList);

 

                    guidanceSection.setFormData(rcFormData);

                    guidanceSection.save();

                    guidanceSection.setLock(false,false);

 

          }

The link template requires lt_link_type (a CMS_INPUT_TOGGLE), lt_title and lt_text (both CMS_INPUT_TEXT) and lt_reference (an FS_REFERENCE) to be valid.

When I inspect the Section ("Previous_guidance") in SiteArchitect, the list is empty so I don't think I am saving the Section or Form correctly.

Can anyone see any obvious errors in my script? Or help me understand the ValidationError?

Many thanks,

Liam Davison

0 Kudos
1 Solution

Accepted Solutions

Hi Liam,

Ok, with all my explaining about the error, I forgot to look closely at the code you posted. I might be mistaken, but it seems as if you create a form data via the lists producer, but then create a seperate link and form data for setting values, then again storing back the originally created form data.

Your code should work using formData instead of linkFD. No need to create a link object yourself.

Best regards,

Stefan

View solution in original post

0 Kudos
4 Replies
StefanSchulz
I'm new here

Hi,

The error returned is rather meant for programmatical evaluation than reading it. The truncation seems to be caused by the outputting system, the original text would be complete. Not sure, if there is anything we can do about that.

The error states issues, found when validating form data when saving. It denotes the language, the error was discovered in, the input component affected (st_related_content_links) and the states causing the report. The Validation-part says, that validation failed for scope save with the message given, e.g., "Text" and "Title", as these are defined in the rules for the template.

This is all I can read from the information without the template and rules.

Best regards,

Stefan

0 Kudos

Hello Stefan,

That's roughly what I thought was happening, though I don't know why "Text" and "Title" would fail valuation, as I give them values in the script. The link template format (for lt_internal_link) is this:

<CMS_MODULE>

  <CMS_INPUT_TOGGLE name="lt_link_type" type="radio" useLanguages="no">

    <LANGINFOS>

      <LANGINFO lang="*" label="Link Type"/>

    </LANGINFOS>

    <OFF>

      <LANGINFO lang="*" label="News and Events Link"/>

    </OFF>

    <ON>

      <LANGINFO lang="*" label="General Link"/>

    </ON>

  </CMS_INPUT_TOGGLE>

  <CMS_INPUT_TEXT name="lt_text" useLanguages="no">

    <LANGINFOS>

      <LANGINFO lang="*" label="Text" description="Enter url text here"/>

    </LANGINFOS>

  </CMS_INPUT_TEXT>

  <CMS_INPUT_TEXT name="lt_title" useLanguages="no">

    <LANGINFOS>

      <LANGINFO lang="*" label="Title" description="Enter url title here"/>

    </LANGINFOS>

  </CMS_INPUT_TEXT>

  <FS_REFERENCE name="lt_reference" useLanguages="no">

    <FILTER>

      <ALLOW type="pageref"/>

    </FILTER>

    <LANGINFOS>

      <LANGINFO lang="*" label="General Link target" description="Select internal page url here"/>

    </LANGINFOS>

    <PROJECTS>

      <LOCAL name=".">

        <SOURCES>

          <FOLDER name="root" store="sitestore"/>

        </SOURCES>

      </LOCAL>

    </PROJECTS>

  </FS_REFERENCE>

  <FS_DATASET name="lt_news_and_events_reference" allowDelete="no" allowEdit="no" allowNew="no" useLanguages="no">

    <LANGINFOS>

      <LANGINFO lang="*" label="News and Events Link target" description="Select internal news and events url here"/>

    </LANGINFOS>

    <SOURCES>

      <CONTENT name="news_and_events__detail_"/>

    </SOURCES>

  </FS_DATASET>

</CMS_MODULE>

The lt_link_type toggle is used to determine if the link is to a page or a Content2 ("news") item. This is set to true, i.e. normal page, so shouldn't trigger any of the rules around lt_news_and_events_reference. The rules are below:

<RULES>

<ON_SAVE>        

          <WITH>          

                    <NOT>

                              <PROPERTY source="lt_text" name="EMPTY"/>

                    </NOT>           

          </WITH>        

          <DO>            

                    <VALIDATION>                   

                              <PROPERTY source="lt_text" name="VALID"/>                   

                              <MESSAGE lang="*" text="Link text cannot be empty."/>                            

                    </VALIDATION>        

          </DO>    

</ON_SAVE>

<ON_SAVE>        

          <WITH>          

                    <NOT>

                              <PROPERTY source="lt_title" name="EMPTY"/>

                    </NOT>           

          </WITH>        

          <DO>            

                    <VALIDATION>                   

                              <PROPERTY source="lt_title" name="VALID"/>                   

                              <MESSAGE lang="*" text="Link title cannot be empty."/>                            

                    </VALIDATION>        

          </DO>    

</ON_SAVE>

<ON_SAVE>        

          <WITH> 

                    <OR>

                              <NOT>

                                        <PROPERTY source="lt_link_type" name="VALUE"/>

                              </NOT>

                              <NOT>

                                        <PROPERTY source="lt_reference" name="EMPTY"/>

                              </NOT>   

                    </OR>       

          </WITH>        

          <DO>            

                    <VALIDATION>                   

                              <PROPERTY source="lt_reference" name="VALID"/>                   

                              <MESSAGE lang="*" text="Page URL cannot be empty."/>                            

                    </VALIDATION>        

          </DO>    

</ON_SAVE>

<ON_SAVE>        

          <WITH> 

                    <OR>

                              <PROPERTY source="lt_link_type" name="VALUE"/>

                              <NOT>

                                        <PROPERTY source="lt_news_and_events_reference" name="EMPTY"/>

                              </NOT>  

                    </OR>        

          </WITH>        

          <DO>            

                    <VALIDATION>                   

                              <PROPERTY source="lt_news_and_events_reference" name="VALID"/>                   

                              <MESSAGE lang="*" text="News and Events URL cannot be empty."/>                            

                    </VALIDATION>        

          </DO>    

</ON_SAVE>

<ON_EVENT>

          <WITH>

                    <PROPERTY source="lt_link_type" name="VALUE"/>

          </WITH>         

          <DO>               

                    <PROPERTY source="lt_reference" name="VISIBLE"/>                      

          </DO>    

</ON_EVENT>

<ON_EVENT>

          <WITH>

                    <NOT>

                              <PROPERTY source="lt_link_type" name="VALUE"/>

                    </NOT>

          </WITH>         

          <DO>               

                    <PROPERTY source="lt_news_and_events_reference" name="VISIBLE"/>                      

          </DO>    

</ON_EVENT>

</RULES>

Many thanks,

Liam Davison

0 Kudos

Hi Liam,

Ok, with all my explaining about the error, I forgot to look closely at the code you posted. I might be mistaken, but it seems as if you create a form data via the lists producer, but then create a seperate link and form data for setting values, then again storing back the originally created form data.

Your code should work using formData instead of linkFD. No need to create a link object yourself.

Best regards,

Stefan

0 Kudos

Hi Stefan,

Thank you so much! I'd gotten all this way with the script and got stuck. Useful to have someone else look at my code.

Liam

0 Kudos