Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#753 - MaterialValueBox now has a new attribute called #returnBlankAsNull #758

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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