JavaFX Form Validation

Greg Brown greg.x.brown at oracle.com
Mon Jun 11 04:28:59 PDT 2012


In order to place nicely with FXML, the syntax should be more along the lines of:

TextField textField = new TextField();
textField.getValidators().add(new MandatoryValidator());
textField.getValidators().add(new UserNameValidator());

That way, the equivalent markup could be declared as follows:

<TextField>
  <validators>
    <MandatoryValidator/>
    <UserNameValidator/>
  </validators>
</TextField>

However, I imagine the generated builder syntax might look similar to what is described below.

G

On Jun 11, 2012, at 7:27 AM, Tom Eugelink wrote:

> +1 on the style. I was thinking in the line of this:
> 
> new TextField().validators(new MandatoryValidator(), new UserNameValidator());
> 
> Tom
> 
> 
> 
> 
> On 2012-06-11 13:08, Randahl Fink Isaksen wrote:
>> 
>> 
>> Finally, from what I have seen of the JGoodies approach, it looks very procedural, and very programming intensive. I found this example online:
>> 
>> |public ValidationResult validate() {
>>    ValidationResult result = new ValidationResult();
>>    if (ValidationUtils.isEmpty(mUser.getUserName())) {
>>      result.addError("User name is required");
>>    }
>>    if (ValidationUtils.isEmpty(mUser.getPassword())) {
>>      result.addError("Password is required");
>>    }
>>    return result;
>>  }|
>> 
>> 
>> Even though the validation logic to check for emptiness is reusable, the developer still has to write a lot of if-then logic. I would much rather want JavaFX to have an object oriented approach, allowing me to write something like
>> 
>> TextField userNameTextField = new TextField(new UserNameValidator());
>> 
>> where the UserNameValidator class does all of the JGoodies stuff above for me and can easily be reused among different parts of my application. With this approach it is obvious that people will start sharing EmailAddressValidator, ConfigurablePasswordValidator, LicensePlateValidator, etc.
> 



More information about the openjfx-dev mailing list