LT-67: Creating default custom validatior for empty strings

This commit is contained in:
Bruno Alves de Faria 2016-08-31 09:51:28 -03:00
parent e5eb98cbc0
commit a057243745
4 changed files with 51 additions and 5 deletions

View File

@ -6,7 +6,8 @@
{{>inputText labelClass='form-group' key='name'}}
</div>
<div class="col-lg-6">
{{>inputSelect labelClass='form-group' key='type' hideSearch=true
{{>inputSelect labelClass='form-group' key='type'
hideSearch=true disabled=(eq this.mode.get 'edit')
options=(clone placeholder='Choose a server type...')}}
</div>
</div>

View File

@ -1,5 +1,27 @@
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
/*
Extend the available options on schema definitions:
* valuesLabels: Used in conjunction with allowedValues to define the text
label for each value (used on forms)
* textOptional: Used to allow empty strings
*/
SimpleSchema.extendOptions({
valuesLabels: Match.Optional([String])
valuesLabels: Match.Optional([String]),
textOptional: Match.Optional(Boolean)
});
// Add default required validation for empty strings which can be bypassed
// using textOptional=true definition
SimpleSchema.addValidator(function() {
if (
this.definition.optional !== true &&
this.definition.textOptional !== true &&
this.value === ''
) {
return 'required';
}
});

View File

@ -30,23 +30,43 @@ OHIF.mixins.group = new OHIF.Mixin({
component.value = value => {
const isGet = _.isUndefined(value);
const isArray = instance.data.arrayValues;
// Create the result data as array or object
const result = isArray ? [] : {};
// Get the group current value and return it
if (isGet) {
// Iterate over each registered item and extract its value
component.registeredItems.forEach(child => {
if (!isArray) {
// Check if it is an array or an object group
if (isArray) {
// Push the item value to the result array
result.push(child.value());
} else {
// Get the item key
const key = child.templateInstance.data.key;
//Check if a key is set for the item
if (key) {
// Add the item value to the result object
result[key] = child.value();
}
} else {
result.push(child.value());
}
});
// Return the resulting data as array or object
return result;
}
// Get the group current value
const groupValue = typeof value === 'object' ? value : result;
// Stop here if there is no value defined for this group
if (!groupValue) {
return;
}
// Iterate over each registered item and set its value
let i = 0;
component.registeredItems.forEach(child => {
const key = isArray ? i : child.templateInstance.data.key;
@ -54,6 +74,8 @@ OHIF.mixins.group = new OHIF.Mixin({
child.value(childValue);
i++;
});
// Trigger the change event after setting the new value
component.$element.trigger('change');
};

View File

@ -4,6 +4,7 @@
class="{{this.class}}"
name="{{this.name}}"
multiple="{{#if this.multiple}}multiple{{/if}}"
disabled="{{#if this.disabled}}disabled{{/if}}"
data-key="{{this.key}}"
{{this.tagAttributes}}
>