【问题标题】:Why is breeze "required" property validation rule not firing when validating new entity?为什么在验证新实体时没有触发微风“必需”属性验证规则?
【发布时间】:2014-12-04 02:27:03
【问题描述】:

我正在尝试将 [必需] 功能用于 Breeze 的客户端验证。我可以这样做,但到目前为止我唯一的成功是使用字符串。我正在尝试对布尔值做同样的事情,但 Breeze 无法识别任何实体验证。

这是我的域代码sn-p:

     [Table("Uwrl")]
public partial class Uwrl
{
    public Uwrl()
    {
    }

    [Key]
    public int customerNumber { get; set; }

    [Required]
    [StringLength(40)]
    public string customerName { get; set; }

     [Required]
    [StringLength(50)]
    public string customerStatus {get;set;}


  public int? taxId {get;set;}

     [Required]
  public bool coAdministration{get;set;}

这是我的控制器代码 sn-p 用于测试验证错误:

            var testEntity = UWRLService.createEntity(entityName, uwrl.customerData);
          if (!testEntity.entityAspect.validateEntity()) { alert("Didn't VALIDATE!"); }

这是我的观点,以确保我的特定属性中包含 z-validate:

                    <td style="text-align:right">
                    Co-Administration:<select ng-model="uwrl.coAdministration" data-z-required>
                        <option></option>
                        <option value="True">Yes</option>
                        <option value="False">No</option>
                    </select>
                </td>

customerName 和 customerStatus 都有效。 customerStatus 甚至是一个选择(下拉)——与 coAdministration 属性完全一样。如何验证布尔值?验证字符串对我来说非常有效......有什么区别?

【问题讨论】:

  • 只是一个猜测,但 coAdministration 属性不可为空,所以轻而易举地将其初始化为 false 对吗? False 不为空,因此不会触发所需的验证。通过在调用 createEntity 之后添加这一行来检查这一点:console.log(testEntity.coAdministration)
  • 所以你的意思是我需要改变这个:[Required] public bool coAdministration{get;set;} 变成这个:[Required] public bool?共同管理{get;set;}
  • 是的-这是我的猜测-console.log(testEntity.coAdministration) 返回了什么?
  • 它返回错误,哈哈。如何防止微风设置此值?
  • 再次猜测,但我认为您可以将 null 分配给 coAdministration 属性或使该属性可以为空,这样它就不会被初始化为 false

标签: validation boolean breeze client-side required


【解决方案1】:

布尔属性不可为空,这意味着它的初始值为“false”。

“false”不为 null 或为空,因此未触发所需的验证规则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多