Skip to content

Commit

Permalink
Schedule form: Limit numeric inputs to min/max (#2833)
Browse files Browse the repository at this point in the history
  • Loading branch information
maurofmferrao authored Dec 23, 2024
1 parent 51fa6cc commit 7863847
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lib/Entity/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,10 @@ public function validate()
if ($this->isPriority < 0) {
throw new InvalidArgumentException(__('Priority must be 0 or a positive number'), 'isPriority');
}
// Check max plays per hour is positive
if ($this->maxPlaysPerHour < 0) {
throw new InvalidArgumentException(__('Maximum plays per hour must be 0 or a positive number'), 'maxPlaysPerHour');
}

// Run some additional validation if we have a recurrence type set.
if (!empty($this->recurrenceType)) {
Expand Down
17 changes: 17 additions & 0 deletions ui/src/core/xibo-cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ window.XiboInitialise = function(scope, options) {
);
});

// Prevent manual numbers input outside of min/max
$(
scope + ' input[type="number"][min], ' +
scope + ' input[type="number"][max]',
).each((_idx, input) => {
const $input = $(input);
const max = $input.attr('max');
const min = $input.attr('min');

$input.on('blur', () => {
(max && $input.val() > max) &&
($input.val(max).trigger('change'));
(min && $input.val() < min) &&
($input.val(min).trigger('change'));
});
});

