Jack P
Jack P

Reputation: 43

Xpages - Rich Text validation in CKEditor ... losing content

Platform : Domino v9

Background:

When an user opens a document using an Xpage, the Rich Text control (CKEditor)displays the existing content in that document.

The RichText field is bound to a backend Notes document.

When an user makes any changes to this content and clicks the Update button, validation occurs (for this example, we are checking to see if length exceeds 20 characters).

Validation is done using the ValidateExpression option.

On validation, user sees the error message but the new content entered by user is lost. The Rich Text control displays the original content.

Validation Code

<xp:this.validators>
<xp:validateExpression>
<xp:this.expression><![CDATA[#{javascript:var contentStr = value.getContentAsText();
var regex = /(<([^>]+)>)/ig ;
contentStr = contentStr.replace(regex,'');
if( contentStr.length < 20 ) {
  return true;
}else{
   return false;
}
}]]></xp:this.expression>
<xp:this.message><![CDATA[#{javascript:var errMsg  = getErrorMessageText   ('713','');
if ( errMsg != null && errMsg != "") {
//return errMsg;
return "Length cannot exceed 20 characters";
   }else{
return "Length cannot exceed 20 characters";
}}]]></xp:this.message>
</xp:validateExpression>
    </xp:this.validators>

Things I tried:

I tried the solution mentioned in Tommy Valand's blog but that did not solve the problem. Tommy's Blog

Question

How do I validate the RichText control without losing the new content ?

Screenshots:

enter image description here

enter image description here

enter image description here

Upvotes: 2

Views: 392

Answers (2)

Oliver Busse
Oliver Busse

Reputation: 3395

Strange behavior. Maybe you can validate your RTF control via CSJS (and not SSJS/Java) with this recently added snippet: http://openntf.org/XSnippets.nsf/snippet.xsp?id=validate-rich-text-field

Upvotes: 2

Tommy Valand
Tommy Valand

Reputation: 868

When I have complex validation, I sometimes use a hidden input field for validation. Not sure if this will help in this situation, but could be worth a try..

getLocalValue should return the converted value in the validation phase. Since you're dealing with strings, getSubmittedValue might be even more appropriate/easier to use.

If you use xp:message, set the for attribute to whatever id you decide for the hidden field.

<xp:inputHidden id="dummyValidatorField" value="dummyValue">
<xp:this.validators>
    <xp:validateExpression>
        <xp:this.expression><![CDATA[#{javascript:var contentStr = getComponent( 'idOfRtField' ).getLocalValue(); // maybe you need to append .getContentAsText();
        var regex = /(<([^>]+)>)/ig ;
        contentStr = contentStr.replace(regex,'');
        if( contentStr.length < 20 ) {
          return true;
        }else{
           return false;
        }
        }]]></xp:this.expression>
        <xp:this.message><![CDATA[#{javascript:var errMsg  = getErrorMessageText   ('713','');
        if ( errMsg != null && errMsg != "") {
        //return errMsg;
        return "Length cannot exceed 20 characters";
           }else{
        return "Length cannot exceed 20 characters";
        }}]]></xp:this.message>
    </xp:validateExpression>
</xp:this.validators>

Upvotes: 3

Related Questions