|
26 | 26 | import reactor.core.publisher.Sinks;
|
27 | 27 |
|
28 | 28 | import org.springframework.beans.BeanUtils;
|
| 29 | +import org.springframework.context.i18n.LocaleContext; |
| 30 | +import org.springframework.context.i18n.LocaleContextHolder; |
29 | 31 | import org.springframework.core.MethodParameter;
|
30 | 32 | import org.springframework.core.ReactiveAdapter;
|
31 | 33 | import org.springframework.core.ReactiveAdapterRegistry;
|
@@ -121,7 +123,7 @@ public Mono<Object> resolveArgument(
|
121 | 123 | return (bindingDisabled(parameter) ? Mono.empty() : bindRequestParameters(binder, exchange))
|
122 | 124 | .doOnError(bindingResultSink::tryEmitError)
|
123 | 125 | .doOnSuccess(aVoid -> {
|
124 |
| - validateIfApplicable(binder, parameter); |
| 126 | + validateIfApplicable(binder, parameter, exchange); |
125 | 127 | BindingResult bindingResult = binder.getBindingResult();
|
126 | 128 | model.put(BindingResult.MODEL_KEY_PREFIX + name, bindingResult);
|
127 | 129 | model.put(name, value);
|
@@ -278,11 +280,23 @@ private boolean hasErrorsArgument(MethodParameter parameter) {
|
278 | 280 | return (paramTypes.length > i + 1 && Errors.class.isAssignableFrom(paramTypes[i + 1]));
|
279 | 281 | }
|
280 | 282 |
|
281 |
| - private void validateIfApplicable(WebExchangeDataBinder binder, MethodParameter parameter) { |
282 |
| - for (Annotation ann : parameter.getParameterAnnotations()) { |
283 |
| - Object[] validationHints = ValidationAnnotationUtils.determineValidationHints(ann); |
284 |
| - if (validationHints != null) { |
285 |
| - binder.validate(validationHints); |
| 283 | + private void validateIfApplicable(WebExchangeDataBinder binder, MethodParameter parameter, ServerWebExchange exchange) { |
| 284 | + LocaleContext localeContext = null; |
| 285 | + try { |
| 286 | + for (Annotation ann : parameter.getParameterAnnotations()) { |
| 287 | + Object[] validationHints = ValidationAnnotationUtils.determineValidationHints(ann); |
| 288 | + if (validationHints != null) { |
| 289 | + if (localeContext == null) { |
| 290 | + localeContext = exchange.getLocaleContext(); |
| 291 | + LocaleContextHolder.setLocaleContext(localeContext); |
| 292 | + } |
| 293 | + binder.validate(validationHints); |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + finally { |
| 298 | + if (localeContext != null) { |
| 299 | + LocaleContextHolder.resetLocaleContext(); |
286 | 300 | }
|
287 | 301 | }
|
288 | 302 | }
|
|
0 commit comments