Text Area Max Length

Jacob V.'s Avatar

Jacob V.

22 Apr, 2015 04:11 PM

When creating a data definition, I'm wanting to place a "text area" field for the user to place a brief description of an article. To enforce the user being brief, I was wanting to place a maximum length on the text area. However, I saw that the "max length" attribute was not available for the "text area" input.

I then tried going into the XML to add the attribute manually but that didn't solve my issue. Is there something I'm missing?

  1. 1 Posted by Ryan Griffith on 22 Apr, 2015 04:35 PM

    Ryan Griffith's Avatar

    Hi Jacob,

    Unfortunately, I don't believe it's possible to limit the number of characters of a text area field via the Data Definition configuration. What you can do is modify your Format such that it outputs only the first 'x' characters from that field, then it outputs something like a 'read more' link. Let me know if you are using XSLT or Velocity and I'll try to track down a sample Format for that language.

    Thanks!

  2. 2 Posted by Jacob V. on 22 Apr, 2015 04:52 PM

    Jacob V.'s Avatar

    I'm using velocity for my formats.

    Ya I looked for format solutions to my problem but I couldn't get finding a string's length to work. Using .size() wasn't doing the trick.

    While we are talking about using a format for this issue, do you think something similar could be done with a WYSIWYG? If so, that may be preferable vs a text area. However, couldn't there be an issue of removing closing tags (like </strong>,</ul>,etc) that are in the end of the string that is removed?

    Ok, I'm rambling now. Thanks for the help!

  3. 3 Posted by Ryan Griffith on 22 Apr, 2015 05:59 PM

    Ryan Griffith's Avatar

    Hi Jacob,

    Ya I looked for format solutions to my problem but I couldn't get finding a string's length to work. Using .size() wasn't doing the trick.

    To get the length of a string, you would use the length() method. For example:

    #set ( $str = "some string" )
    ## Outputs '11'
    $str.length()
    

    While we are talking about using a format for this issue, do you think something similar could be done with a WYSIWYG? If so, that may be preferable vs a text area. However, couldn't there be an issue of removing closing tags (like ,,etc) that are in the end of the string that is removed?

    You can definitely truncate WYSIWYG content issue; however, as you indicated, you will need to deal with HTML tags and properly closing them after the truncation occurs.

    If generating unclosed tags is not of concern, you can use the Display Tool to truncate the contents of the WYSIWYG (generated using the Serializer Tool).

    If you are concerned about closing the open tags, you have a couple of options:

    • Remove all tags from the content using the Display Tool stripTags method and then truncate the resulting text
    • Implement a way to parse through the tags and close them during truncation. I actually worked on a Velocity macro for another client that would abbreviate/truncate a string that contains HTML tags and will attempt to close any that are left open during the process. Feel free to give this a try if you are interested. To use the macro, include it within your Format and use it as follows:
      #set ( $content = $_SerializerTool.serialize($wysiwygField, true) )
      #abbreviateHtmlString($content, 500, true)
      <p><a class="readmore" href="${link}">Read more...</a></p>
      

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by Jacob V. on 22 Apr, 2015 09:12 PM

    Jacob V.'s Avatar

    Ryan,

    I decided to use a WYSIWYG with the Macro you linked and it worked like a charm. I did need to change it just a bit so that the max length was based on character count rather than word count, but other than that there were no major changes.

    I just want to say that you have helped me more times than I can count with all of the posts on these forums. You da man!

  5. 5 Posted by Bradley Wagner on 22 Apr, 2015 09:36 PM

    Bradley Wagner's Avatar

    Hi Jacob,

    Ryan is out this afternoon. Thanks for the kind words. I will definitely pass the message along to both him and his team.

    Have a great afternoon.

  6. Bradley Wagner closed this discussion on 22 Apr, 2015 09:36 PM.

  7. Ryan Griffith re-opened this discussion on 23 Apr, 2015 04:14 PM

  8. 6 Posted by Ryan Griffith on 23 Apr, 2015 04:14 PM

    Ryan Griffith's Avatar

    Hi Jacob,

    Just iIn-case you did not notice and changed the macro's code, the macro does accept a third parameter which is a flag to choose to truncate by number of words or number of characters. That is, passing false as the third parameter would indicate number of characters and true would be number of words.

    So you would adjust the code I provided to the following:

    #abbreviateHtmlString($content, 500, false)
    

    Again, thank you for the kind words and let us know if you have any other questions.

    Have a great day!

  9. Ryan Griffith closed this discussion on 23 Apr, 2015 04:14 PM.

  10. Jacob V. re-opened this discussion on 23 Apr, 2015 04:23 PM

  11. 7 Posted by Jacob V. on 23 Apr, 2015 04:23 PM

    Jacob V.'s Avatar

    Whoops, I didn't realize. I suppose I just made it a bit harder on myself, but anyways thanks for the clarification.

  12. 8 Posted by Ryan Griffith on 23 Apr, 2015 06:11 PM

    Ryan Griffith's Avatar

    Not a problem at all, Jacob. If your change is better than what I had, please feel free to share and I'll update the macro in our Velocity Formats examples project on Github.

    Thanks!

  13. Tim closed this discussion on 04 May, 2015 09:09 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