Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Conditionally Disabling a FormItem based on Add/Update status

    What's the best way to have a DynamicForm item conditionally enabled or disabled based on whether the record being edited is new or existing (respectively)?

    I'm using a DynamicForm to allow users to edit a dataset and I only want them to be able to edit a certain field when its a new record. When they're editing an existing record that field should be disabled.

    I was hoping there would be some way to attach a Handler to the form such that when the record is changed I can insert the necessary enable/disable logic based on whether the record isNew or not. But alas, there is not such Handler. I tried also using the item's setInitHandler() but I don't think that is fired everytime the underlying record is changed.

    Any ideas or suggestions? Thanks in advance.

    #2
    The ShowIf conditions are evaluated on any redraw, and you should be able to use form.getSaveOperationType() to detect add vs update.

    Comment


      #3
      Fabulous. Works like a charm.

      For the benefit of others I'm posting my code which follows your recommendation.

      Code:
        // this field is disabled for existing records but enabled for new records
        field.setShowIfCondition(new FormItemIfFunction() {			
      	@Override
      	public boolean execute(FormItem item, Object value, DynamicForm form) {
      		// only new Records can edit this field.  Otherwise, its disabled
      		field.setDisabled( dynamicForm.getSaveOperationType() != DSOperationType.ADD);
      
      		// always show field whether disabled or not 
      		return true;
      	}
        });
      cheers,
      Brenton

      Comment

      Working...
      X