Hidden default value in text field

How can I set a default value “0” in a text field on create mode. Here's what I've tried:

 <widget name="obsolete" type="text">
                <labels>
                  <label mode="any">Obsolete</label>
                </labels>
                <translated>false</translated>
                <fields>
                  <field>bc:obsolete</field>
                </fields>
                <widgetModes>
                  <mode value="any">hidden</mode>
                </widgetModes>
    </widget>
0 votes

1 answers

2352 views

ANSWER

Note that this configuration is equivalent to not having any contribution since the widget mode always resolves to "hidden".
07/24/2015



Static default values can be declared in the schema:

<xs:element name="obsolete" type="xs:int" default="0">

If you need to setup a dynamic value, you can create a listener like this:

@Name("MyCustomListener")
public class MyCustomListener implements EventListener {

    public void handleEvent(Event event) throws ClientException {
        DocumentEventContext docCtx = (DocumentEventContext) ctx;
        DocumentModel doc = docCtx.getSourceDocument();
        if (doc != null) {
            if ("myCustomType".equals(doc.getType())) {
                doc.setPropertyValue("bc:obsolete", "myCustomValue");
            }
            [...]
        }
    }
}
```
<extension target="org.nuxeo.ecm.core.event.EventServiceComponent" point="listener">
  <listener name="myCustomListener" async="false" postCommit="false"
    class="org.my.custom.package.myCustomListener">
    <event>emptyDocumentModelCreated</event>
  </listener>
</extension>

As an alternative, you can also try using a custom widget property with name “defaultValue”:

<widget name="obsolete" type="text">
  <labels>
    <label mode="any">Obsolete</label>
  </labels>
  <translated>false</translated>
  <fields>
    <field>bc:obsolete</field>
  </fields>
  <properties mode="any">
    <property name="defaultValue">0</property>
  </properties>
</widget>
0 votes