formly-js / angular-formly

JavaScript powered forms for AngularJS
http://docs.angular-formly.com
MIT License
2.22k stars 405 forks source link

Display errors on submit #563

Closed monkeymonk closed 8 years ago

monkeymonk commented 8 years ago

Hi there,

I'm trying to display the fields that contains error (required, etc) after the user try to submit the form.

For now, the only way to display error is to focus each fields one after the other... thus is way to dumb.

Is anyone know how to trigger a validation of the form ?

<form class="form-horizontal" name="vm.form" ng-submit="vm.onSubmit()" novalidate>
    <formly-form model="vm.model" options="vm.options" fields="vm.fields">
        <div class="row">
            <div class="col-sm-4 col-sm-offset-3">
                <button class="btn btn-info" type="submit">Envoyer</button>
            </div>
        </div>
    </formly-form>
</form>
export default class FormlyController {
    constructor($timeout) {
        this.$timeout = $timeout;
        this.model = {};
        this.options = {};
    }

    onSubmit() {
        this.$timeout(() => {
            if (this.form.$valid) {
                // this is good
            } else {
                // ... how to display errors on the form ??
            }
        });
    }
}
monkeymonk commented 8 years ago

Ok. So I finally find the answer :

export default function FormConfig(formlyConfigProvider) {
    'ngInject';

    formlyConfigProvider.extras.errorExistsAndShouldBeVisibleExpression = 'fc.$touched || form.$submitted';
}

I've already tried with this but updating the expression with my namespace... the thing is that fc and form seems to be internal magic stuff.

This should be announced in the examples ! ^^'