I can't get Aurelia-Validate to work on the fields in my calendar record.
calendar.html (excerpt)
<form>
<div class="form-group">
<label class="control-label" for="cal_name_orig">Calendar name</label>
<input type="text" class="form-control" id="cal_name_orig" value.bind="calendar.cal_name_orig & validate">
</div>
<div class="form-group">
<label class="control-label" for="cal_name_tran">Translated name</label>
<input type="text" class="form-control" id="cal_name_tran" value.bind="calendar.cal_name_tran & validate">
</div>
</form>
calendars.js (simplified):
import { inject, NewInstance } from 'aurelia-dependency-injection';
import { ValidationController, ValidationRules } from 'aurelia-validation';
import { BootstrapFormRenderer } from '../../common/bootstrap-form-renderer';
@inject(NewInstance.of(ValidationController))
export class CalendarForm {
controller = null;
constructor(controller) {
this.controller = controller;
this.controller.addRenderer(new BootstrapFormRenderer());
this.calendar = {
cal_name_orig = "",
cal_name_tran = ""
}
}
validateCalendar() {
let v = this.controller.validate();
console.log(v);
}
}
ValidationRules
.ensure('calendar.cal_name_orig').required().minLength(5).maxLength(20)
.ensure('calendar.cal_name_tran').minLength(5).maxLength(20)
.on(CalendarForm);
If I change value.bind="calendar.cal_name_orig & validate"
to value.bind="cal_name_orig & validate"
and .ensure('calendar.cal_name_orig')
to .ensure('cal_name_orig')
, the validation works and renders on the form (but the data doesn't bind to the right part of the data in this class.
How can I get Aurelia Validate to recognize calendar.cal_name_orig?
The solution was to move ValidationRules into the class constructor()
and change the last line to on(this.calendar)
. I also needed to remove the calendar.
from each of the ensure rules.
ValidationRules
.ensure('cal_name_orig').required().minLength(5).maxLength(20)
.ensure('cal_name_tran').required().minLength(5).maxLength(20)
.on(this.calendar);
来源:https://stackoverflow.com/questions/40250698/aurelia-validation-not-working-with-object-properties