Skip to content

Commit

Permalink
fix #675 add radio and checkbox (#733)
Browse files Browse the repository at this point in the history
* #675 initial work for supporting radio and checkboxes elements

* #675 radio and checkbox are the restricted values kind

* #675 serialize restricted values also for checkbox and radio

* #675 add support for multiple values in additional fields

* 675 handle required case for checkbox -> where it's at least one value

* #675 use external validation only if the count is more than *1*

* #675 update front end
  • Loading branch information
syjer authored and cbellone committed Sep 1, 2019
1 parent 30ff84a commit d9d6d84
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ junitVersion=5.1.0
systemProp.jdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2"

# https://jitpack.io/#alfio-event/alf.io-public-frontend -> go to commit tab, set the version
alfioPublicFrontendVersion=48b776bdbc
alfioPublicFrontendVersion=b1ddbe29d9
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ private static ReservationInfo.BookingInfoTicket toBookingInfoTicket(Ticket tick
.map(tfc -> {
var tfd = descriptionsByTicketFieldId.get(tfc.getId()).get(0);//take first, temporary!
var fieldValue = valueById.get(tfc.getId());
var t = new TicketFieldConfigurationDescriptionAndValue(tfc, tfd, 1, fieldValue == null ? null : fieldValue.getValue());
var t = new TicketFieldConfigurationDescriptionAndValue(tfc, tfd, tfc.getCount(), fieldValue == null ? null : fieldValue.getValue());
var descs = fromFieldDescriptions(descriptionsByTicketFieldId.get(t.getTicketFieldConfigurationId()));
return toAdditionalField(t, descs);
}).collect(Collectors.toList());
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/alfio/manager/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import alfio.model.PromoCodeDiscount.DiscountType;
import alfio.model.Ticket.TicketStatus;
import alfio.model.TicketFieldConfiguration.Context;
import alfio.model.api.v1.admin.EventCreationRequest;
import alfio.model.modification.*;
import alfio.model.modification.EventModification.AdditionalField;
import alfio.model.result.ErrorCode;
Expand Down Expand Up @@ -239,11 +240,11 @@ private void createAdditionalFields(EventAndOrganizationId event, EventModificat
}

private static String toSerializedRestrictedValues(EventModification.WithRestrictedValues f) {
return "select".equals(f.getType()) ? generateJsonForList(f.getRestrictedValuesAsString()) : null;
return EventCreationRequest.WITH_RESTRICTED_VALUES.contains(f.getType()) ? generateJsonForList(f.getRestrictedValuesAsString()) : null;
}

private static String toSerializedDisabledValues(EventModification.WithRestrictedValues f) {
return "select".equals(f.getType()) ? generateJsonForList(f.getDisabledValuesAsString()) : null;
return EventCreationRequest.WITH_RESTRICTED_VALUES.contains(f.getType()) ? generateJsonForList(f.getDisabledValuesAsString()) : null;
}

private static String generateJsonForList(Collection<?> values) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/alfio/model/TicketFieldConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public boolean isSelectField() {
return "select".equals(type);
}

public int getCount() {
if ("checkbox".equals(type) && this.restrictedValues != null) {
return Math.max(this.restrictedValues.size(), 1);
} else {
return 1;
}
}

public boolean isEuVat() {
return "vat:eu".equals(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ enum AdditionalInfoType {
MULTI_LINE_TEXT("textarea"),
LIST_BOX("select"),
COUNTRY("country"),
EU_VAT_NR("vat:eu");
EU_VAT_NR("vat:eu"),
CHECKBOX("checkbox"),
RADIO("radio");

private final String code;

Expand All @@ -368,6 +370,8 @@ enum AdditionalInfoType {

}

public static Set<String> WITH_RESTRICTED_VALUES = Set.of(AdditionalInfoType.LIST_BOX.code, AdditionalInfoType.CHECKBOX.code, AdditionalInfoType.RADIO.code);

@Getter
@AllArgsConstructor
public static class ContentLengthRequest {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/alfio/util/EventUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static Function<Ticket, List<TicketFieldConfigurationDescriptionAndValue>
.filter(f -> f.getContext() == ATTENDEE || Optional.ofNullable(f.getAdditionalServiceId()).filter(additionalServiceIds::contains).isPresent())
.filter(f -> CollectionUtils.isEmpty(f.getCategoryIds()) || f.getCategoryIds().contains(ticket.getCategoryId()))
.map(f-> {
int count = Math.max(1, Optional.ofNullable(f.getAdditionalServiceId()).map(id -> (int) additionalServiceItems.stream().filter(i -> i.getAdditionalServiceId() == id).count()).orElse(1));
int count = Math.max(1, Optional.ofNullable(f.getAdditionalServiceId()).map(id -> (int) additionalServiceItems.stream().filter(i -> i.getAdditionalServiceId() == id).count()).orElse(f.getCount()));
return new TicketFieldConfigurationDescriptionAndValue(f, descriptions.getOrDefault(f.getId(), TicketFieldDescription.MISSING_FIELD), count, extractor.apply(f));
})
.collect(Collectors.toList());
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/alfio/util/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ public static ValidationResult validateTicketAssignment(UpdateTicketOwnerForm fo
}

List<String> values = Optional.ofNullable(form.getAdditional().get(fieldConf.getName())).orElse(Collections.emptyList());

//handle required for multiple choice (checkbox) where required is interpreted as at least one!
if (fieldConf.isRequired() && fieldConf.getCount() > 1 && values.stream().allMatch(StringUtils::isBlank)) {
errors.rejectValue(prefixForLambda + "additional["+fieldConf.getName()+"]", ErrorsCode.EMPTY_FIELD);
}

for(int i = 0; i < values.size(); i++) {
String formValue = values.get(i);
if(fieldConf.isMaxLengthDefined()) {
Expand All @@ -284,7 +290,7 @@ public static ValidationResult validateTicketAssignment(UpdateTicketOwnerForm fo
"error.restrictedValue", fieldConf.getRestrictedValues(), errors);
}

if(fieldConf.isRequired() && StringUtils.isBlank(formValue)){
if(fieldConf.isRequired() && fieldConf.getCount() == 1 && StringUtils.isBlank(formValue)){
errors.rejectValue(prefixForLambda + "additional["+fieldConf.getName()+"]["+i+"]", ErrorsCode.EMPTY_FIELD);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <h3>Configuration</h3>

<div class="row">
<div class="col-xs-12" ng-if="field.type !== 'country'">
<div data-ng-if="field.type === 'select'" class="form-horizontal">
<div data-ng-if="field.type === 'select' || field.type === 'radio' || field.type === 'checkbox'" class="form-horizontal">
<div data-ng-repeat="restrictedValue in field.restrictedValues">
<div class="form-group">
<div class="col-xs-12">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
'input:text': 'Generic Text Input',
'input:tel': 'Phone Number',
'textarea': 'Multi-line Text',
'select': 'List Box',
'select': 'Drop-down list',
'checkbox': 'Multiple choice (checkbox)',
'radio': 'One choice list (radio button)',
'country': 'Country',
'vat:eu': 'EU VAT'
};
Expand Down

0 comments on commit d9d6d84

Please sign in to comment.