1
votes

Is there any way to validate a Rich Text I add a validateExpression but is doesn't work.

Thanks

<xp:inputRichText
        value="#{document1.Request}" id="inputRichText1" style="width:99.0%">
  <xp:this.validators>
    <xp:validateExpression 
          message="Attachment is missing">
    <xp:this.expression><![CDATA[#{javascript: if(getComponent("inputRichText1").getSubmittedValue()!== ''){
           return true
           }}]]></xp:this.expression>
</xp:validateExpression>
</xp:this.validators></xp:inputRichText>
2

2 Answers

6
votes

The validatorRequired is required in addition but doesn't help on its own.

If user clicks into RichText field and doesn't input anything then it's content gets set to

<p dir="ltr">
    &nbsp;</p>

and the test for empty value doesn't work anymore.

So, we have to eliminate this with replace() before we test for "".

<xp:messages
    id="messages1" />
<xp:inputRichText
    value="#{document1.Request}"
    id="inputRichText1"
    style="width:99.0%"
    disableClientSideValidation="true">
    <xp:this.validators>
        <xp:validateExpression message="Attachment is missing">
            <xp:this.expression><![CDATA[#{javascript: 
                var text = (value + " ").replace(/(<((?!img|a\s)[^>]+)>)|&nbsp;/ig, "").trim();                             
                return text !== "";
            }]]></xp:this.expression>
        </xp:validateExpression>
        <xp:validateRequired
            message="Attachment is missing" />
    </xp:this.validators>
</xp:inputRichText>

The regular expression eliminates all html tags <...> and &nbsp; except image tags <img...> and link tags <a...>.

1
votes

For standard components, I know validators are only triggered if there is a requiredValidator as well. Basically, if you're wanting to ensure content fits a particular criteria, the assumption is that you need to also validate that the field's not blank.