Converting Users Input to Title Case

0
I want to make one of my input fields on a form either convert the sentence into title case, where the first letter of each word is capitalized, or send an error to the user when it is not in title case before they can save the form. I’ve been given some tips and have looked through the forums and have started to create a microflow that is triggered by an on change event in the form when entering the Title. I started with a create object followed by a change object activity. I am still struggling to get this to work, so I have attached screenshots of my microflow. The entity is ‘Session’ and the attribute I want capitalized is “Session_Title”. Any guidance would be appreciated. Thanks in advance!  
asked
4 answers
2

Hi Molly,

You can use a regular expression as a field validation rule as one option for this as below to let Mendix check this for you.

This only allows the field to be valid if it fits the Abcdef type format with the first character upper case and all other characters lower case.

The alternative is to let the user enter what they like and use a Before Commit event to call a Microflow to correct the text if necessary.  You can use the string functions to covert the first character to uppercase and the rest to lowercase, substring, toUpperCase, toLowerCase as one option

 

https://docs.mendix.com/refguide/string-function-calls

 

See example below

 

Is the input text just a single word or more complex?

 

Regex based validation rule

 

Before commit Microflow

 

 

 

answered
1

HI,

I would prefer you to check this https://forum.mendix.com/link/questions/238 post. Hope this gives you an idea for your requirement.

answered
1

If you are comfortable with Java, then a Java action may be the easiest approach. This the action I use when I need to title case an input.
 

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package robertprice.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

/**
 * Attempt to covert a string to Title Case.
 * 
 * This means the String is iterated over and the first character after a non word letter is capitalised. This should work with diacritics and other languages.
 */
public class ToTitleCase extends CustomJavaAction<java.lang.String>
{
    private java.lang.String Input;

    public ToTitleCase(IContext context, java.lang.String Input)
    {
        super(context);
        this.Input = Input;
    }

    @java.lang.Override
    public java.lang.String executeAction() throws Exception
    {
        // BEGIN USER CODE
        return toTitleCase(this.Input);
        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @java.lang.Override
    public java.lang.String toString()
    {
        return "ToTitleCase";
    }

    // BEGIN EXTRA CODE
    private static String toTitleCase(String str) {
        boolean uppercaseNextCharacter = true;
        
        if(str == null || str.isEmpty())
            return "";
        
        if(str.length() == 1)
            return str.toUpperCase();
        
        //split the string into characters
        String[] parts = str.split("");
        
        StringBuilder sb = new StringBuilder( str.length() );
        
        for(String part : parts){
            if (uppercaseNextCharacter)
                sb.append(part.toUpperCase());
            else
                sb.append(part.toLowerCase());
            
            if (Character.isLetter(part.charAt(0)))
                uppercaseNextCharacter = false;
            else
                uppercaseNextCharacter = true;
        }
        
        return sb.toString();
    }
    // END EXTRA CODE
}

 

answered
0

Yesterday, you asked the same question here: https://forum.mendix.com/link/questions/109131

answered