简体   繁体   English

在带有Breeze的基于角度的SPA中的Kendo DatePicker中进行验证

[英]Validation in Kendo DatePicker in an Angular-based SPA with Breeze

I'm building a SPA with Angular. 我正在使用Angular构建SPA。 The backend is an ASP.NET MVC WebApi server. 后端是ASP.NET MVC WebApi服务器。 The Model is persisted with the help of Entity Framework. 该模型在实体框架的帮助下得以保留。 Breeze picks up the metadata offered by Entity Framework and transfers it to the SPA app in the browser. Breeze拾取了Entity Framework提供的元数据,并将其传输到浏览器中的SPA应用程序。

In one form I'm using a Kendo DatePicker. 在一种形式中,我正在使用Kendo DatePicker。 It's configured with a german date format ("dd.MM.yyyy"). 它使用德语日期格式(“ dd.MM.yyyy”)配置。 I also annotated the input tag with the data-z-validate attribute. 我还用data-z-validate属性注释了输入标签。

When someone enters an invalid date string I'm want to see the red warning tooltip with a validation error message. 当有人输入无效的日期字符串时,我想看到带有警告错误消息的红色警告工具提示。 Unfortunately this does not happen. 不幸的是,这没有发生。 Even worse it seems that no validation is executed at all. 更糟糕的是,似乎根本没有执行任何验证。

Do I need to add a custom validator on the client side? 我是否需要在客户端添加自定义验证器? If so how do I do that? 如果是这样,我该怎么做?

Angular view: 角度图:

<input kendo-date-picker id="aktion-termin" type="text" placeholder="Termin"
       k-ng-model="vm.aktion.termin" k-format="'dd.MM.yyyy'" 
       data-z-validate />

Model class: 型号类别:

public class Aktion
{

    ...

    [DataType(DataType.Date)]
    public DateTime? Termin { get; set; }

    ...    
}

Metadata: 元数据:

"entityType":[

    {
        "name":"Aktion",

        ...

        "property":[

            ...

            {
                "name":"Termin",
                "type":"Edm.DateTime"
            },

            ...
        ],

        ...
    }

Additional Information 附加信息

There is another field on the same form which is required. 在同一表格上还有一个必填字段。 Here the validation works well. 在这里,验证效果很好。 The validation error popup comes up when I leave the field empty. 当我将该字段留空时,将弹出验证错误弹出窗口。 So the whole infrastructure seems to work generally. 因此,整个基础架构似乎都能正常工作。 But it is a standard input field. 但这是一个标准的输入字段。 No Kendo involved. 没有剑道参与。

<input class="form-control" id="aktion-titel" placeholder="Name"
       data-ng-model="vm.aktion.titel" data-z-validate />

The said date field is also functional. 所述日期字段也起作用。 When I enter a valid date string according to the given format (eg "02.01.2015") I can see the correct value in the database after a save command. 当我根据给定的格式(例如“ 02.01.2015”)输入有效的日期字符串时,在执行保存命令后,可以在数据库中看到正确的值。 I'm also able to see the inserted date in the JSON which is sent by Breeze to the server. 我还能够看到Breeze发送到服务器的JSON中的插入日期。 Everthing works properly as long as the inserted date string satisfies the given date format. 只要插入的日期字符串满足给定的日期格式,一切都可以正常工作。

{
    "$id":"1",
    "$type":"Breeze.ContextProvider.SaveResult, Breeze.ContextProvider",
    "Entities":[
        {
            "$id":"2",
            "$type":"Aktionen.Model.Aktion, Aktionen.Model",
            "Id":"a0b2639a-c79b-46ef-a5ee-6982dcbe1782",
            "Titel":"abc",
            "Termin":"2015-01-02T00:00:00.000+01:00",        // <----- ok

            ...
        },

        ...
    ],
    "KeyMappings":[

    ],
    "Errors":null
}

When I enter rubbish (eg "xyz") a null value is sent to the server. 当我输入垃圾(例如“ xyz”)时,会将空值发送到服务器。 And a null is what I'm seeing in the database. 我在数据库中看到的是null。

{
    "$id":"1",
    "$type":"Breeze.ContextProvider.SaveResult, Breeze.ContextProvider",
    "Entities":[
        {
            "$id":"2",
            "$type":"Aktionen.Model.Aktion, Aktionen.Model",
            "Id":"736f976c-9e3b-42ee-90a9-ca421b6c2843",
            "Titel":"The Name",
            "Termin":null,                                 // <------ bad

            ...
        },

        ...
    ],
    "KeyMappings":[

    ],
    "Errors":null
}

That is not exactly the behavior I had in mind. 那不完全是我的初衷。 The application should inform the user about the invalid date by showing the validation popup. 应用程序应通过显示验证弹出窗口来通知用户有关无效日期的信息。 And it should not accept the record and it should not send it the the server. 并且它不应接受该记录,也不应将其发送给服务器。

When I bypass the kendo date picker by setting the date string programmatically I'm getting a validaton error from Breeze: "'termin' must be a date". 当我通过编程设置日期字符串来绕过剑道日期选择器时,我从Breeze收到一个validaton错误:“'termin'必须是日期”。

function save() {
    if (vm.isSaving) { return common.$q.when(null); }
    if (!canSave()) { return common.$q.when(null); }

    vm.aktion.termin = "xyz";    // <--------- rubbish

    vm.isSaving = true;
    return datacontext.save().then(function() {
        vm.isSaving = false;
    }, function () {
        vm.isSaving = false;
    });
}

When I debug this function I can see the values of the property vm.aktion.termin: 当我调试此函数时,我可以看到vm.aktion.termin属性的值:

"01.01.2015" --> Thu Jan 01 2015 00:00:00 GMT+0100 (Mitteleuropäische Zeit) “ 01.01.2015”-> 2015年1月1日星期四00:00:00 GMT + 0100(MitteleuropäischeZeit)

"xzy" --> null “ xzy”->空

Given this output I assume that the kendo date picker spits out the date string only when the input is valid. 给定此输出,我假设kendo日期选择器仅在输入有效时才吐出日期字符串。

I'm not sure what HTML Kendo is actually generating around the date picker. 我不确定在日期选择器周围实际上会生成什么HTML Kendo。 I'll bet that z-validate can't recognize it; 我敢打赌, z-validate无法识别它。 z-validate is kind of a prototype or proof-of-concept and it certainly wasn't designed w/ Kendo controls in mind. z-validate是一种原型或概念验证,肯定不是在考虑使用Kendo控件的情况下设计的。

Can you see if the new value is written through to the entity's date property? 您是否可以查看新值是否写入实体的date属性中? Also does Breeze validate the date when you set the property programmatically rather than through any UI binding? 当您以编程方式而不是通过任何UI绑定设置属性时,Breeze还会验证日期吗? Let me know if you don't know how to test for that. 如果您不知道如何测试,请告诉我。

Kendo UI Controls intraduce a new k-ng-model directive which you are using for data binding instead of the default Angular ng-model . Kendo UI Controls引入了一个用于数据绑定的新k-ng-model指令,而不是默认的Angular ng-model z-validate by default only recognizes ng-model . 默认情况下, z-validate仅识别ng-model

See this Pull Request for z-validate which adds support for Kendo UI Controls to `z-validate. 请参阅此对z-validate 请求 ,该请求将对Kendo UI控件的支持添加到`z-validate。

It's up to @Ward for deciding if this fits in the default z-validate distribution or not, but you still can apply the PR to your used version of breeze.directives.js 由@Ward决定是否适合默认的z-validate发行版,但是您仍然可以将PR应用于所使用的breeze.directives.js版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM