liam_davison
I'm new here

Building complex conditional validation in <RULES>

Jump to solution

Good afternoon,

I am creating a simple image list. The image may, or may not, include a link to another page or external URL. For technical reasons, there are four "link" types available, "none", "internal", "news" and "external". The user must choose one using a CMS_INPUT_RADIOBUTTON. If a link type is chosen, we must ensure the user selects a PageRef, a Content2 row, or provides an external URL.

I cannot make this conditional rule work - if the clicks on "internal" then clicks on "external", and provides a URL for the external link, the CMS still shows an error for the "internal" link.

lt_link_type is the radiobutton

lt_internal_ref is an FS_REFERENCE

lt_news_or_event_ref is an FS_DATASET

lt_external_url is a CMS_INPUT_TEXT

The logic I want is:

IF lt_link_type == internal AND lt_internal_ref.isEmpty() THEN fail_validation

IF lt_link_type == news AND lt_news_or_event_ref.isEmpty() THEN fail_validation

IF lt_link_type == external AND lt_external_url.isEmpty() THEN fail_validation

IF lt_link_type == none THEN no_validation_required.

Here are my RULES:

<RULES>
<ON_EVENT>
<WITH>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>internal</TEXT>
</EQUAL>
</WITH>
<DO>
<PROPERTY source="lt_internal_ref" name="VISIBLE"/>
</DO>
</ON_EVENT>
<ON_EVENT>
<WITH>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>external</TEXT>
</EQUAL>
</WITH>
<DO>
<PROPERTY source="lt_external_text" name="VISIBLE"/>
<PROPERTY source="lt_external_url" name="VISIBLE"/>
</DO>
</ON_EVENT>
<ON_EVENT>
<WITH>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>news</TEXT>
</EQUAL>
</WITH>
<DO>
<PROPERTY source="lt_news_or_event_ref" name="VISIBLE"/>
</DO>
</ON_EVENT>

<ON_EVENT>
<IF>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>external</TEXT>
</EQUAL>
</IF>
<WITH>
<AND>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>external</TEXT>
</EQUAL>
<NOT>
<PROPERTY source="lt_external_url" name="EMPTY"/>
</NOT>
</AND>
</WITH>
<DO>
<VALIDATION>
<PROPERTY source="lt_external_url" name="VALID"/>
<MESSAGE lang="*" text="External URL cannot be empty" />
</VALIDATION>
</DO>
</ON_EVENT>

<ON_EVENT>
<IF>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>internal</TEXT>
</EQUAL>
</IF>
<WITH>
<AND>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>internal</TEXT>
</EQUAL>
<NOT>
<PROPERTY source="lt_internal_ref" name="EMPTY"/>
</NOT>
</AND>
</WITH>
<DO>
<VALIDATION>
<PROPERTY source="lt_internal_ref" name="VALID"/>
<MESSAGE lang="*" text="You must choose a page" />
</VALIDATION>
</DO>
</ON_EVENT>

<ON_SAVE>
<IF>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>news</TEXT>
</EQUAL>
</IF>
<WITH>
<AND>
<EQUAL>
<PROPERTY source="lt_link_type" name="VALUE"/>
<TEXT>news</TEXT>
</EQUAL>
<NOT>
<PROPERTY source="lt_news_or_event_ref" name="EMPTY"/>
</NOT>
</AND>
</WITH>
<DO>
<VALIDATION>
<PROPERTY source="lt_news_or_event_ref" name="VALID"/>
<MESSAGE lang="*" text="You must choose a news or event page" />
</VALIDATION>
</DO>
</ON_SAVE>
</RULES>

It seems to me that the CMS is ignoring the <IF> conditions on the rules.

Can I "reset" the validity of the components with an <ON_EVENT> when the user clicks on different lt_link_types?

Many thanks,

Liam Davison

0 Kudos
1 Solution

Accepted Solutions
StefanSchulz
I'm new here

Dear Liam,

The IF-part of a rule is to determine, whether the rule will be executed. Therefore, the rule will not be executed (again), if the IF evaluates false. In your case, the once set invalid state will not be reset.

I'd suggest you include the condition into the WITH part.

Another question I have for your approach: what made you not choose the CMS_INPUT_LINK component?

Cheers,

Stefan

View solution in original post

0 Kudos
2 Replies
StefanSchulz
I'm new here

Dear Liam,

The IF-part of a rule is to determine, whether the rule will be executed. Therefore, the rule will not be executed (again), if the IF evaluates false. In your case, the once set invalid state will not be reset.

I'd suggest you include the condition into the WITH part.

Another question I have for your approach: what made you not choose the CMS_INPUT_LINK component?

Cheers,

Stefan

0 Kudos

Dear Stefan,

I removed the IF clause and extended the WITH clause:

<OR>

     <AND>

          <EQUAL><PROPERTY source="lt_link_type" name="VALUE" /><TEXT>internal</TEXT></EQUAL>

          <NOT><PROPERTY source="lt_internal_ref" name="EMPTY" />

     </AND>

     <NOT>

          <EQUAL><PROPERTY source="lt_link_type" name="VALUE"/><TEXT>internal</TEXT></EQUAL>

     </NOT>

</OR>

So: lt_internal_ref.isValid = (lt_link_type == "internal" && !lt_internal_ref.isEmpty()) || lt_link_type != "internal"

I don't want to use a CMS_INPUT_LINK because the use case is: The user adds a picture (which may have a link) rather than: The user adds a link (which may have a picture). Most pictures will not include a link.

Additionally, using CMS_INPUT_LINK would mean I would have to create several new link templates.

Many thanks,

Liam

0 Kudos