Make field read only in web form

2
I have a field which should be enabled (editable) when a new record is created. But should be read only when the record is edited.
asked
5 answers
3

This is such a simple question, but some complex answers are given here.

Just do the following:

  • Add a boolean microflow attribute to you object. Let the microflow return isNew($object). You don't need any activity in the microflow just add this expression directly to the end event.
  • Conditional format the edibility of the field based on this attribute. You don't need two rows, just use the editable property.

In this case, you don't need two forms (this is very important due to maintainability). You don't need two rows either.

Good luck.

Note: There are several alternatives (some of them maybe better from performance perspective) but I won't mention that to keep it simple and clear.

answered
0

You could do this with conditional formatting. So make two rows, and with the editable field and and one with readonly. Use conditional formatting to display the two different views like David said.

answered
0

Thanks Samet.

The proposed solution won't work. In my version (4.2.1) of Mendix, I don't have the option "conditional formatting". I need to have something like the an indicator to see whether the record is read from the database or is new in memory. Or I need to condition the button (new or edit) that is used.

answered
0

You need to create a boolean value that is true when a new record is created and false when a new record isn't. Then in a data view sourcing the entity create 2 rows. Right click the rows to edit their conditional value and set them according to the boolean you created.

You can make the boolean attribute be a calculated value since you probably don't want your user checking a box before creating a new record. In this case when creating the attribute choose for it to be a calculated value and in the microflow retrieve the new record and compare its size (if its greater than 0 its a new record, mind you this is assuming new records are string attributes).

Hope that helps.

answered
0

Hi Ab

I still think the simplest solution is to create 2 copies of your form - one connected to your New button, and the other connected to your Edit button. Set the attribute editable property to read-only (Never) in the Edit View. But if you really want to use a single form view...

You control editability of an attribute through a boolean or selection attribute. So you probably need to add a new boolean attribute to the entity in your domain model, then add workflow to set a suitable value to that attribute. You can then set the Editable property of the field to Conditional, select your new boolean and set editability for each possible condition.

answered