Announcement

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

    Custom Validation Message for CustomValidator in ListGridField

    Hi Isomorphic.

    I have a CustomValidator associated to a ListGridField which is working as expected, generating validation errors when some specific condition is not met by the values entered in the field. But now I want to display custom messages explaining the cause of the error (which for my real case, are several), but I can't seem to be able to get it to work. I read several posts here in the forums, and they all talk about the setFieldError api, which is the one I am using, but the custom error message simply won't show.

    Here is a simple test case:

    Code:
    public class TestCases implements EntryPoint {  
        
        private static final String PARAMETER_FIELD = "Parameter";
        private static final String GROUP_FIELD = "Group";
        private static final String VALUE_FIELD = "Value";
        
        private ListGrid grid = new ListGrid();
        
        public void onModuleLoad() {       
            configureListGrid(grid);
            setGridFields();
            setGridData();
            grid.show();
            enableAdminConsole();
        }
    
        private void configureListGrid(ListGrid grid) {
            grid.setWidth(400);
            grid.setHeight(300);
            grid.setCanEdit(true);
        }
        
        private void setGridFields() {
            
            CustomValidator validator = new CustomValidator() {
                @Override
                protected boolean condition(Object rawValue) {
                    Record validatedRecord = getRecord();
                    String parameterName = validatedRecord.getAttribute(PARAMETER_FIELD);
                    return isValid(parameterName, rawValue);
                }
                
                private boolean isValid(String parameterName, Object rawValue) {
                    try {
                        int value = Integer.parseInt((String) rawValue);
                        SC.say("Value: " + value);
                        if ((value < 0) || (value > 100)){
                            return false;
                        }
                    } catch (Exception e) {
                        RecordList gridRecords = grid.getDataAsRecordList();
                        int validatedRow = gridRecords.findIndex(PARAMETER_FIELD, parameterName);
                        SC.say("Row: " + validatedRow);
                        grid.setFieldError(validatedRow, VALUE_FIELD, "Wrong number format");
                        grid.refreshRow(validatedRow);
                        return false;
                    }
                    return true;
                }
            };
            
            ListGridField group = new ListGridField(GROUP_FIELD);
            group.setCanEdit(false);
            ListGridField parameter = new ListGridField(PARAMETER_FIELD);
            parameter.setCanEdit(false);
            ListGridField value = new ListGridField(VALUE_FIELD);
            value.setValidators(validator);
            grid.setFields(group, parameter, value);
        }
        
        private void setGridData() {  
            RecordList parameters = new RecordList();
            for (int i = 1; i < 5; i++) {
                   Record parameter = new Record();
                   int group = ((i % 2) != 0) ? 1 : 2;
                   parameter.setAttribute(GROUP_FIELD, "Group" + group);
                   parameter.setAttribute(PARAMETER_FIELD, "Parameter" + i);
                   parameters.add(parameter);
            }
            grid.setData(parameters);
        }
    }
    So the question is what am I missing here? Maybe something in specific place I am calling setFieldError? Any specific required call sequence I need to be aware of?

    SmartGWT 6.0-p20160622. Chrome 52.0.2743.116 m.

    #2
    You want Validator.setErrorMessage().

    setFieldError() would be attempting to set an error message for something other than validation. Doing this in the middle of validation is invalid.

    Comment


      #3
      Thanks Isomorphic. Indeed, this solved the issue. Here is the full working code in case anyone is interested (it shows different errors according to the validation that failed):

      Code:
      public class TestCases implements EntryPoint {  
          
          private static final String PARAMETER_FIELD = "Parameter";
          private static final String GROUP_FIELD = "Group";
          private static final String VALUE_FIELD = "Value";
          
          private ListGrid grid = new ListGrid();
          
          public void onModuleLoad() {       
              configureListGrid(grid);
              setGridFields();
              setGridData();
              grid.show();
              enableAdminConsole();
          }
      
          private void configureListGrid(ListGrid grid) {
              grid.setWidth(400);
              grid.setHeight(300);
              grid.setCanEdit(true);
          }
          
          private void setGridFields() {
              
              CustomValidator validator = new CustomValidator() {
                  @Override
                  protected boolean condition(Object rawValue) {
                      Record validatedRecord = getRecord();
                      String parameterName = validatedRecord.getAttribute(PARAMETER_FIELD);
                      return isValid(parameterName, rawValue);
                  }
                  
                  private boolean isValid(String parameterName, Object rawValue) {
                      try {
                          int value = Integer.parseInt((String) rawValue);
                          if ((value < 0) || (value > 100)){
                              setErrorMessage("Value outside limits");
                              return false;
                          }
                      } catch (Exception e) {
                          setErrorMessage("Wrong number format");
                          return false;
                      }
                      return true;
                  }
              };
              
              ListGridField group = new ListGridField(GROUP_FIELD);
              group.setCanEdit(false);
              ListGridField parameter = new ListGridField(PARAMETER_FIELD);
              parameter.setCanEdit(false);
              ListGridField value = new ListGridField(VALUE_FIELD);
              value.setValidators(validator);
              grid.setFields(group, parameter, value);
          }
          
          private void setGridData() {  
              RecordList parameters = new RecordList();
              for (int i = 1; i < 5; i++) {
                     Record parameter = new Record();
                     int group = ((i % 2) != 0) ? 1 : 2;
                     parameter.setAttribute(GROUP_FIELD, "Group" + group);
                     parameter.setAttribute(PARAMETER_FIELD, "Parameter" + i);
                     parameters.add(parameter);
              }
              grid.setData(parameters);
          }
      }
      Last edited by carlossierra; 14 Sep 2016, 12:11. Reason: Improve sample code

      Comment

      Working...
      X