// Links that just need to be submitted as forms
$(scope + ' .XiboAjaxSubmit').on('click', function(ev) {
$.ajax({
Expand Down
4 changes: 2 additions & 2 deletions views/forms.twig
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@
</div>
{% endmacro %}

{% macro number(name, title, value, helpText, groupClass, validation, accessKey, maxNumber) %}
{% macro number(name, title, value, helpText, groupClass, validation, accessKey, maxNumber, minNumber) %}
<div class="form-group row {{ groupClass }}">
<label class="col-sm-2 control-label" for="{{ name }}" accesskey="{{ accessKey }}">{{ title }}</label>
<div class="col-sm-10">
<input class="form-control" name="{{ name }}" {% if maxNumber %}max="{{maxNumber}}" {% endif %}type="number" id="{{ name }}" value="{{ value }}" {{ validation }} />
<input class="form-control" name="{{ name }}" {% if maxNumber != undefined %}max="{{maxNumber}}" {% endif %}{% if minNumber != undefined %}min="{{minNumber}}" {% endif %}type="number" id="{{ name }}" value="{{ value }}" {{ validation }} />
<small class="form-text text-muted">{{ helpText }}</small>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions views/inline.twig
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
</div>
{% endmacro %}

{% macro number(name, title, value, helpText, groupClass, validation, accessKey, maxNumber) %}
{% macro number(name, title, value, helpText, groupClass, validation, accessKey, maxNumber, minNumber) %}
<div class="form-group mr-1 mb-1 {{ groupClass }}">
<label class="control-label mr-1" title="{{ helpText }}" for="{{ name }}" accesskey="{{ accessKey }}">{{ title }}</label>
<input class="form-control" name="{{ name }}" {% if maxNumber %}max="{{maxNumber}}" {% endif %}type="number" id="{{ name }}" value="{{ value }}" {{ validation }} />
<input class="form-control" name="{{ name }}" {% if maxNumber %}max="{{maxNumber}}" {% endif %}{% if minNumber %}min="{{minNumber}}" {% endif %}type="number" id="{{ name }}" value="{{ value }}" {{ validation }} />
</div>
{% endmacro %}

Expand Down
8 changes: 4 additions & 4 deletions views/schedule-form-add.twig
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@

{% set title %}{% trans "Display Order" %}{% endset %}
{% set helpText %}{% trans "Please select the order this event should appear in relation to others when there is more than one event scheduled" %}{% endset %}
{{ forms.number("displayOrder", title, "", helpText, 'displayOrder-control') }}
{{ forms.number("displayOrder", title, "", helpText, 'displayOrder-control', "", "", "", "0") }}

{% set title %}{% trans "Priority" %}{% endset %}
{% set helpText %}{% trans "Sets the event priority - events with the highest priority play in preference to lower priority events." %}{% endset %}
{{ forms.number("isPriority", title, "", helpText, 'priority-control') }}
{{ forms.number("isPriority", title, "", helpText, 'priority-control', "", "", "", "0") }}

{% set title %}{% trans "Maximum plays per hour" %}{% endset %}
{% set helpText %}{% trans "Limit the number of times this event will play per hour on each display. For unlimited plays set to 0." %}{{ forms.playerCompat("R308", "", "", "", "R306", "") }}{% endset %}
{{ forms.number("maxPlaysPerHour", title, 0, helpText, 'max-plays-control') }}
{{ forms.number("maxPlaysPerHour", title, 0, helpText, 'max-plays-control', "", "", "", "0") }}

{% set title %}{% trans "Run at CMS Time?" %}{% endset %}
{% set helpText %}{% trans "When selected, your event will run according to the timezone set on the CMS, otherwise the event will run at Display local time" %}{% endset %}
Expand Down Expand Up @@ -297,7 +297,7 @@
<label class="col-sm-2 control-label" for="recurrenceDetail">{{ title }}</label>
<div class="col-sm-10">
<div class="input-group">
<input class="form-control" name="recurrenceDetail" type="number" id="recurrenceDetail" value="{{ event.recurrenceDetail }}" />
<input class="form-control" name="recurrenceDetail" min="0" type="number" id="recurrenceDetail" value="{{ event.recurrenceDetail }}" />
<div class="input-group-append">
<span class="input-group-text input-group-addon"></span>
</div>
Expand Down
8 changes: 4 additions & 4 deletions views/schedule-form-edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@

{% set title %}{% trans "Display Order" %}{% endset %}
{% set helpText %}{% trans "Please select the order this event should appear in relation to others when there is more than one event scheduled" %}{% endset %}
{{ forms.number("displayOrder", title, event.displayOrder, helpText, 'displayOrder-control') }}
{{ forms.number("displayOrder", title, event.displayOrder, helpText, 'displayOrder-control', "", "", "", "0") }}

{% set title %}{% trans "Priority" %}{% endset %}
{% set helpText %}{% trans "Sets the event priority - events with the highest priority play in preference to lower priority events." %}{% endset %}
{{ forms.number("isPriority", title, event.isPriority, helpText, 'priority-control') }}
{{ forms.number("isPriority", title, event.isPriority, helpText, 'priority-control', "", "", "", "0") }}

{% set title %}{% trans "Maximum plays per hour" %}{% endset %}
{% set helpText %}{% trans "Limit the number of times this event will play per hour on each display. For unlimited plays set to 0." %}{{ forms.playerCompat("R308", "", "", "", "R306", "") }}{% endset %}
{{ forms.number("maxPlaysPerHour", title, event.maxPlaysPerHour, helpText, 'max-plays-control') }}
{{ forms.number("maxPlaysPerHour", title, event.maxPlaysPerHour, helpText, 'max-plays-control', "", "", "", "0") }}

{% set title %}{% trans "Run at CMS Time?" %}{% endset %}
{% set helpText %}{% trans "When selected, your event will run according to the timezone set on the CMS, otherwise the event will run at Display local time" %}{% endset %}
Expand Down Expand Up @@ -311,7 +311,7 @@
<label class="col-sm-2 control-label" for="recurrenceDetail">{{ title }}</label>
<div class="col-sm-10">
<div class="input-group">
<input class="form-control" name="recurrenceDetail" type="number" id="recurrenceDetail" value="{{ event.recurrenceDetail }}" />
<input class="form-control" name="recurrenceDetail" min="0" type="number" id="recurrenceDetail" value="{{ event.recurrenceDetail }}" />
<div class="input-group-append">
<span class="input-group-text input-group-addon"></span>
</div>
Expand Down

0 comments on commit 7863847

Please sign in to comment.