Search in sources :

Example 91 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project mots by motech-implementations.

the class ModuleController method validateCourseForRelease.

private void validateCourseForRelease(Course course) {
    BindingResult bindingResult = new BeanPropertyBindingResult(course, COURSE);
    validator.validate(course, bindingResult, CourseReleaseCheck.class);
    checkBindingResult(bindingResult);
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult)

Example 92 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project entando-core by entando.

the class AbstractListViewerWidgetValidator method validate.

@Override
public BeanPropertyBindingResult validate(WidgetConfigurationRequest widget, IPage page) {
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(widget, widget.getClass().getSimpleName());
    try {
        logger.debug("validating widget {} for page {}", widget.getCode(), page.getCode());
        WidgetValidatorCmsHelper.validateTitle(widget, getLangManager(), bindingResult);
        WidgetValidatorCmsHelper.validateLink(widget, getLangManager(), getPageManager(), bindingResult);
    } catch (Throwable e) {
        logger.error("error in validate wiget {} in page {}", widget.getCode(), page.getCode());
        throw new RestServerError("error in widget config validation", e);
    }
    return bindingResult;
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RestServerError(org.entando.entando.aps.system.exception.RestServerError)

Example 93 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project entando-core by entando.

the class AbstractEntityService method deleteEntityType.

protected void deleteEntityType(String entityManagerCode, String entityTypeCode) {
    IEntityManager entityManager = this.extractEntityManager(entityManagerCode);
    try {
        IApsEntity entityType = entityManager.getEntityPrototype(entityTypeCode);
        if (null == entityType) {
            return;
        }
        List<String> ids = entityManager.searchId(entityTypeCode, null);
        if (null != ids && ids.size() > 0) {
            BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(entityType, "entityType");
            bindingResult.reject(EntityTypeValidator.ERRCODE_ENTITY_TYPE_REFERENCES, new Object[] { entityTypeCode }, "entityType.cannot.delete.references");
            throw new ValidationConflictException(bindingResult);
        }
        ((IEntityTypesConfigurer) entityManager).removeEntityPrototype(entityTypeCode);
    } catch (ApsSystemException e) {
        logger.error("Error in delete entityType {}", entityTypeCode, e);
        throw new RestServerError("error in delete entityType", e);
    }
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) IEntityManager(com.agiletec.aps.system.common.entity.IEntityManager) RestServerError(org.entando.entando.aps.system.exception.RestServerError) IApsEntity(com.agiletec.aps.system.common.entity.model.IApsEntity) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) IEntityTypesConfigurer(com.agiletec.aps.system.common.entity.IEntityTypesConfigurer) ValidationConflictException(org.entando.entando.web.common.exceptions.ValidationConflictException)

Example 94 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project alf.io by alfio-event.

the class EventApiV1Controller method create.

