check for empty textValue using getStructuredDataNode

Nando's Avatar

Nando

28 Apr, 2016 03:06 PM

We have a staff foreach where we output 6 required fields and 1 optional. All our required fields come in fine but as soon as 1 optional field is populated, the format populates all the other optional fields with that one value... I obviously only want the field to show if it has a value but it seems the system thinks it always has a value.

 #set ($creds = $asset.getStructuredDataNode("credentials").textValue)
#if($creds.value !=""), $creds#end

^^ this code shows all the all the same values as the only value set

 #if($asset.getStructuredDataNode("credentials").textValue !=""), $asset.getStructuredDataNode("credentials").textValue#end

^^ this code shows the value of the one populated field and then " , $asset.getStructuredDataNode("credentials").textValue " for the rest...

How do I check if the credentials field is populated?

Thanks in advance!

  1. 1 Posted by Nando on 28 Apr, 2016 03:19 PM

    Nando's Avatar

    Just tried:

    #if(!$_PropertyTool.isNull($creds))#if($creds.isText()), $creds.textValue#end#end
    

    this code shows the value of the one populated field and then " , $creds.textValue " for the rest...

  2. 2 Posted by Nando on 28 Apr, 2016 03:27 PM

    Nando's Avatar

    ok then... this seems to work:

    #if(!$_PropertyTool.isNull($asset.getStructuredDataNode("credentials").textValue)), $creds.trim()#end##
    
  3. 3 Posted by Ryan Griffith on 28 Apr, 2016 05:31 PM

    Ryan Griffith's Avatar

    Hi Nando,

    You got it, I am glad to see you were able to find the solution.

    When the node has no value the textValue property will return null. This is why you were seeing the literal code being output instead of the value (or nothing), because Velocity will simply "dump" the code if you try to output null (since it's technically a Null Pointer Error).

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  4. Ryan Griffith closed this discussion on 28 Apr, 2016 05:31 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM