Skip to content

Commit fe52b87

Browse files
author
Baylee Swenson
committed
Get validations to work, update type
1 parent 9ed3151 commit fe52b87

File tree

10 files changed

+24
-15
lines changed

10 files changed

+24
-15
lines changed

lib/handbook/addon/docs/components/form-controls/controller.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { reads } from '@ember-decorators/object/computed';
33
import { service } from '@ember-decorators/service';
44
import Controller from '@ember/controller';
55
import { action } from '@ember/object';
6-
// import Changeset from 'ember-changeset';
7-
// import lookupValidator from 'ember-changeset-validations';
86
import { ChangesetDef } from 'ember-changeset/types';
97
import DS from 'ember-data';
108
import Toast from 'ember-toastr/services/toast';
@@ -32,7 +30,10 @@ export default class FormController extends Controller {
3230

3331
@action
3432
submit() {
35-
this.changeset.save({});
36-
this.toast.success('Saved!');
33+
this.changeset.validate();
34+
if (this.changeset.isValid) {
35+
this.changeset.save({});
36+
this.toast.success('Saved!');
37+
}
3738
}
3839
}

lib/handbook/addon/docs/components/form-controls/demo-default/template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<demo.example @name='form-controls.demo-default.hbs'>
33
<form {{action this.submit on='submit'}}>
44
<FormControls
5-
@changeset={{changeset this.node this.validation}}
5+
@changeset={{this.changeset}}
66
as |form|
77
>
88
<form.text

lib/handbook/addon/docs/components/form-controls/route.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { computed } from '@ember-decorators/object';
22
import Route from '@ember/routing/route';
33
import Changeset from 'ember-changeset';
4+
import lookupValidator from 'ember-changeset-validations';
45
import { task } from 'ember-concurrency';
56
import ConfirmationMixin from 'ember-onbeforeunload/mixins/confirmation';
67

@@ -18,7 +19,11 @@ export default class FormControls extends Route.extend(ConfirmationMixin, {
1819
}
1920

2021
setupController(controller: FormController) {
21-
const changeset = new Changeset(controller.node, controller.validation);
22+
const model = {
23+
title: '',
24+
description: '',
25+
};
26+
const changeset = new Changeset(model, lookupValidator(controller.validation), controller.validation);
2227
controller.set('changeset', changeset);
2328
}
2429

lib/handbook/addon/docs/components/form-controls/template.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# validated-form-controls
1+
# form-controls
22
Takes a changeset and yields validated form components
33

44
### Params
55
The only param that `<FormControls />` takes is a changeset for the current form being created
66

77
### Yielded hash
8-
When invoked in a form, `<ValidatedFormControls />` yields a hash with the following keys:
8+
When invoked in a form, `<FormControls />` yields a hash with the following keys:
99

1010
* Several `validated-model-form/*` components with common arguments (`model`, `changeset`, `shouldShowMessages`, `disabled`) already bound:
1111
* `checkbox`
@@ -18,6 +18,5 @@ When invoked in a form, `<ValidatedFormControls />` yields a hash with the follo
1818
## Demo: Default
1919
{{docs/components/form-controls/demo-default
2020
submit=(action this.submit)
21-
node=this.node
22-
validation=this.validation
21+
changeset=this.changeset
2322
}}

lib/handbook/addon/docs/components/form-controls/validation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ export const nodeValidation: ValidationObject<NodeModel> = {
66
title: [
77
validatePresence(true),
88
],
9+
description: [
10+
validatePresence(true),
11+
],
912
};

lib/handbook/addon/docs/template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<nav.item @label='contributor-list' @route='docs.components.contributor-list' />
2424
<nav.item @label='copyable-text' @route='docs.components.copyable-text' />
2525
<nav.item @label='delete-button' @route='docs.components.delete-button' />
26+
<nav.item @label='form-controls' @route='docs.components.form-controls' />
2627
<nav.item @label='osf-link' @route='docs.components.osf-link' />
2728
<nav.item @label='institutions-widget' @route='docs.components.institutions-widget' />
2829
<nav.item @label='loading-indicator' @route='docs.components.loading-indicator' />
@@ -33,7 +34,6 @@
3334
<nav.item @label='panel' @route='docs.components.panel' />
3435
<nav.item @label='placeholder' @route='docs.components.placeholder' />
3536
<nav.item @label='tags-widget' @route='docs.components.tags-widget' />
36-
<nav.item @label='validated-form-controls' @route='docs.components.validated-form-controls' />
3737
<nav.item @label='validated-model-form' @route='docs.components.validated-model-form' />
3838
</viewer.nav>
3939

lib/handbook/addon/routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default buildRoutes(function() {
2828
this.route('contributor-list');
2929
this.route('copyable-text');
3030
this.route('delete-button');
31+
this.route('form-controls');
3132
this.route('osf-link');
3233
this.route('institutions-widget');
3334
this.route('loading-indicator');
@@ -38,7 +39,6 @@ export default buildRoutes(function() {
3839
this.route('panel');
3940
this.route('placeholder');
4041
this.route('tags-widget');
41-
this.route('validated-form-controls');
4242
this.route('validated-model-form');
4343
});
4444

lib/osf-components/addon/components/form-controls/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import template from './template';
99
export default class FormControls extends Component {
1010
// Optional arguments
1111
disabled: boolean = defaultTo(this.disabled, false);
12-
shouldShowMessages: boolean = false;
12+
shouldShowMessages: boolean = true;
1313
}

lib/osf-components/addon/components/validated-input/x-input-wrapper/component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default class ValidatedXInputWrapper extends Component {
1919
// Optional arguments
2020
errors?: string;
2121
label?: string;
22+
id?: string;
2223

2324
@className
2425
@equal('validationStatus', ValidationStatus.HasError)
@@ -34,6 +35,6 @@ export default class ValidatedXInputWrapper extends Component {
3435

3536
@computed('elementId', 'valuePath')
3637
get inputElementId() {
37-
return `${this.elementId}__${this.valuePath}`;
38+
return this.id ? this.id : `${this.elementId}__${this.valuePath}`;
3839
}
3940
}

types/ember-changeset-validations/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { validator } from 'ember-validations';
33

44
export type ValidatorFunction = (key: string, result: any) => string | string[] | true;
55

6-
export function lookupValidator(validator: validatorObject): ValidatorFunc;
6+
export default function lookupValidator(validator: validatorObject): ValidatorFunc;
77

88
// FIXME: Below doesn't actually work, but setting it aside for now to get actual
99
// functionality implemented. James is upgrading types a bit, and the following

0 commit comments

Comments
 (0)