Created
March 11, 2017 03:21
-
-
Save kemsakurai/3ae4003c33a540245f1654af2138f913 to your computer and use it in GitHub Desktop.
StatelessForm 内での使用は上手く動作しませんでした。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.extern.log4j.Log4j2; | |
import org.apache.wicket.devutils.stateless.StatelessComponent; | |
import org.apache.wicket.markup.ComponentTag; | |
import org.apache.wicket.markup.html.form.HiddenField; | |
import org.apache.wicket.model.Model; | |
import org.apache.wicket.validation.IValidatable; | |
import org.apache.wicket.validation.IValidator; | |
import org.apache.wicket.validation.ValidationError; | |
import java.util.UUID; | |
/** | |
* SyncTokenField | |
*/ | |
@Log4j2 | |
@StatelessComponent | |
public class SyncTokenField extends HiddenField { | |
private static final long serialVersionUID = 5172245669993637549L; | |
/** | |
* Construct. | |
* | |
* @param id | |
*/ | |
public SyncTokenField(String id) { | |
super(id); | |
} | |
@Override | |
protected void onInitialize() { | |
super.onInitialize(); | |
setType(String.class); | |
setRequired(true); | |
regenerateToken(); | |
add(new IValidator<String>() { | |
@Override | |
public void validate(IValidatable<String> validatable) { | |
String token = (String) getModel().getObject(); | |
log.info("validatable.getValue() = {} || token = {}", validatable.getValue(), token); | |
if (token == null || !token.equals(validatable.getValue())) { | |
ValidationError validation = new ValidationError(getString("messageMailDoubleSendError")); | |
error(validation); | |
} | |
} | |
}); | |
} | |
protected final void onComponentTag(final ComponentTag tag) { | |
super.onComponentTag(tag); | |
String token = (String) getModel().getObject(); | |
tag.put("value", token); | |
} | |
public String regenerateToken() { | |
String token = UUID.randomUUID().toString(); | |
setModel(Model.of(token)); | |
return (String) getModel().getObject(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
regenerateToken
メソッドはvalidation処理後のsubmit処理時、に実装する想定です。