Skip to content

Disable submit button #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pimhooghiemstra opened this issue Feb 21, 2017 · 15 comments
Closed

Disable submit button #128

pimhooghiemstra opened this issue Feb 21, 2017 · 15 comments

Comments

@pimhooghiemstra
Copy link

I am a bit stuck with the submit button of a form. I would like to disable the submit button when validation fails and enable it again once all fields pass validation.

However, according to the docs we can use the disabled property in the schema for any field and using it on a submit button adds a 'disabled' attribute to the HTML, but on the outer div.form-group. I expected it on the input[type=submit]...

My idea was to use the onValidated callback for an individual field to toggle this enabled/disabled state on the submit button.

Is something like this possible?

Here is a fiddle showing the issue: https://jsfiddle.net/pimhooghiemstra/0mg1v81e/207/

@icebob
Copy link
Member

icebob commented Feb 21, 2017

The disabled property can be function too.
Please try it:

{
              type:"submit",
              buttonText:"Click me to submit",
              onSubmit:this.submitForm,
              disabled() {
		return this.errors.length == 0;
   	     }
}

or this.$parent.errors.length ?!?!

@icebob
Copy link
Member

icebob commented Feb 21, 2017

Hmm, not working because the this not binded to the instance of field.

@icebob
Copy link
Member

icebob commented Feb 21, 2017

I will fix it in the next release.

@pimhooghiemstra
Copy link
Author

Great! I have a workaround for the moment but it quickly becomes ugly i am afraid.

@icebob icebob added this to the v2.0.0 milestone Feb 21, 2017
@icebob
Copy link
Member

icebob commented Feb 21, 2017

@pimhooghiemstra the solution & fix is coming with #129

@icebob
Copy link
Member

icebob commented Feb 22, 2017

If you updated, could you try this solution?

{
              type:"submit",
              buttonText:"Click me to submit",
              onSubmit:this.submitForm,
              disabled() {
		return this.$parent.errors.length != 0;
   	     }
}

@pimhooghiemstra
Copy link
Author

I'll try this tomorrow

@pimhooghiemstra
Copy link
Author

@icebob I copied the code above for the submit button, but unfortunately this gives me an error.
screen shot 2017-02-23 at 10 40 07

If I check the Vue devtools, it doesn't seem there is a parent errors object?

screen shot 2017-02-23 at 10 43 30

Then I tried the disabled() function to just return true, thinking the button would be disabled on page load but that was not the case.

Maybe I am missing something?

@icebob
Copy link
Member

icebob commented Feb 23, 2017

Yes, there are some other bugs :) I'm doing a hotfix.

@icebob
Copy link
Member

icebob commented Feb 23, 2017

Done.

So, the first problem this is not the field instance, it is the vfg instance. So you have to use this.errors.length > 0.

Other problem, fieldSubmit hadn't disabled attribute, I fixed.
Last problem, there was no any CSS style for disabled button, fixed.

Firstly, run npm install icebob/vue-form-generator to update the last files.
Use this code in your schema:

{
	type: "submit",
	label: "",
	buttonText: "Submit form",
	onSubmit(model, schema) {
		alert("Form submitted!");
	},
	disabled() {
		return this.errors.length > 0;
	}
}

I hope it'll work for you.

@icebob
Copy link
Member

icebob commented Feb 27, 2017

@pimhooghiemstra Working?

@pimhooghiemstra
Copy link
Author

@icebob I had a quick (maybe too quick) look last week and it did not work. However, I'll try again today and will post my results later today. Excuse me for the delay.

@pimhooghiemstra
Copy link
Author

pimhooghiemstra commented Feb 28, 2017

@icebob I had a more rigorous look and now it works indeed, thanks!

What took me by surprise is the fact that the styling of the disabled submit button is not different from the not-disabled version, this makes the button clickable even when disabled.

Maybe this would be better
`
fieldset.vue-form-generator .field-wrap input[type=submit]:disabled {

cursor: not-allowed;

opacity: 0.65;

}
`

Anyway, I am happy it works now

@icebob
Copy link
Member

icebob commented Feb 28, 2017

I added the same css style to form generator when fixed the issue: https://github.com/icebob/vue-form-generator/blob/master/src/formGenerator.vue#L405

I'm glad it is working. I will make a new beta release in a short time.

@icebob
Copy link
Member

icebob commented Mar 1, 2017

New beta released

@icebob icebob closed this as completed Mar 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants