Aurelia验证也不工作当数据预填充在形式

Aurelia验证也不工作当数据预填充在形式

问题描述:

我一直无法验证工作的编辑模型 - 视图的数据绑定到它的视图模型的激活方法。Aurelia验证也不工作当数据预填充在形式

我有一个created.ts工作在一个对象与相同的领域。这个文件具有几乎相同的代码 - 例外是因为这是一个创建,所以不需要将数据加载到它。在这种情况下,验证工作正常。

如果我评论加载编辑模型视图的数据的代码 - 在激活方法中 - 验证工作正常。

不用说我是SPA,Aurelia和TypeScript的新手,需要一些帮助!

下面是edit.ts代码

import { ContactRepository } from "./contactRepository"; 
import { autoinject } from "aurelia-framework"; 
import { ContactForEditDto } from "./contactForEditDto"; 
import { json } from "aurelia-fetch-client"; 
import { inject, NewInstance } from "aurelia-dependency-injection"; 
import { ValidationRules, ValidationControllerFactory, validateTrigger, 
Validator } from "aurelia-validation"; 


@autoinject 
export class Edit { 

public saveStatusMessage: string; 
public isSaved: number = -1; 
private controller = null; 
private validator = null; 
public canSave: boolean = false; 

constructor(public contactForEdit: ContactForEditDto, private repository: 
ContactRepository, private controllerFactory: ValidationControllerFactory, 
public contactFullName: string, validator: Validator) { 

console.log("starting edit controller"); 

this.controller = controllerFactory.createForCurrentScope(validator); 
this.controller.validateTrigger = validateTrigger.changeOrBlur; 
this.validator = validator; 
this.controller.subscribe(event => this.validateWhole()); 

ValidationRules 

    .ensure((c: ContactForEditDto) => c.contactFirstName) 
    .displayName("First Name") 
    .required().withMessage("\${$displayName} cannot be empty.") 
    .maxLength(50).withMessage("\${$displayName} cannot have more than 50 characters.") 

    .ensure((c: ContactForEditDto) => c.contactLastName) 
    .displayName("Last Name") 
    .required().withMessage("\${$displayName} cannot be empty.") 
    .maxLength(50).withMessage("\${$displayName} cannot have more than 50 characters.") 


    .ensure((c: ContactForEditDto) => c.contactEmailAddress) 
    .displayName("Email Address") 
    //.required().withMessage("\${$displayName} cannot be empty.") 
    .email().withMessage("\${$displayName} needs to be in a correct email address format.") 
    .maxLength(50).withMessage("\${$displayName} cannot have more than 50 characters.") 

    .ensure((c: ContactForEditDto) => c.contactPhoneNumber) 
    .displayName("Phone Number") 
    .required().withMessage("\${$displayName} cannot be empty.") 
    .maxLength(12).withMessage("\${$displayName} cannot have more than 12 characters.") 
    .matches(/\d{3}-\d{3}-\d{4}/).withMessage("'${$value}' is not a valid \${$displayName}. Please enter a phone in the format xxx-xxx-xxxx") 

    .on(ContactForEditDto); 

} 

// source https://www.jujens.eu/posts/en/2017/Jan/24/aurelia-validation/ 
workaround 3 
validateWhole() { 
this.validator.validateObject(this.contactForEdit) 
    .then(results => this.canSave = results.every(result => result.valid)); 
} 

// Returning data from here because I can return a promise 
// and let the router know when i have retrieved my initial data. 

// Activate receives a params object as defined in the route. 

activate(params) { 
console.log("ACTIVATE ON EDIT PARAMS:" + params); 
this.repository 
    .getById(params.id) 
    .then(data => { 
    console.log(data); 
    this.contactForEdit = data; 
    this.contactFullName = this.contactForEdit.contactLastName + ", " + 
    this.contactForEdit.contactFirstName; // This needs to come from a method 
    in 
    contact. 
    }); 
    } 

    edit() { 

this.saveStatusMessage = ""; 
this.isSaved = -1; 

// This will be an edit 
if (this.contactForEdit.contactId >= 1) { 

    this.repository 
    .update(this.contactForEdit) 
    .then(response => { 
     if (((response.status == 201) || (response.status == 204)) 
     && (response.ok == true)) { 
     this.isSaved = 1; 
     this.saveStatusMessage = "Successfully saved the contact"; 
     } 
     else { 
     this.isSaved = 0; 
     //response.json().then(json => this.retVal = json); 

     //TODO: get more details about the error. 
     if (response.status == 400) { 
      this.saveStatusMessage = "Unable to save the contact. Please make sure that you entered correct values for every field and try again."; 
     } 
     else { 
      this.saveStatusMessage = "Unable to save the contact."; 
     } 
     } 
    }); 
} 

} 

clearContactFields() { 
this.contactForEdit = new ContactForEditDto(-1, "", "", "", ""); 
} 


} 

下面是edit.html代码

<template> 
<form id="editContact" submit.delegate="edit()"> 
<!-- placeholder for status messages. If equal to 1 display it. If equals to 
-1 or 1 hide this.--> 
<div id="successStatusMessage" class.bind="isSaved == 1 ? 'visible' : 
'hidden'"> 
    <div id="divSuccessMessage" class="alert alert-success"> 
    <a href="#" class="close" data-dismiss="alert">&times;</a> 
    <!--<span class="glyphicon glyphicon glyphicon-ok" aria-hidden="true"> 
