Closed matthieusb closed 7 years ago
Thanks for the report, indeed we missed this, and that's really important! Like you, I would just put "false" in the model, that's easier.
According to the HTML spec, we cannot do the following <input type="checkbox" checked="false"/>
So, I'm defaulting boolean fields to false in the constructor.
Closing as the PR is merged
Seems like this change can cause bugs. What if I want to construct an object with a boolean value starting as true?
export class Candidate {
constructor(
public paidKind?: boolean,
public taskPaid?: boolean,
) {
this.paidKind = false;
this.taskPaid = false;
}
}
Then let candidate = new Candidate(true, true);
will create the object with false values!
Instead, I think JHipster should automatically generate the constructor as:
this.paidKind = this.paidKind || false;
this.taskPaid = this.taskPaid || false;
I'm surprised this has never been an issue for anyone. Am I using code wrong or not following best practice? Should the constructor not be used to instantiate objects with data?
@drez769 Looks correct, would you like to fix it in a Pull Request? The lines to change are: https://github.com/jhipster/generator-jhipster/blob/d237bd8a56521826482c0c28f3679bfaa4f7b169/generators/generator-base-private.js#L879-L885
Overview of the issue
We generated an entity from a JDL file, containing some boolean attributes. The entities were generated correctly and checkboxes appeared in the adding form as expected. However, when completing all fields and pressing 'Save', the spring application controller return a 400 bad request error, as the entity object was not correct. This was caused by the checkboxes values : undefined rather than false when not checked.
Motivation for or Use Case
Unchecked checked boxes in entity form should be set to false by default when generating.
Reproduce the error
Here is the piece of code from the candidate entity we generated :
The generated HTML is the following :
The Js Console notifies that spring returns a 400 bad request error. When I display the "Candidate" json object sent to Spring, it displays :
Related issues
Suggest a Fix
There are several fixes possible, the first one being setting each checkbox to false automatically in the html when generating. As I had a lot of fields to take into account, I decided to set all the boolean values concerned to false in the candidate constructor.
Here is an exract of my candidate.model.ts file :
JHipster Version(s)
JHipster configuration, a
.yo-rc.json
file generated in the root folderEntity configuration(s)
entityName.json
files generated in the.jhipster
directoryCandidate.json
Contract.json
ContractTemplate.json
ContractTemplateMapping.json
DpaeGeneration.json
Validation.json
Browsers and Operating System
java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)
git version 2.7.4
node: v7.4.0
npm: 4.2.0
bower: 1.8.0
yeoman: 1.8.5
yarn: 0.20.3
Docker version 1.12.3, build 6b644ec