Capitalize first letter of a field

Hi there,

Is there a way to auto capitilize the first letter of a field. I know we can use upper of lower case. But for example if a field contains firstname, if the user enters lloyd, is there a way to auto convert this to Lloyd?

I’ve been searching for a while now but didn’t find anything.

any good ideas are welcom.

Lloyd

Hello @LloydS

I suggest you to solve this task with CSS. Extend a theme that is used in your project and add the following CSS rule:

.capitalized {
  text-transform: capitalize;
}

Then you’ll be able to add this stylename to required fields:

<textFIeld stylename="capitalized"/>

jsFiddle Demo

Regards

I suppose you want the name to be capitalized in the database. Then use the following approach with ValueChangeEvent:

public class PersonEdit extends StandardEditor<Person> {

    @Inject
    private InstanceContainer<Person> personDc;

    @Subscribe("nameField")
    public void onNameFieldValueChange(HasValue.ValueChangeEvent<String> event) {
        if (event.isUserOriginated()) { // execute only if the change is done by user in UI component
            String value = event.getValue();
            if (value != null) {
                // set capitalized value to the entity attribute
                personDc.getItem().setName(StringUtils.capitalize(value));
            }
        }
    }
}

Thanks Konstantin, but i still use CUBA 6.10. Tried to apply this solution for multiple fields but i encounter errors.

Regards,

Lloyd