class AssessorRegistrationController {

    @PostMapping("/{inviteHash}/register")
    public String submitYourDetails(Model model,
                                    @PathVariable("inviteHash") String inviteHash,
                                    @Validated({Default.class, PhoneNumberValidationGroup.class}) @ModelAttribute(FORM_ATTR_NAME) RegistrationForm registrationForm,
                                    BindingResult bindingResult,
                                    ValidationHandler validationHandler) {
        Supplier<String> failureView = () -> doViewYourDetails(model, inviteHash);

        return validationHandler.failNowOrSucceedWith(failureView, () -> {

            ServiceResult<Void> result = assessorService.createAssessorByInviteHash(inviteHash, registrationForm,
                    registrationForm.getAddressForm().getSelectedAddress(this::searchPostcode));

            result.getErrors().forEach(error -> {
                if (StringUtils.hasText(error.getFieldName())) {
                    bindingResult.rejectValue(error.getFieldName(), "registration." + error.getErrorKey());
                } else {
                    bindingResult.reject("registration." + error.getErrorKey());
                }
            });

            return validationHandler.
                    failNowOrSucceedWith(failureView, () -> format("redirect:/registration/%s/register/account-created", inviteHash));
        });
    }

}