</span> TODO: How to get the glyphicon working? --> 
    <span class="sr-only"> Success:</span> ${saveStatusMessage} 
    </div> 
</div> 
<!-- placeholder for status messages. If equal to 0 is in error, so dislay error message. if equals to -1 or 1 hide this.--> 
<div id="errorStatusMessage" class.bind="isSaved == 0 ? 'visible' : 'hidden'"> 
    <!-- placeholder for status messages. --> 
    <div id="divErrorMessage" class="alert alert-danger"> 
    <a href="#" class="close" data-dismiss="alert">&times;</a> 
    <!-- <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> TODO: class="glyphicon glyphicon-exclamation-sign" how to get these in here for class? --> 
    <span class="sr-only"> Error:</span> ${saveStatusMessage} 
    </div> 
</div> 

<div class="panel panel-default"> 

    <div class="panel-heading"> 
    <div class="panel-title"> 
     <!--<div if.bind="isCreate"> 
     "Create a Contact" 
     </div> 

     <div if.bind="!isCreate"> 
     Edit ${contactForEdit.contactFirstName} 
     </div>--> 
     ${ "Edit " + contactFullName } 
    </div> 
    </div> 

    <div class="panel-body"> 
    <div class="form-horizontal"> 

     <div class="form-group" validation-errors.bind="editFirstNameErrors" 
      class.bind="editFirstNameErrors.length ? 'has-error' : ''"> 

     <label class="control-label col-md-2">First Name: </label> 
     <div class="col-md-10"> 
      <input type="text" 
       placeholder="First Name" 
       class="form-control" 
       value.bind="contactForEdit.contactFirstName & validate" required /> <!-- NO ${} !!!--> 

      <span class="help-block" repeat.for="editErrorInfo of editFirstNameErrors"> 
      ${editErrorInfo.error.message} 
      </span> 
     </div> 

     </div> 
     <div class="form-group" validation-errors.bind="editLastNameErrors" 
      class.bind="editLastNameErrors.length ? 'has-error' : ''"> 
     <label class="control-label col-md-2">Last Name: </label> 
     <div class="col-md-10"> 
      <input type="text" 
       placeholder="Last Name" 
       class="form-control" 
       value.bind="contactForEdit.contactLastName & validate" required /> <!-- NO ${} !!!--> 
      <span class="help-block" repeat.for="editErrorInfo of editLastNameErrors"> 
      ${editErrorInfo.error.message} 
      </span> 
     </div> 
     </div> 

     <div class="form-group" validation-errors.bind="emailErrors" 
      class.bind="editEmailErrors.length ? 'has-error' : ''"> 
     <label for="emailAddress" class="control-label col-md-2">Email: </label> 
     <div class="col-md-10"> 
      <input id="emailAddress" 
       type="text" 
       placeholder="Email Address (format: [email protected])" 
       class="form-control" 
       value.bind="contactForEdit.contactEmailAddress & validate" /> <!-- NO ${} !!!--> 
      <span class="help-block" repeat.for="editErrorInfo of editEmailErrors"> 
      ${editErrorInfo.error.message} 
      </span> 
     </div> 
     </div> 

     <div class="form-group" validation-errors.bind="editPhoneErrors" 
      class.bind="editPhoneErrors.length ? 'has-error' : ''"> 
     <label class="control-label col-md-2">Phone: </label> 
     <div class="col-md-10"> 
      <input type="text" 
       placeholder="Phone Number (format: xxx-xxx-xxxx)" 
       class="form-control" 
       value.bind="contactForEdit.contactPhoneNumber & validate" required /> <!-- NO ${} !!!--> 
      <span class="help-block" repeat.for="editErrorInfo of editPhoneErrors"> 
      ${editErrorInfo.error.message} 
      </span> 
     </div> 
     </div> 

     <button type="submit" class="btn btn-default ${canSave ? '' : 'disabled'}"> 
     <!-- Look at click.dependent when there are child with buttons calling this.--> 
     Save 
     </button> 
     <!-- AA-10-17-17 - replaced with errors per input field. <ul for.bind="controller.errors"> 
     <li repeat.for="error of controller.errors" style="color:red"> 
      ${error.message} 
     </li> 
     </ul>--> 
    </div> 
    </div> 
</div> 
</form> 
<div> 
    <a route-href="route: home" 
    class="btn btn-default"> 
    Back to list 
    </a> 

</div> 
</template> 

我希望它是因为这段代码: .getById(params.id) .then(data => { console.log(data); this.contactForEdit = data;

您的验证是针对ContactForEditDto对象,但我的猜测是您的存储库正在将一个JSON对象转换为ContactForEditDto,所以它是n根本不是一个班级。 试着这么做 console.log(data); this.contactForEdit = new ContactForEditDto(data.id, data.firstname ..etc..); console.log(data); this.contactForEdit = Object.assign(new ContactForEditDto(), data);

我们只是有类似的问题,并通过远程数据分配给我们的本土后场建立我们的验证解决了这个问题。在你的代码中,你可以在this.contactForEdit = data;

之后设置验证规则