Validation of custom fields in custom windows

I have the following

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://editorCaption"
        class="com.dtc.callbook.web.address.AddressEdit"
        datasource="addressDs"
        focusComponent="userInput"
        messagesPack="com.dtc.callbook.web.address">
    <dsContext>
        <datasource id="addressDs"
                    class="com.dtc.callbook.entity.Address"
                    view="_local"/>
    </dsContext>
    <dialogMode height="600"
                width="800"/>
    <layout spacing="true">
        <vbox margin="false,false,false,true"
              spacing="true">
            <hbox expand="userInput"
                  width="100%">
                ...
            <hbox expand="addressField"
                  width="100%">
                <label value="msg://chosenAddress"/>
                <textField id="addressField"
                           datasource="addressDs"
                           editable="false"
                           property="name"
                           required="true"/>
            </hbox>
           ...
        </vbox>
         <hbox id="windowActions"
              height="100%"
              spacing="true"
              width="100%">
            <button id="windowCommit"
            action="windowCommit"
            invoke="onSaveBtnClick"/>
            <button id="windowClose"
                    action="windowClose"/>
        </hbox>
    </layout>
</window>

But there was no validation by default for addressField Why?
So you see I added my function but onSaveBtnClick isn’t called. Why?
so if I change it to

...
 <button id="windowCommit"
            invoke="onSaveBtnClick"/>

then onSaveBtnClick gets called but validation doesn’t happen…

package com.dtc.callbook.web.address;

import com.haulmont.cuba.gui.components.AbstractEditor;
//...

import static java.util.Collections.singletonList;

public class AddressEdit extends AbstractEditor<Address> {
	@Inject
	private TextField addressField;
	//...

	public void onFindBtnClick() {
		//...
	}

	@Override
	public void init(Map<String, Object> params) {
		super.init(params);
		//...
		addressField.addValidator(value -> {
     //debugger doesn't get into here! WHY?!
			if (value.equals("")) {
				throw new ValidationException("You cannot select admin user");
			}
		});
	}

	public void onSaveBtnClick() {
		validate(singletonList(addressField));
		
	}
}

As far as I can see it doesn’t work because in com.haulmont.cuba.web.gui.components.WebAbstractField
isEditableWithParent() returns false
I will try to replace custom fields with fieldGroup but why doesn’t it work currently?

The platform though puts red border on non-filled required field and then throws an exception with postgres error. How do I get this field from Java. As a workaround I can catch exception and show notification “you haven’t filled some of required fields” But that’s not the solution especially when a field isn’t connected to anything in database…

I realised that when I get exception from database I can catch it in my button handler It actually was in GitHub - cuba-platform/sample-user-registration: Example of users self-registration . In this case if I set the requiredMessage for the field everything works. It was in totally custom “ok button” but where in my code do I catch exceptions that happen in

<button id="windowCommit"
        action="windowCommit"/>

of generic edit screen?

And anyway Do I have to provide message for each separate field in such a way? It’s basically "you have to fill" + field.getName() + "field"

I found a way to cause Error message displayed to user

public void onSaveBtnClick() {
		try {
			validate();
			commit();
			close(COMMIT_ACTION_ID);
		} catch (Exception e) {
			ValidationErrors ve = new ValidationErrors();
			ve.add(addressField, "Вы должны заполнить!");
			showValidationErrors(ve);
		}
	}

and use it in

<button id="windowCommit"
                    invoke="onSaveBtnClick"/>

instead of

<button id="windowCommit"
                    action="windowCommit"/>

But it’s not a solution Rather a bad workaround. How to make field red I.e change it’s “error state” … And how to perform Generic error check/display for fields with required=true and requiredMessage set to so,ething ?

I believe you can override validate() method of edit-screen controller.

It turned out that when field.editable=false the field doesn’t get red despite having required=true and being empty…