MDL-46262 availability: validation of fields without value
[moodle.git] / availability / condition / profile / yui / src / form / js / form.js
blobaa01d6a64a275cb070413806fb76b17f8ca1a5e3
1 /**
2  * JavaScript for form editing profile conditions.
3  *
4  * @module moodle-availability_profile-form
5  */
6 M.availability_profile = M.availability_profile || {};
8 /**
9  * @class M.availability_profile.form
10  * @extends M.core_availability.plugin
11  */
12 M.availability_profile.form = Y.Object(M.core_availability.plugin);
14 /**
15  * Groupings available for selection (alphabetical order).
16  *
17  * @property profiles
18  * @type Array
19  */
20 M.availability_profile.form.profiles = null;
22 /**
23  * Initialises this plugin.
24  *
25  * @method initInner
26  * @param {Array} standardFields Array of objects with .field, .display
27  * @param {Array} customFields Array of objects with .field, .display
28  */
29 M.availability_profile.form.initInner = function(standardFields, customFields) {
30     this.standardFields = standardFields;
31     this.customFields = customFields;
34 M.availability_profile.form.getNode = function(json) {
35     // Create HTML structure.
36     var strings = M.str.availability_profile;
37     var html = '<span class="availability-group"><label>' + strings.conditiontitle + ' ' +
38             '<select name="field">' +
39             '<option value="choose">' + M.str.moodle.choosedots + '</option>';
40     var fieldInfo;
41     for (var i = 0; i < this.standardFields.length; i++) {
42         fieldInfo = this.standardFields[i];
43         // String has already been escaped using format_string.
44         html += '<option value="sf_' + fieldInfo.field + '">' + fieldInfo.display + '</option>';
45     }
46     for (i = 0; i < this.customFields.length; i++) {
47         fieldInfo = this.customFields[i];
48         // String has already been escaped using format_string.
49         html += '<option value="cf_' + fieldInfo.field + '">' + fieldInfo.display + '</option>';
50     }
51     html += '</select></label> <label><span class="accesshide">' + strings.label_operator +
52             ' </span><select name="op" title="' + strings.label_operator + '">';
53     var operators = ['isequalto', 'contains', 'doesnotcontain', 'startswith', 'endswith',
54             'isempty', 'isnotempty'];
55     for (i = 0; i < operators.length; i++) {
56         html += '<option value="' + operators[i] + '">' +
57                 strings['op_' + operators[i]] + '</option>';
58     }
59     html += '</select></label> <label><span class="accesshide">' + strings.label_value +
60             '</span><input name="value" type="text" style="width: 10em" title="' +
61             strings.label_value + '"/></label></span>';
62     var node = Y.Node.create('<span>' + html + '</span>');
64     // Set initial values if specified.
65     if (json.sf !== undefined &&
66             node.one('select[name=field] > option[value=sf_' + json.sf + ']')) {
67         node.one('select[name=field]').set('value', 'sf_' + json.sf);
68     } else if (json.cf !== undefined &&
69             node.one('select[name=field] > option[value=cf_' + json.cf + ']')) {
70         node.one('select[name=field]').set('value', 'cf_' + json.cf);
71     }
72     if (json.op !== undefined &&
73             node.one('select[name=op] > option[value=' + json.op + ']')) {
74         node.one('select[name=op]').set('value', json.op);
75         if (json.op === 'isempty' || json.op === 'isnotempty') {
76             node.one('input[name=value]').set('disabled', true);
77         }
78     }
79     if (json.v !== undefined) {
80         node.one('input').set('value', json.v);
81     }
83     // Add event handlers (first time only).
84     if (!M.availability_profile.form.addedEvents) {
85         M.availability_profile.form.addedEvents = true;
86         var updateForm = function(input) {
87             var ancestorNode = input.ancestor('span.availability_profile');
88             var op = ancestorNode.one('select[name=op]');
89             var novalue = (op.get('value') === 'isempty' || op.get('value') === 'isnotempty');
90             ancestorNode.one('input[name=value]').set('disabled', novalue);
91             M.core_availability.form.update();
92         };
93         var root = Y.one('#fitem_id_availabilityconditionsjson');
94         root.delegate('change', function() {
95              updateForm(this);
96         }, '.availability_profile select');
97         root.delegate('change', function() {
98              updateForm(this);
99         }, '.availability_profile input[name=value]');
100     }
102     return node;
105 M.availability_profile.form.fillValue = function(value, node) {
106     // Set field.
107     var field = node.one('select[name=field]').get('value');
108     if (field.substr(0, 3) === 'sf_') {
109         value.sf = field.substr(3);
110     } else if (field.substr(0, 3) === 'cf_') {
111         value.cf = field.substr(3);
112     }
114     // Operator and value
115     value.op = node.one('select[name=op]').get('value');
116     var valueNode = node.one('input[name=value]');
117     if (!valueNode.get('disabled')) {
118         value.v = valueNode.get('value');
119     }
122 M.availability_profile.form.fillErrors = function(errors, node) {
123     var value = {};
124     this.fillValue(value, node);
126     // Check profile item id.
127     if (value.sf === undefined && value.cf === undefined) {
128         errors.push('availability_profile:error_selectfield');
129     }
130     if (value.v !== undefined && /^\s*$/.test(value.v)) {
131         errors.push('availability_profile:error_setvalue');
132     }