Skip to content
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

Missing [disable] in buttons (from chapter 8) #1

Closed
Visar78 opened this issue Jun 24, 2022 · 2 comments
Closed

Missing [disable] in buttons (from chapter 8) #1

Visar78 opened this issue Jun 24, 2022 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@Visar78
Copy link

Visar78 commented Jun 24, 2022

In files city-edit.component.html and country-edit.component.html there is no [disable]="!form.valid" in button element (type="submit"). So all validators are active, they show errors but you can submit bad values into database. (fe. country without name, without iso2 and iso3).

@Darkseal Darkseal self-assigned this Jun 24, 2022
@Darkseal Darkseal added the bug Something isn't working label Jun 24, 2022
@Darkseal
Copy link
Collaborator

Darkseal commented Jun 24, 2022

Nice catch. I've used the [disable] attribute approach in the previous editions of the book, but it seems like I've forgot to add that behavior in this edition.

I think this should be fixed in Chapter 7, when we first introduce the client-side validators, replacing the following content (PDF page 324):


Not bad, right? The input errors couldn't be more visible, and the Create button will stay disabled until they are all fixed, thus preventing accidental submits. All of these colored warnings should help our users understand what they're doing wrong and fix these issues.


With the following content:


Not bad, right? The input errors couldn't be more visible: all of these colored warnings should help our users understand what they're doing wrong and fix these issues.

However, notice how the form’s Create button is still enabled - even when those validators are showing an error: this is not a good behavior, since it allows the user to submit the form with invalid data. To prevent that, we need to disable the Create button until the form is valid, which can be done by adding a [disable] attribute to the form’s submit button in the following way:

<button mat-flat-button color="primary"
    type="submit"
    [disable]="!form.valid">

This will ensure that the Create button will stay disabled until all the validators give a positive (valid) response, thus preventing accidental submits.


This, together with the code fix (which I've just pushed in the repo) in all the edit components from Chapter 7 onwards, should fix the issue, at least from the client-side. The server side issue is another story (we should also add a [Required] ASP.NET attribute to the model classes to shield the Web API from client-side hacks) - I'll think about adding something about that as well.

Darkseal added a commit that referenced this issue Jun 24, 2022
@Darkseal
Copy link
Collaborator

EDIT: replaced [disable] with [disabled] (see #2 - thanks @Mike-Guidry for the nice catch!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants