use of org.apache.tapestry5.internal.BeanValidationContext in project tapestry-5 by apache.
the class BeanEditorTest method refresh_bean_validation_context.
@Test
public void refresh_bean_validation_context() {
ComponentResources resources = mockComponentResources();
BeanModelSource source = mockBeanModelSource();
BeanModel model = mockBeanModel();
Environment env = mockEnvironment();
RegistrationData data = new RegistrationData();
Messages messages = mockMessages();
PropertyOverrides overrides = mockPropertyOverrides();
BeanValidationContext beanValidationContext = newMock(BeanValidationContext.class);
train_getBoundType(resources, "object", RegistrationData.class);
train_createEditModel(source, RegistrationData.class, messages, model);
train_getOverrideMessages(overrides, messages);
expect(model.newInstance()).andReturn(data);
expect(env.push(eq(BeanValidationContext.class), isA(BeanValidationContext.class))).andReturn(beanValidationContext);
expect(model.getBeanType()).andReturn(RegistrationData.class);
BeanEditContext ctxt = new BeanEditContext() {
public Class<?> getBeanClass() {
return RegistrationData.class;
}
public <T extends Annotation> T getAnnotation(Class<T> type) {
return null;
}
};
expect(env.push(eq(BeanEditContext.class), contextEq())).andReturn(ctxt);
replay();
BeanEditor component = new BeanEditor();
component.inject(resources, overrides, source, env);
component.doPrepare();
verify();
}
use of org.apache.tapestry5.internal.BeanValidationContext in project tapestry-5 by apache.
the class Form method beginRender.
void beginRender(MarkupWriter writer) {
Link link = resources.createFormEventLink(EventConstants.ACTION, context);
String actionURL = secure && secureEnabled ? link.toAbsoluteURI(true) : link.toURI();
actionSink = new ComponentActionSink(logger, clientDataEncoder);
clientId = javascriptSupport.allocateClientId(resources);
// Pre-register some names, to prevent client-side collisions with function names
// attached to the JS Form object.
IdAllocator allocator = new IdAllocator();
preallocateNames(allocator);
formSupport = createRenderTimeFormSupport(clientId, actionSink, allocator);
environment.push(FormSupport.class, formSupport);
environment.push(ValidationTracker.class, tracker);
if (autofocus) {
ValidationDecorator autofocusDecorator = new AutofocusValidationDecorator(environment.peek(ValidationDecorator.class), tracker, javascriptSupport);
environment.push(ValidationDecorator.class, autofocusDecorator);
}
// Now that the environment is setup, inform the component or other
// listeners that the form
// is about to render.
resources.triggerEvent(EventConstants.PREPARE_FOR_RENDER, context, null);
resources.triggerEvent(EventConstants.PREPARE, context, null);
// Push BeanValidationContext only after the container had a chance to prepare
environment.push(BeanValidationContext.class, new BeanValidationContextImpl(validate));
// Save the form element for later, in case we want to write an encoding
// type attribute.
form = writer.element("form", "id", clientId, "method", "post", "action", actionURL, "data-update-zone", zone, DATA_ATTRIBUTE, DATA_ATTRIBUTE_VALUE);
if (clientValidation != ClientValidation.NONE) {
writer.attributes("data-validate", "submit");
}
if (async) {
javascriptSupport.require("t5/core/zone");
writer.attributes("data-async-trigger", true);
}
resources.renderInformalParameters(writer);
div = writer.element("div");
for (String parameterName : link.getParameterNames()) {
String[] values = link.getParameterValues(parameterName);
for (String value : values) {
// but the input value shouldn't be encoded.
try {
value = URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error("Enable to decode parameter value for parameter {} in form {}", parameterName, form.getName(), e);
}
writer.element("input", "type", "hidden", "name", parameterName, "value", value);
writer.end();
}
}
// div
writer.end();
environment.peek(Heartbeat.class).begin();
}
use of org.apache.tapestry5.internal.BeanValidationContext in project tapestry-5 by apache.
the class PropertyEditor method setupEnvironment.
/**
* Creates a {@link org.apache.tapestry5.services.PropertyEditContext} and pushes it onto the {@link
* org.apache.tapestry5.services.Environment} stack.
*/
void setupEnvironment(final String propertyName) {
propertyModel = model.get(propertyName);
PropertyEditContext context = new PropertyEditContext() {
public Messages getContainerMessages() {
return overrides.getOverrideMessages();
}
public String getLabel() {
return propertyModel.getLabel();
}
public String getPropertyId() {
return propertyModel.getId();
}
public Class getPropertyType() {
return propertyModel.getPropertyType();
}
public Object getPropertyValue() {
return propertyModel.getConduit().get(object);
}
public FieldTranslator getTranslator(Field field) {
return fieldTranslatorSource.createDefaultTranslator(field, propertyName, overrides.getOverrideMessages(), locale, propertyModel.getPropertyType(), propertyModel.getConduit());
}
public FieldValidator getValidator(Field field) {
return fieldValidatorDefaultSource.createDefaultValidator(field, propertyName, overrides.getOverrideMessages(), locale, propertyModel.getPropertyType(), propertyModel.getConduit());
}
public void setPropertyValue(Object value) {
propertyModel.getConduit().set(object, value);
}
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return propertyModel.getAnnotation(annotationClass);
}
};
environment.push(PropertyEditContext.class, context);
BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class);
if (beanValidationContext != null) {
beanValidationContext.setCurrentProperty(propertyName);
}
heartbeat.begin();
}
use of org.apache.tapestry5.internal.BeanValidationContext in project tapestry-5 by apache.
the class BeanEditor method doPrepare.
/**
* Used to initialize the model if necessary, to instantiate the object being edited if necessary, and to push the
* BeanEditContext into the environment.
*/
void doPrepare() {
if (model == null) {
Class type = resources.getBoundType("object");
model = modelSource.createEditModel(type, overrides.getOverrideMessages());
BeanModelUtils.modify(model, add, include, exclude, reorder);
}
if (object == null) {
try {
object = model.newInstance();
} catch (Exception ex) {
String message = String.format("Exception instantiating instance of %s (for component '%s'): %s", PlasticUtils.toTypeName(model.getBeanType()), resources.getCompleteId(), ex);
throw new TapestryException(message, resources.getLocation(), ex);
}
}
BeanEditContext context = new BeanEditContextImpl(model.getBeanType());
cachedObject = object;
environment.push(BeanEditContext.class, context);
// TAP5-2101: Always provide a new BeanValidationContext
environment.push(BeanValidationContext.class, new BeanValidationContextImpl(object));
}
use of org.apache.tapestry5.internal.BeanValidationContext in project tapestry-5 by apache.
the class AbstractField method removePropertyNameFromBeanValidationContext.
protected void removePropertyNameFromBeanValidationContext() {
if (beanValidationDisabled) {
return;
}
BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class);
if (beanValidationContext == null)
return;
beanValidationContext.setCurrentProperty(null);
}
Aggregations