Projet

Général

Profil

0003-environment-add-js-validation-of-variable-values.patch

Benjamin Dauvergne, 19 décembre 2018 14:44

Télécharger (1,91 ko)

Voir les différences:

Subject: [PATCH 3/3] environment: add js validation of variable values

 hobo/environment/forms.py |  3 +++
 hobo/static/js/hobo.js    | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
hobo/environment/forms.py
157 157
    class Meta:
158 158
        model = Variable
159 159
        exclude = ('service_type', 'service_pk', 'auto')
160
        widgets = {
161
            'value': forms.Textarea(attrs={'data-json-validation': '#id_interpret_as_json'}),
162
        }
160 163

  
161 164
    def __init__(self, service=None, **kwargs):
162 165
        self.service = service
hobo/static/js/hobo.js
39 39
    if ($id_name.data('_changed')) return;
40 40
    $id_name.val(URLify($(this).val()).replace(/-/g, '_'));
41 41
  });
42
  $('body').delegate("[data-json-validation]", 'change input keyup keypress', function() {
43
      console.log(Date(), 'validation1');
44
      var boolean_id = $(this).data('json-validation');
45
      var $field = $(boolean_id);
46
      var validate = true;
47

  
48
      if ($field.length) {
49
          validate = $field.is(':checked');
50
      }
51
      console.log(Date(), 'validation2', validate);
52
      if (validate) {
53
          try {
54
              JSON.parse($(this).val())
55
              this.setCustomValidity('');
56
              console.log('ok');
57
          } catch (e) {
58
              this.setCustomValidity('invalid JSON');
59
              console.log('not ok');
60
          }
61
      } else {
62
          console.log('dont care');
63
      }
64
  })
65

  
42 66
});
43
-