@PostMapping("/create")
@Transactional
public ResponseEntity<String> create(@RequestBody EventCreationRequest request, Principal user) {
    String imageRef = Optional.ofNullable(request.getImageUrl()).map(this::fetchImage).orElse(null);
    Organization organization = userManager.findUserOrganizations(user.getName()).get(0);
    AtomicReference<Errors> errorsContainer = new AtomicReference<>();
    Result<String> result = new Result.Builder<String>().checkPrecondition(() -> isNotBlank(request.getTitle()), ErrorCode.custom("invalid.title", "Invalid title")).checkPrecondition(() -> isBlank(request.getSlug()) || eventNameManager.isUnique(request.getSlug()), ErrorCode.custom("invalid.slug", "Invalid slug")).checkPrecondition(() -> isNotBlank(request.getWebsiteUrl()), ErrorCode.custom("invalid.websiteUrl", "Invalid Website URL")).checkPrecondition(() -> isNotBlank(request.getTermsAndConditionsUrl()), ErrorCode.custom("invalid.tc", "Invalid Terms and Conditions")).checkPrecondition(() -> isNotBlank(request.getImageUrl()), ErrorCode.custom("invalid.imageUrl", "Invalid Image URL")).checkPrecondition(() -> isNotBlank(request.getTimezone()), ErrorCode.custom("invalid.timezone", "Invalid Timezone")).checkPrecondition(() -> isNotBlank(imageRef), ErrorCode.custom("invalid.image", "Image is either missing or too big (max 200kb)")).checkPrecondition(() -> {
        EventModification eventModification = request.toEventModification(organization, eventNameManager::generateShortName, imageRef);
        errorsContainer.set(new BeanPropertyBindingResult(eventModification, "event"));
        int descriptionMaxLength = configurationManager.getFor(ConfigurationKeys.DESCRIPTION_MAXLENGTH, ConfigurationLevel.system()).getValueAsIntOrDefault(4096);
        ValidationResult validationResult = validateEvent(eventModification, errorsContainer.get(), descriptionMaxLength);
        if (!validationResult.isSuccess()) {
            log.warn("validation failed {}", validationResult.getValidationErrors());
        }
        return validationResult.isSuccess();
    }, ErrorCode.lazy(() -> toErrorCode(errorsContainer.get()))).build(() -> insertEvent(request, user, imageRef).map(Event::getShortName).orElseThrow(IllegalStateException::new));
    if (result.isSuccess()) {
        return ResponseEntity.ok(result.getData());
    } else {
        return ResponseEntity.badRequest().body(Json.toJson(result.getErrors()));
    }
}
Also used : Errors(org.springframework.validation.Errors) Organization(alfio.model.user.Organization) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) EventModification(alfio.model.modification.EventModification) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) ValidationResult(alfio.model.result.ValidationResult) ValidationResult(alfio.model.result.ValidationResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Result(alfio.model.result.Result) Transactional(org.springframework.transaction.annotation.Transactional)

Example 95 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project alf.io by alfio-event.

the class PromoCodeRequestManager method makeSimpleReservation.

private Pair<Optional<String>, BindingResult> makeSimpleReservation(Event event, int ticketCategoryId, String promoCode, ServletWebRequest request, Optional<PromoCodeDiscount> promoCodeDiscount, Principal principal) {
    Locale locale = RequestUtils.getMatchingLocale(request, event);
    ReservationForm form = new ReservationForm();
    form.setPromoCode(promoCode);
    TicketReservationModification reservation = new TicketReservationModification();
    reservation.setQuantity(1);
    reservation.setTicketCategoryId(ticketCategoryId);
    form.setReservation(Collections.singletonList(reservation));
    var bindingRes = new BeanPropertyBindingResult(form, "reservationForm");
    return Pair.of(createTicketReservation(form, bindingRes, event, locale, promoCodeDiscount.map(PromoCodeDiscount::getPromoCode), principal), bindingRes);
}
Also used : Locale(java.util.Locale) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ReservationForm(alfio.controller.form.ReservationForm) PromoCodeDiscount(alfio.model.PromoCodeDiscount) TicketReservationModification(alfio.model.modification.TicketReservationModification)

Aggregations

BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)153 Test (org.junit.jupiter.api.Test)71 RestServerError (org.entando.entando.aps.system.exception.RestServerError)28 Errors (org.springframework.validation.Errors)24 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)23 BindingResult (org.springframework.validation.BindingResult)23 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)21 StringReader (java.io.StringReader)20 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 List (java.util.List)17 Document (org.dom4j.Document)17 Element (org.dom4j.Element)17 SAXReader (org.dom4j.io.SAXReader)17 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 ResourceNotFoundException (org.entando.entando.aps.system.exception.ResourceNotFoundException)14 ArrayList (java.util.ArrayList)13 ValidationConflictException (org.entando.entando.web.common.exceptions.ValidationConflictException)13 FieldError (org.springframework.validation.FieldError)12 PropertyEditorSupport (java.beans.PropertyEditorSupport)10 Test (org.junit.Test)9