- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 Silverlight 4 应用程序中有一个简单的测试页面,我试图在其中触发自定义验证规则。
我有一个 TextBox 和一个 Button,我在 TextBlock 中显示验证结果。我的 View 模型有一个 Name 属性,它绑定(bind)到 TextBox 的 Text 属性。我在 Name 属性上有两个验证属性,[Required]
和 [CustomValidation]
。
当我点击提交按钮时,必需的验证器正确触发,但我的自定义验证器的验证方法内的断点永远不会被命中。我不明白这是为什么,因为我认为我已经非常仔细地遵循了 MS 的示例:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute(v=vs.95).aspx
这是 View 模型的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using GalaSoft.MvvmLight.Command;
namespace MyProject
{
// custom validation class
public class StartsCapitalValidator
{
public static ValidationResult IsValid(string value)
{
// this code never gets hit
if (value.Length > 0)
{
var valid = (value[0].ToString() == value[0].ToString().ToUpper());
if (!valid)
return new ValidationResult("Name must start with capital letter");
}
return ValidationResult.Success;
}
}
// my view model
public class ValidationTestViewModel : ViewModelBase
{
// the property to be validated
string _name;
[Required]
[CustomValidation(typeof(StartsCapitalValidator), "IsValid")]
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value, () => Name); }
}
string _result;
public string Result
{
get { return _result; }
private set { SetProperty(ref _result, value, () => Result); }
}
public RelayCommand SubmitCommand { get; private set; }
public ValidationTestViewModel()
{
SubmitCommand = new RelayCommand(Submit);
}
void Submit()
{
// perform validation when the user clicks the Submit button
var errors = new List<ValidationResult>();
if (!Validator.TryValidateObject(this, new ValidationContext(this, null, null), errors))
{
// we only ever get here from the Required validation, never from the CustomValidator
Result = String.Format("{0} error(s):\n{1}",
errors.Count,
String.Join("\n", errors.Select(e => e.ErrorMessage)));
}
else
{
Result = "Valid";
}
}
}
}
这是 View :
<navigation:Page x:Class="Data.Byldr.Application.Views.ValidationTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">
<Grid Width="400">
<StackPanel>
<TextBox Text="{Binding Name, Mode=TwoWay}" />
<Button Command="{Binding SubmitCommand}" Content="Submit" />
<TextBlock Text="{Binding Result}" />
</StackPanel>
</Grid>
</navigation:Page>
最佳答案
你为什么不像这样创建你自己的验证属性..
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class StartsCapital : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var text = value as string;
if(text == null)
return ValidationResult.Success;
if (text.Length > 0)
{
var valid = (text[0].ToString() == text[0].ToString().ToUpper());
if (!valid)
return new ValidationResult("Name must start with capital letter");
}
return ValidationResult.Success;
}
}
然后像这样使用它
// my view model
public class ValidationTestViewModel : ViewModelBase
{
// the property to be validated
string _name;
[Required]
[StartsCapital]
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value, () => Name); }
}
关于c# - CustomValidation 属性似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4687422/
到目前为止,我只需要在我的应用程序中构建一个 java customValidator。在当前的项目中,我现在面临着需要在应用程序中注册多个自定义 validator 的必要性。 由于我的 bean
我有一个自定义验证器,希望它在客户端而不是服务器端进行验证。这是我的功能: function checkCampDockTemplate(source, args) { var
我模拟了一个非常简单的页面,我完全不明白为什么当我在两个文本框之间切换时 javascript 不会触发:- c function checkthisvalue(source, ar
好吧,iv 在此处和其他一些网站上跟踪了多个用于多个 asp 复选框的 CustomValidators,但在选中复选框时实际验证它有点困难。 虽然我尝试了多种答案,但我特别选择了一个答案。我已经知道
那么问题来了。我在 .ascx 控件中有一个 DropDownList,上面有一个 CustomValidator。此控件具有由 PageLoad 中的属性设置的 ValidationGroup。触发
我有以下问题: Page 上有很多验证器,除了一个之外,所有验证器都同时具有客户端和服务器端验证。其中之一只有服务器端验证。 问题: 我的页面正在发布,即使某些客户端验证无效。我认为它应该首先验证客户
考虑下面的示例,它检查 fromDate 和 toDate 是否是有效日期以及 fromDate 是否小于 toDate: @CustomValidator(type = "DateValidator
有没有办法让自定义验证器从两个控件中获取输入,例如两个复选框并确保只有一个被选中? 还是我做错了,更重要的是,如果我错了,你能给我指出正确的方向吗? 即使这是错误的方向,实际上我也想知道如何做到这一点
在提交表单之前,我需要测试总和 (txtA + txtB) 是否大于 100。是否可以使用 CustomValidator 执行此操作,因为我不知道我是否可以选择控件中的 2 个文本框进行验证
我有一个 CustomValidator 并且我定义了它的每个可能的参数: PS:我有一个用于相同文本框的 RequiredFieldValidator,我不想检查空值。 以下是其他形式的对象:
我的 Silverlight 4 应用程序中有一个简单的测试页面,我试图在其中触发自定义验证规则。 我有一个 TextBox 和一个 Button,我在 TextBlock 中显示验证结果。我的 Vi
我有如下一段 asp: ... Username: * * 然后是函数: protected void UserValidate(object source, ServerValidat
我认为我试图做的事情是微不足道的,但结果却给我带来了很大的麻烦。这是情况。 我有两个单选按钮(使用 RadButton 实现)和一个 RadTextBox。我想检查客户端,然后在选择两个 radio
我有问题:我的页面上有一个自定义验证器,用于验证 imieTextbox 控件。但这不起作用。我也不知道为什么。 该方法来自register.aspx.cs文件: protected void Cus
我正在尝试弄清楚如何在业务对象上使用验证。 到目前为止,我只在 CustomValidator 上看到过只检查 1 个错误的示例。我有两个带有 DateTime 输入的字段,应该检查 3 个或更多错误
我有一个带有 ASP 验证器(必填字段、正则表达式...)的 ASP .NET 页面以及用于额外验证的 Java 脚本函数(例如,检查第二个日期是否大于第一个日期等)。 我通常这样做: fu
我正在尝试asp.net的customvalidator控件,但问题是它没有调用javascript函数。但是它正在调用验证方法的服务器端版本.. JavaScript 函
我正在尝试使用关键字“OnServerValidate”来使用 CustomValidator 这是我在 .aspx 文件中设置它的地方: Email Address
我在转发器中有一个自定义验证器 作为测试,我在 OnClick 事件上尝试了以下代码 foreach(RepeaterItem it
我在表单上放置了一个 CustomValidator。我还没有设置它的 ControlToValidate 属性。在其 ServerValidate 事件中,我编写了以下内容: protected v
我是一名优秀的程序员,十分优秀!