Data Definition - Optional element, Not just empty

karlkfi's Avatar

karlkfi

21 Jan, 2011 02:42 AM

I've been playing with data definitions and it seems that the Required (Yes/No) and Multiple (Yes/No) options don't really act the way I expect them too.
For example: On a Text Field if you indicate Not Required there doesn't seem to be a way for the user to indicate that they don't want that field. If they leave it empty the field still shows up in the xml passed to the page/template/format with an empty string as the value.

This is rather annoying when formatting, because you can't just check if the field exists, you have to also check if it is empty.
You can't do:
#if($field) $field.value #end

A related problem is that there doesn't seem to be a way to indicate zero or more of an element. If you indicate Multiple and Not Required it still puts an empty one in the xml, even if you set Minimum Number to zero.

I'd love to be able to make optional Groups. I'd also like to be able to make Required elements inside an optional Group.
For Example: Have zero or more Groups that must each have a Field, but if you indicate zero groups then you don't need any of the fields.

I'm using v6.7.3.
I see that 6.8 beta has a Collapsed option but I'm not sure what it does.

  1. 1 Posted by Charlie Holder on 21 Jan, 2011 01:25 PM

    Charlie Holder's Avatar

    The collapsed option for groups in 6.10 is the ability for the group to shrink/expand and hide/display it's contents. Similar to clicking the +/- icons on the folder tree structure. You would be able to shrink/expand a group and default a group to shrunk/expanded.

    As far as empty nodes appearing in the XML, that is how the system works. All fields that are listed in a Data Definition will have an XML node rendered.

    That ties in with non-required fields being listed in the XML as well. The option to make a field required or not is an administrators (or someone that has sufficient permissions to set this option) ability to say whether or not the field must be filled in for the Page/Block to be submitted. You can think of the required fields like comments on a blog; you must fill out a name and email if you want to post your comment.

    Again, all fields listed in a Data Definition will appear in the XML, so setting a multiple option of zero does nothing. That option technically already exists because you can mark the filed as not required, effectively giving them the option of entering zero of that field.

    The idea of optional groups already exists for reasons previously stated above. However, required fields within optional groups does sound like a good idea. I think that is something you could suggest on our Idea Exchange.

    Please let me know if I've answered all your concerns.

  2. 2 Posted by karlkfi on 21 Jan, 2011 08:06 PM

    karlkfi's Avatar

    "The idea of optional groups already exists for reasons previously stated above. "

    This statement really confused me. I think the problem is that I was thinking of "Required" as applying to the element in the schema. But they way you're using required/optional seems to apply exclusively to user input or value, not the element itself.

    From a programmer perspective there are three states that an element can have. It can be populated, empty, or not exist at all.
    ex:
    var = "value"
    var = ""
    var = null

    There is a distinct difference between empty and null. Not allowing null as an option removes functionality and flexibility from a language/schema.

    So lets say I wanted to have an optional list of links. Each link requires a text and a url. So I make a Group with two text Fields in it. I make the group allow multiple and both fields not required.
    Is it possible to write a Filter in velocity and/or xslt that will transform this into an unordered list if it's populated or skip it completely if it is empty?

  3. 3 Posted by Charlie Holder on 21 Jan, 2011 09:39 PM

    Charlie Holder's Avatar

    But they way you're using required/optional seems to apply exclusively to user input or value, not the element itself.

    Yes, that is correct.

    Inline Metadata is handled differently from Dynamic Metadata and Data Definition fields. They are different types of information and are handled differently (i.e. Inline Metadata isn't shown in the XML when null). A user specifically made the choice to create that field within the Dynamic Metadata or Data Definition fields, so we included it in the XML.

    It is definitely possible to write a Format in Velocity/XSLT that would transform a number of links from a Data Definition Group into an XHTML unordered list while at the same time ignoring the Group altogether if all of the fields.

    In Velocity, Dynamic Metadata fields are checked similar to this:

    #set ($dmdField = $_XPathTool.selectSingleNode($page, "dynamic-metadata[field='value']/value"))
    #if ($dmdField)
        ##do something
    #end
    

    This would be the type checking that you are accustomed to.

    Data Definition fields would be checked similar to this:

    #set ($sds = $_XPathTool.selectSingleNode($contentRoot, "/path/to/system-data-structure"))
    #set ($ddField = $sds.getChild("title"))
    #if ($ddField.value != "")
        ##do something
    #end
    

    Now, for your example of having a Group that may contain multiple groups (little G) of link title/URL pairs. In your logic, you would simply tack on the condition that those were filled out onto your XPath. The resulting list would contain only those nodes that had both fields filled out. You would then wrap your list definition in a test to check the size of the array. This would effectively skip any node (could be ALL of them if they were all left blank) by leaving it out of the list that looped through for output.

    Example Data Definition:

    <system-data-structure>
      <group identifier="links" label="Links" multiple="true" maximum-number="10">
        <text identifier="title" label="Link Title"/>
        <text identifier="url" label="Link URL"/>
      </group>
    </system-data-structure>
    

    Example Velocity Format:

    #set ($links = $_XPathTool.selectNodes($contentRoot, "/path/to/system-data-structure/links[title != '' and url != '']"))
    #if ($links.size() >= 1)
        <ul>
            #foreach ($link in $links)
                ##make the <li> node
            #end
        </ul>
    #end
    

    Please let me know if this helps!

  4. 4 Posted by karlkfi on 21 Jan, 2011 10:21 PM

    karlkfi's Avatar

    Instead of using the list result from getChildren you're using the XPathTool to make a list that excludes empty values. That works, I suppose. I can implement what I wanted now. But it just feels like a clever trick to work around the problem.

    The user populating the page data will have no indication that the title and url fields are required while the group is optional. That information now only exists in the Format.

  5. 5 Posted by Charlie Holder on 22 Jan, 2011 01:06 AM

    Charlie Holder's Avatar

    I agree that required fields inside of optional fields would be a good addition. I would suggest adding it to the Idea Exchange to gain popularity/support so that it might be added to a future release.

  6. Ross closed this discussion on 07 Mar, 2011 05:21 PM.

Comments are currently closed for this discussion. You can start a new one.

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