Last active
July 20, 2021 19:02
-
-
Save MichalBryxi/c33ff9ab9a95454d651d6927e94090fc to your computer and use it in GitHub Desktop.
dynamic changeset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
export default class ApplicationController extends Controller { | |
appName = 'Ember Twiddle'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
import { action } from '@ember/object'; | |
import { tracked } from '@glimmer/tracking'; | |
import { | |
validatePresence, | |
validateLength, | |
validateConfirmation, | |
validateFormat | |
} from 'ember-changeset-validations/validators'; | |
import lookupValidator from 'ember-changeset-validations'; | |
import Changeset from 'ember-changeset'; | |
class MyValidations { | |
async validate(key, newValue, oldValue, changes, content) { | |
console.log('Spawning XHR validation'); | |
try { | |
// Some XHR stuff goes here | |
// TODO[1]: This XHR call knows how to validate _every_ field. So ideally | |
// we want to call it only _once_ per change and then grab the results | |
// for every field in the form that uses this validator | |
return 'Server says no!'; | |
} catch (_) { | |
return 'Server says double no!'; | |
} | |
} | |
} | |
export default class extends Component { | |
@tracked changeset; | |
@tracked | |
fields = { | |
name: 'my name', | |
permissions: 'admin', | |
'field[0]_value': 'foo' | |
} | |
constructor() { | |
super(...arguments); | |
const ServerValidations = new MyValidations(); | |
const AllValidations = { | |
name: [ | |
validatePresence(true), | |
validateLength({ min: 12 }), | |
ServerValidations | |
], | |
permissions: [ | |
validatePresence(true), | |
validateLength({ min: 12 }), | |
ServerValidations | |
], | |
['field[0]_value']: [ // TODO[3]: We are actually using indexes `field[0].value`, but that breaks changeset | |
validatePresence(true), | |
validateLength({ min: 12 }), | |
ServerValidations | |
] | |
}; | |
// TODO[2]: This is not very dynamic. Once we add more fields to the form | |
// via "add field" button, we won't get our changeset updated | |
this.changeset = new Changeset( | |
this.fields, | |
lookupValidator(AllValidations), | |
AllValidations | |
); | |
} | |
@action addField() { | |
const oldLength = Object.keys(this.fields).length; | |
this.fields = { | |
...this.fields, | |
[`field[${oldLength}]_value`]: `new one ${oldLength}` | |
}; | |
} | |
@action submitForm() { | |
// TODO[4]: No idea why, but validation works only once. If I click on | |
// "validate me now" for second time, the validation goes blank. | |
this.changeset.validate(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"ember-changeset-validations": "3.14.7" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment