misc: do not turn condition widget red when typing (#23617) #1124

Merged
fpeters merged 1 commits from wip/23617-do-not-update-condition-error-when-typing into main 2024-02-12 10:26:49 +01:00
3 changed files with 25 additions and 29 deletions

View File

@ -127,28 +127,22 @@ def test_tracking_code(pub, auth, admin_user):
def test_validate_condition(pub):
resp = get_app(pub).get('/api/validate-condition?type=python&value_python=hello')
assert resp.json == {'klass': None, 'msg': ''}
assert resp.json == {'msg': ''}
resp = get_app(pub).get('/api/validate-condition?type=python&value_python=~2')
assert resp.json == {'klass': None, 'msg': ''}
assert resp.json == {'msg': ''}
resp = get_app(pub).get('/api/validate-condition?type=python&value_python=hello -')
assert resp.json['klass'] == 'error'
assert resp.json['msg'].startswith('syntax error')
resp = get_app(pub).get('/api/validate-condition?type=python&value_python={{form_number}}==3')
assert resp.json['klass'] == 'error'
assert 'Python condition cannot contain {{' in resp.json['msg']
resp = get_app(pub).get('/api/validate-condition?type=django&value_django=un+%C3%A9l%C3%A9phant')
assert resp.json['klass'] == 'error'
assert resp.json['msg'].startswith("syntax error: Unused 'éléphant'")
resp = get_app(pub).get('/api/validate-condition?type=django&value_django=~2')
assert resp.json['klass'] == 'error'
assert resp.json['msg'].startswith('syntax error')
resp = get_app(pub).get('/api/validate-condition?type=django&value_django=%22...%22+inf') # "..." + inf
assert resp.json['klass'] == 'error'
assert resp.json['msg'].startswith('syntax error')
resp = get_app(pub).get('/api/validate-condition?type=unknown&value_unknown=2')
assert resp.json['klass'] == 'error'
assert resp.json['msg'] == 'unknown condition type'

View File

@ -1466,11 +1466,10 @@ def validate_condition(request, *args, **kwargs):
condition = {}
condition['type'] = request.GET.get('type') or ''
condition['value'] = request.GET.get('value_' + condition['type']) or ''
hint = {'klass': None, 'msg': ''}
hint = {'msg': ''}
try:
Condition(condition).validate()
except ValidationError as e:
hint['klass'] = 'error'
hint['msg'] = str(e)
return JsonResponse(hint)

View File

@ -127,30 +127,33 @@ $(function() {
});
});
// "live" update on condition widget
$('div[data-validation-url]').each(function(idx, elem) {
var $widget = $(this);
var widget_name = $widget.find('input').attr('name');
var prefix = widget_name.substr(0, widget_name.lastIndexOf('$')) + '$';
$(this).find('input, select').on('change focus input', function() {
clearTimeout($widget.validation_timeout_id);
$widget.validation_timeout_id = setTimeout(function() {
var data = Object();
$widget.find('select, input').each(function(idx, elem) {
data[$(elem).attr('name').replace(prefix, '')] = $(elem).val();
});
$.ajax({
url: $widget.data('validation-url'),
data: data,
dataType: 'json',
success: function(data) {
$widget.removeClass('hint-warning');
$widget.removeClass('hint-error');
if (data.klass) {
$widget.addClass('hint-' + data.klass);
}
$widget.prop('title', data.msg);
$(this).find('input, select').on('blur', function() {
var data = Object();
$widget.find('select, input').each(function(idx, elem) {
data[$(elem).attr('name').replace(prefix, '')] = $(elem).val();
});
$.ajax({
url: $widget.data('validation-url'),
data: data,
dataType: 'json',
success: function(data) {
var $error = $widget.find('.error');
if ($error.length == 0) {
$error = $('<div class="error"></div>');
$error.appendTo($widget);
}
})}, 250);
if (data.msg) {
$error.text(data.msg);
} else {
$error.remove();
}
}
});
return false;
});
});