我生成了一个Jhipster应用,并且正在尝试在用户管理部分工作。
我希望用户从他们的电子邮件中登录,因此删除了登录字段。 我可以登录,或从主页创建一个新帐户。 但是在用户管理页面中,我收到此错误:
{
"type" : "https://www.jhipster.tech/problem/constraint-violation",
"title" : "Method argument not valid",
"status" : 400,
"path" : "/api/users/resource",
"message" : "error.validation",
"fieldErrors" : [ {
"objectName" : "userDTO",
"field" : "email",
"message" : "NotBlank"
} ]
}
这是我的代码:
user-management-update.component.html
<div class="form-group">
<label class="form-control-label" jhiTranslate="userManagement.email">Email</label>
<input type="email" class="form-control" name="email" #emailInput="ngModel" [(ngModel)]="user.email" minlength="5" required maxlength="254" pattern="[a-zA-Z0-9.-_]{1,}@[a-zA-Z.-]{2,}[.]{1}[a-zA-Z]{2,}" email>
<div *ngIf="emailInput.dirty && emailInput.invalid">
<small class="form-text text-danger" *ngIf="emailInput.errors.required" jhiTranslate="entity.validation.required">
This field is required.
</small>
<small class="form-text text-danger"
*ngIf="emailInput.errors.maxlength" jhiTranslate="entity.validation.maxlength"
translateValues="{max: 100}">
This field cannot be longer than 100 characters.
</small>
<small class="form-text text-danger"
*ngIf="emailInput.errors.minlength" jhiTranslate="entity.validation.minlength"
translateValues="{min: 5}">
This field is required to be at least 5 characters.
</small>
<small class="form-text text-danger"
*ngIf="emailInput.errors.email" jhiTranslate="global.messages.validate.email.invalid">
Your email is invalid.
</small>
</div>
</div>
<!-- other inputs with no error -->
<div>
<button type="button" class="btn btn-secondary" (click)="previousState()">
<fa-icon [icon]="'ban'"></fa-icon> <span
jhiTranslate="entity.action.cancel">Cancel</span>
</button>
<button type="submit" [disabled]="editForm.form.invalid || isSaving" class="btn btn-primary">
<fa-icon [icon]="'save'"></fa-icon> <span jhiTranslate="entity.action.save">Save</span>
</button>
</div>
我没有更改TS部分。我不太了解电子邮件的问题,在键入电子邮件时可以很好地进行验证(例如,我使用“ example@example.com”),没有“红色”,可以单击按钮。
这里是TS部分,以防万一:
user-management-update.component.ts
@Component({
selector: 'jhi-user-mgmt-update',
templateUrl: './user-management-update.component.html'
})
export class UserMgmtUpdateComponent implements OnInit {
user: User;
promoters: Promoter[];
promoter: Promoter;
selectedPromoterId: number;
languages: any[];
authorities: any[];
isSaving: boolean;
constructor(
private languageHelper: JhiLanguageHelper,
private userService: UserService,
private route: ActivatedRoute,
private router: Router,
private promoterService: PromoterService
) {}
ngOnInit() {
this.isSaving = false;
this.route.data.subscribe(({ user }) => {
this.user = user.body ? user.body : user;
});
this.authorities = [];
this.userService.authorities().subscribe(authorities => {
this.authorities = authorities;
});
this.languageHelper.getAll().then(languages => {
this.languages = languages;
});
this.promoterService.list().subscribe(response => (this.promoters = response.body), () => {}, () => {});
}
previousState() {
this.router.navigate(['/admin/user-management']);
}
save() {
this.isSaving = true;
if (this.user.id !== null) {
this.userService
.updateWithPromoter(this.user, this.selectedPromoterId)
.subscribe(response => this.onSaveSuccess(response), () => this.onSaveError());
} else {
this.userService
.createWithPromoter(this.user, this.selectedPromoterId)
.subscribe(response => this.onSaveSuccess(response), () => this.onSaveError());
}
}
onChange(value: any) {
this.selectedPromoterId = value;
}
private onSaveSuccess(result) {
this.isSaving = false;
this.previousState();
}
private onSaveError() {
this.isSaving = false;
}
}
user.service.ts
@Injectable({ providedIn: 'root' })
export class UserService {
private resourceUrl = SERVER_API_URL + 'api/users';
constructor(private http: HttpClient) {}
create(user: IUser): Observable<HttpResponse<IUser>> {
return this.http.post<IUser>(this.resourceUrl, user, { observe: 'response' });
}
createWithPromoter(user: IUser, promoterId: number): Observable<HttpResponse<{ IUser, number }>> {
return this.http.post<{ IUser, number }>(this.resourceUrl + '/resource', { user, promoterId }, { observe: 'response' });
}
update(user: IUser): Observable<HttpResponse<IUser>> {
return this.http.put<IUser>(this.resourceUrl, user, { observe: 'response' });
}
updateWithPromoter(user: IUser, promoterId: number): Observable<HttpResponse<{ IUser, number }>> {
return this.http.put<{ IUser, number }>(this.resourceUrl + '/resource', { user, promoterId }, { observe: 'response' });
}
find(login: string): Observable<HttpResponse<IUser>> {
return this.http.get<IUser>(`${this.resourceUrl}/${login}`, { observe: 'response' });
}
query(req?: any): Observable<HttpResponse<IUser[]>> {
const options = createRequestOption(req);
return this.http.get<IUser[]>(this.resourceUrl, { params: options, observe: 'response' });
}
delete(login: string): Observable<HttpResponse<any>> {
return this.http.delete(`${this.resourceUrl}/${login}`, { observe: 'response' });
}
authorities(): Observable<string[]> {
return this.http.get<string[]>(SERVER_API_URL + 'api/users/authorities');
}
}
谢谢。