Skip to content

Commit

Permalink
Merge pull request #758 from jzaruba/release_2.1
Browse files Browse the repository at this point in the history
#753 - MaterialValueBox now has a new attribute called #returnBlankAsNull
  • Loading branch information
kevzlou7979 authored Jan 4, 2018
2 parents 988dfa5 + 0fa3214 commit d9fab0f
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public class MaterialValueBox<T> extends AbstractValueWidget<T> implements HasCh
HasDirectionEstimator, HasText, AutoDirectionHandler.Target, IsEditor<ValueBoxEditor<T>>, HasIcon,
HasInputType, HasPlaceholder, HasCounter, HasReadOnly, HasActive {

/**
* @see #setReturnBlankAsNull(boolean)
*/
private boolean returnBlankAsNull;

private InputType type = InputType.TEXT;
private ValueBoxEditor<T> editor;
private Label label = new Label();
Expand Down Expand Up @@ -211,9 +216,41 @@ public void setType(InputType type) {

@Override
public T getValue() {
if(isReturnBlankAsNull() && isBlank()) {
return null;
}
return valueBoxBase.getValue();
}

/**
* @see #setReturnBlankAsNull(boolean)
*
* @return
*/
public boolean isReturnBlankAsNull() {
return this.returnBlankAsNull;
}

/**
* <p>With this attribute set to <code>true</code> a call to {@link #getValue()} returns <code>null</code> when
* the validator obtained by {@link #createBlankValidator()} recognizes the value as blank.
* </p>
* <p>
* You can use this attribute for non-mandatory input fields that have @Pattern constraint, like an e-mail or phone input.
* This way you avoid the @Pattern#regexp being matched against an empty string when your user does not enter
* a value into such input.
* </p>
*
* @param returnBlankAsNull should {@link #getValue()} return <code>null</code> instead of a blank value?
*/
public void setReturnBlankAsNull(boolean returnBlankAsNull) {
this.returnBlankAsNull = returnBlankAsNull;
}

protected boolean isBlank() {
return !createBlankValidator().isValid(valueBoxBase.getValue());
}

@Override
public void setValue(T value, boolean fireEvents) {
valueBoxBase.setValue(value, fireEvents);
Expand Down

0 comments on commit d9fab0f

Please sign in to comment.