Skip to content

Instantly share code, notes, and snippets.

@klimesf
Last active May 14, 2016 09:54
Show Gist options
  • Save klimesf/cc831f663c78669360e5b322ad46f546 to your computer and use it in GitHub Desktop.
Save klimesf/cc831f663c78669360e5b322ad46f546 to your computer and use it in GitHub Desktop.
Nutforms Core extended by Nutforms Rules
window.NutformsRules.modelBuildCallback = (model) => {
// Fetch rules from server
let rawRules = fetchRules(model.entityName, model.context);
// Translate rules to JS
let rules = translateRulesToJS(rawRules);
// Register listeners to attributes
Object.keys(rules).forEach((attributeName) => {
model.attribute[attributeName].listen("value-changed", (attribute) => {
attribute.validationState = "changed";
let result = evaluate(rule, attribute);
attribute.validationState = result ? "valid" : "invalid";
});
});
// Register listeners to model...
};
window.NutformsRules.formSubmittedCallback = (model, values) => {
// Validate attribtues
// Validate the whole model
};
<!-- Import NUTFORMS core -->
<script src="../../dist/nutforms.js"></script>
<!-- Import NUTFORMS rules -->
<script src="../../dist/nutforms-rules.js"></script>
¨
<script type="text/javascript">
// Register NutformsRules to Nutforms core events
Nutforms.listen(NutformsActions.MODEL_BUILT, NutformsRules.modelBuildCallback);
Nutforms.listen(NutformsActions.FORM_SUBMITTED, NutformsRules.formSubmittedCallback);
// Generate the form
nutforms = Nutforms.generateForm(
document.getElementById("form"), // HTML Element
"cz.cvut.fel.nutforms.example.model.Bug", // Entity name
"cz_CS", // Locale
1, // Entity id
"cz.cvut.fel.nutforms.example.model.Bug/new", // Layout name
mappingFunction, // Mapping function
"new" // Business context
);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment