Announcement

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

    Dynamic change formitem's required

    Hello, I got a problem with the required status of formitem. You know that when a formitem is required non-empty value, its title will be bold.

    I got a checkbox, required is false, not checked.
    I add a ChangedHandler to it. When it's checked, i set its required true, and when unchecked, i set its required false.
    What i want to achieve is that when i check it, it will be bold.

    But I get the opposite result. When i check it, it's not bold. When i uncheck it, it becomes bold. I thought the checkbox's required has changed, but need refresh, so I try to redraw the form, but no effects.

    So help me plz. Thank you very much.

    #2
    Can you please show your code that isn't working?

    Comment


      #3
      Yes, here is my code.
      Code:
      CheckboxItem check_org = new CheckboxItem();
      check_org.setRequired(false);
      check_org.setShowTitle(false);
      check_org.setTitle("Organization:");
      
      check_org.addChangedHandler(new ChangedHandler() {
          public void onChanged(ChangedEvent event) {
      	check_org.setRequired(isOrgChecked());
          }
      });
      Code:
      private boolean isOrgChecked() {
          Boolean isOrgChecked = (Boolean) check_org.getValue();
          if (isOrgChecked != null && isOrgChecked)
      	return true;
          return false;
      }

      Comment


        #4
        Have you tried a check_org.redraw() after the setRequired() call in onChanged?

        Comment


          #5
          Sorry, I found that there isn't the method of check_org.redraw().

          But, thank you very much, I use form.redraw() and it works.

          Thank you again~

          Comment


            #6
            just a little offtopic: in this way you redraw all the form, souldn't the CheckboxItem have a .redraw() method to avoid refreshing the entire form?

            Comment


              #7
              You should use FormItem.setRedrawOnChange(true);

              I have added FormItem.redraw() as well for completeness.

              Sanjiv

              Comment


                #8
                Hi,

                I'm setting the required boolean by toggling it a few times on a formitem and call redraw() on the Form. However, the error that the field is required does not come up.

                1st item: no setRequired called
                2nd item setRequired(false)
                3rd item: setRequired(true)


                -Press validate (OK)
                restart test
                -Press toggle, the 2 first ones are bold, the 3rd is not (OK)
                -Press Validate, the 2 first ones have no ! icon, the 3rd one is now suddenly bold and does show the ! icon (NOK)
                -Press toggle
                -Press toggle all 3 are bold (NOK) toggle not working anymore?


                Code:
                	VLayout result = new VLayout();
                	
                	TextItem item1 = new TextItem();
                	item1.setTitle("not set");
                
                TextItem item2 = new TextItem();
                item2.setTitle("set not required");
                item2.setRequired(false);
                
                TextItem item3 = new TextItem();
                item3.setTitle("set required");
                item3.setRequired(true);
                
                final DynamicForm form = new DynamicForm();
                form.setItems(item1, item2, item3);
                
                Button toggleRequired = new Button("Toggle setRequired");
                toggleRequired.addClickHandler(new ClickHandler() {
                	
                	public void onClick(ClickEvent event) {
                		form.clearErrors(true);
                		for (FormItem item : form.getFields()) {
                			Boolean isRequired = item.getRequired();
                			if (isRequired == null) isRequired = false;
                			item.setRequired(!isRequired);
                		}
                		form.redraw(); //tried with mark for redraw as well
                	}
                });
                
                Button validateValues = new Button("Validate");
                validateValues.addClickHandler(new ClickHandler() {
                	
                	public void onClick(ClickEvent event) {
                		form.clearErrors(true);
                		if (!form.validate()) {
                			Map errors = form.getErrors();
                			if (errors != null && !errors.isEmpty()) {
                				SC.say("At least one validation error occurred:<br/>" + errors.values().iterator().next().toString());
                			} else {
                				SC.say("Validation failed without errors");
                			}
                		}
                	}
                });
                
                
                result.addMember(form);
                result.addMember(toggleRequired);
                result.addMember(validateValues);
                
                return result;

                SmartClient Version: SC_SNAPSHOT-2010-09-16/EVAL Deployment (expires 2010.11.15_09.37.00)

                Comment


                  #9
                  We'll look into why setRequired isn't being reliable offline.
                  However you should be able to use a RequiredIfValidator to get conditionally required behavior. We have an example of this in the showcase: http://www.smartclient.com/smartgwt/..._conditionally

                  Comment


                    #10
                    Update: support for modifying formItem.required at runtime has now been fixed in mainline.

                    Comment

                    Working...
                    X