Adding validation for email field in User entity

Hi,

I have ExtUser entity with parent class User [sec$User](super class extended) everything works fine.

Now I would like to have validation for Email field which is in User [sec$User] entity(super class) and want to make it mandatory.

How do I achieve this? because I want to send email to end user with user name and password as soon as admin creates the user.

image

1 Like

Maybe this can help you?

https://www.cuba-platform.com/discuss/t/how-to-translate-validation-messages/29/4

We are extending User entity which is standard entity which we can not modify at all.
Email field is in user entity so how do we apply mandatory validation?

Above link doss not help me.

Hi,

Unfortunately, there is no easy way to extend existing entities with Bean Validation, but you can create a simple meta property and add your validators there in the extended entity.

Use the following trick:

@Extends(User.class)
@Entity(name = "demo$ExtUser")
public class ExtUser extends User {
    private static final long serialVersionUID = 1150002660073436509L;

    public void setCustomerEmail(String customerEmail) {
        this.email = customerEmail;
    }

    @Email(message = "Should be valid email")
    @Length(message = "Email should have min 3 and max 50 length", min = 3, max = 50)
    @NotNull
    @Transient
    @MetaProperty(mandatory = true, related = "email")
    public String getCustomerEmail() {
        return this.email;
    }
}

As you see, we simply provide additional getter / setter instead of separate column in the database.

Then extend user-edit screen:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        class="com.company.demo.web.screens.ExtUserEditor"
        extends="/com/haulmont/cuba/gui/app/security/user/edit/user-edit.xml"
        messagesPack="com.company.demo.web.screens"
        xmlns:ext="http://schemas.haulmont.com/cuba/window-ext.xsd">
    <layout>
        <groupBox id="propertiesBox">
            <grid id="propertiesGrid">
                <rows>
                    <row id="propertiesRow">
                        <fieldGroup id="fieldGroupLeft">
                            <column>
                                <field id="customerEmail"
                                       ext:index="7"
                                       property="customerEmail"/>
                                <field id="email"
                                       visible="false"/>
                            </column>
                        </fieldGroup>
                    </row>
                </rows>
            </grid>
        </groupBox>
    </layout>
</window>

Just hide existing email field and add the new one.

Thanks @artamonov

Will implement this change