2 Replies Latest reply on Apr 24, 2015 4:58 AM by mani_mylavarapu

    Help needed in writing a simple UI validator

    mani_mylavarapu

      I have a method  configureInputs which is called by initializeUI method (when i create a command extending AbstractProjectCommand)

       

      private void configureInputs(UIBuilder builder) {

       

            targetDirectory.addValidator(new UIValidator() {

                  @Override

                  public void validate(UIValidationContext context) {

       

       

                  }

              });

       

      here targetDirectory is a UI input field

       

        @Inject

          @WithAttributes(label = "Target Directory", type = InputType.DIRECTORY_PICKER, shortName = 'p', description = "Location of the page")

          private UIInput<String> targetDirectory;


      now what iam trying to do is i want to validate the directory path when the user browses for a particular file location.For example when user selects the targetDirectory path to create a file, he should be only selecting the path from current project.

      currentProject/abc/file

       

      not

      C:\xyz\currentProject\file

       

      I am not sure how to use builder object to do this.

       

       

      Pls help!!

      Manasa.

        • 1. Re: Help needed in writing a simple UI validator
          gastaldi

          Hi mani,

           

          One solution would be to use the Resource.resolveChildren, like in this example:

           

          public void initializeUI(UIBuilder builder) throws Exception {
               Project project = getSelectedProject(builder);
               final Resource<?> projectRoot = project.getRoot();
               targetDirectory.addValidator(new UIValidator()
               {
                    @Override
                     public void validate(UIValidationContext context)
                     {
                        try
                        {
                           projectRoot.resolveChildren(targetDirectory.getValue());
                        }
                        catch (RuntimeException re)
                        {
                           context.addValidationError(targetDirectory, "Target Directory is not a child of the project root "
                                    + projectRoot);
                        }
                     }
               });
          }
          
          
          
          • 2. Re: Help needed in writing a simple UI validator
            mani_mylavarapu

            Thank you for your suggestion.

            Is it possible to validate this at compile time.For example the moment user clicks on browse for directory or something he can actually pick the directory from current project rather than getting error at run time once the user executes the command.