Mandatory unless empty reference, empty choice in dropdown
Hello, Just starting with Mendix, a few questions came up regarding references: - Is it possible, to make a mandatory-unless-empty reference in the domain model, or must I set the mandatory flag to Yes on every page that uses the reference? - Is is possible, to set the default value of a reference to the first row in the referencing table and/or prevent the "empty" choice in dropdowns? Example: Model with two Entities: Person and Country Person has attribute "name", with validation rules Mandatory and Unique Country has sttribute "countryname", with validation rules Mandatory and Unique 1-N association between Country and Person Form to edit Person has Name and a reference selector with datasource Person-Person_country-Country-Countryname On execution, the first selectable value in the dropdown is an empty value. I have to put mandatory on the reference selector to prevent this empty value to be stored. I would prefer to store this in the model instead of the page. Is this possible?
to answer your second question. It is not possible in Mendix to make the empty/blank option in a reference selector go away. You can as Johan suggested create a microflow that substituts the default New button had set the association to the first item of the list. However the user would still be able to select the empty value, so you will have to implement a validation for that. Either use the required option on the page or use a microflow for the save button and validate the reference in that microflow befor saving.
I don't think you can do what you want in the domain model, but what you can do is create some custom logic to achieve what you want.
So in your person-country example, make a microflow that checks for the association with a country object every time you try to save a person object.
Selecting a default value for a reference can be done in a similar way. Instead of using a standard new action, you can create a microflow that creates a new person and immediately sets an association