是否可以在组件中包装Aurelia.js验证?

时间:2016-09-20 16:00:30

标签: validation aurelia aurelia-validation aurelia-framework

主要思想是在一个根组件中包含aurelia-validation的相当常见的逻辑。

包装器示例

import { inject, bindable, containerless, NewInstance } from 'aurelia-framework';
import { ValidationController, validateTrigger, ValidationRules } from 'aurelia-validation';
import FormValidationRenderer from '../custom/form-validation-renderer';

@inject(Element, NewInstance.of(ValidationController))
export class SomeForm {
    @bindable class = "class";

    constructor(element, controller) {
        this.element = element;

        // other way it would be everywhere
        this.controller = controller;
        this.controller.addRenderer(new FormValidationRenderer());
        this.controller.validateTrigger = validateTrigger.manual;
    }

    back() {
        window.history.back();
    }

    // hide all imports to simplify usages.
    rules() {
        return ValidationRules;
    }

    setValidation(model, rules) {
        this.controller.addObject(model, rules);
    }

    submit() {
        this.element.dispatchEvent(new CustomEvent('submit', {}));
    }

    validate() {
        return this.controller.validate();
    }
}

我将如何使用它:

view.html

 <custom-form view-model.ref="form" submit.trigger="submit()">
        <h1>Form footer test</h1>
        <input type="text" class="form-control" value.bind="params.text & validate" />
 </custom-form>

view.js

export class View {
    params = {
        text: ''
    };

    attached() {
        this.setValidation(this.params, this.rules().ensure('text')
            .required().rules);

    }
    submit() {
        this.validate().then(err => {
            err.forEach(console.log);
        });
    }
}

我唯一遇到的问题是&amp; validate标记。 validate-binding-behavior.js内的aurelia-validation内部@inject。在第36行。

如果没有NewInstance.of(ValidationController) ValidateBindingBehavior.prototype.bind = function (binding, source, rulesOrController, rules) { var _this = this; // identify the target element. var target = this.getTarget(binding, source); // locate the controller. var controller; if (rulesOrController instanceof validation_controller_1.ValidationController) { controller = rulesOrController; } else { controller = source.container.get(aurelia_dependency_injection_1.Optional.of(validation_controller_1.ValidationController)); rules = rulesOrController; } if (controller === null) { throw new Error("A ValidationController has not been registered."); } 视图,我们就无法使用该属性......

有可能在它上面进行解决吗?

 public class App
{
    [Key]
    public Guid Id { get; set; }

    public virtual IEnumerable<Build> Builds { get; set; }
}

public class Build
{
    [Key]
    public Guid Id { get; set; }

    public Guid AppId { get; set; }

    [ForeignKey("AppId")]
    public virtual App App { get; set; }
}


public class TestContext : DbContext
{
    public TestContext() : base("name=DefaultConnection")
    {
        Database.SetInitializer<TestContext>(new CreateDatabaseIfNotExists<TestContext>());
    }

    public DbSet<App> Apps { get; set; }
    public DbSet<Build> Builds { get; set; }
}

0 个答案:

没有答案