• WCF LINQ TO SQL 验证


    客户端生成验证代码代码

    <script type="text/javascript" src="../Scripts/jquery-1.6.2.min.js"></script>
    <script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

    <div class="editor-label">
                <span class="fieldname">Username:</span>
                <input type="text" name="Username" id="Username" value="" data-val="true" data-val-required="You must Input Username." />
                <span class="field-validation-valid" data-valmsg-for="Username" data-valmsg-replace="true"></span>
            </div>
            <div class="editor-label">
                <span class="fieldname">Password:</span>
                <input type="password" name="Password" id="Password" value="" data-val="true" data-val-required="You must Input Password." data-val-length="at least 6 letters" data-val-length-min="6" />
                <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
            </div>
            <div class="editor-label">
                <span class="fieldname">Confirm:</span>
                <input type="password" name="Confirm" id="Confirm" value="" class="" data-val="true"  data-val-equalto="the same password." data-val-equalto-other="Password" />
                <span class="field-validation-valid" data-valmsg-for="Confirm" data-valmsg-replace="true"></span>
            </div>

    linq to sql 自动生成,覆盖验证属性的解决方案:

      首先我们可以简单的想,直接在Linq to SQL 生成的cs文件中引用System.ComponentModel.DataAnnotations,然后在对应的Property中加我们所需要的Annotatio,从理想的状态来讲,我们说这个方案是可行的,但是前提条件是你所设计的Table、Stored Procedure或者Function 结构不会再更改,因为当我们Linq to SQL里的对象在Database的结构发生变化时,我们需要删除Linq to SQL文件中的对象,然后从Database重新的拖入,以生成更新后的强类型文件,那么我们之前加入的Annotation也会被删除掉,后果就是我们需要重新加注Data Annotation,这样会增加我们很大的工作量, 那么我们如何来解决这样的问题呢?

      在Linq to SQL中,如果你的Database的表的名称是T_PRODUCT,那么当你把表插入到Linq to SQL的文件中生,他会帮我们生成对应名称为Staffs的Class。

    我们的解决方案是定义一个新的Class(我们这里叫StaffsAnnotation
    ),然后在里面加入哪些需要加注Annotation的Properties(Property的名字需要和Staffs表中的字段相同)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel;

    namespace SkySolar.Models
    {
    public class StaffsAnnotation
    {
    [Required(ErrorMessage
    ="LoginName is Required.")]
    [StringLength(
    50,ErrorMessage="Max Length of Loginname can not greate than 50 characters.")]
    public string Loginname { get; set; }

    [Required(ErrorMessage
    ="PassWord is Required.")]
    [StringLength(
    50,ErrorMessage="Max Length of Product Name can not greater than 20 characters.")]
    public string PassWord { get; set; }

    [DisplayName(
    "Name:")]
    [Required(ErrorMessage
    ="Realname is Required.")]
    public string Realname { get; set; }

    }
    }

    之后:

    using System.ComponentModel.DataAnnotations;
    namespace SkySolar.Models
    {
    [MetadataType(
    typeof(StaffsAnnotation))]
    public partial class Staffs
    {

    }
    }

    到此实现对Linq to SQL生成的强类型Class加Validation。

    <script src="../js/jquery.js" type="text/javascript"></script>
    <script src="../js/jquery.validate.js" type="text/javascript"></script>
    <script src="./js/jquery.metadata.js" type="text/javascript"></script>
    $().ready(function() { $(
    "#signupForm").validate(); });
    <form id="signupForm" method="get" action="">
    <p>
    <label for="firstname">
    Firstname
    </label>
    <input id="firstname" name="firstname" class="required" />
    </p>
    <p>
    <label for="email">
    E
    -Mail</label>
    <input id="email" name="email" class="required email" />
    </p>
    <p>
    <label for="password">
    Password
    </label>
    <input id="password" name="password" type="password" class="{required:true,minlength:5}" />
    </p>
    <p>
    <label for="confirm_password">
    确认密码
    </label>
    <input id="confirm_password" name="confirm_password" type="password" class="{required:true,minlength:5,equalTo:'#password'}" />
    </p>
    <p>
    <input class="submit" type="submit" value="Submit" />
    </p>
    </form>
    meta:
    "validate"
    <input id="password1" name="password" type="password" class="{validate:{required:true,minlength:5}}" />
    $.metadata.setType(
    "attr", "validate");这样可以使用validate="{required:true}"的方式,或者class="required",但class="{required:true,minlength:5}"将不起作用
    2.将校验规则写到代码中 $().ready(function() { $("#signupForm").validate({ rules: { firstname:
    "required", email: { required: true, email: true }, password: { required: true,
    minlength:
    5 }, confirm_password: { required: true, minlength: 5, equalTo: "#password"
    } }, messages: { firstname:
    "请输入姓名", email: { required: "请输入Email地址", email: "请输入正确的email地址"
    }, password: { required:
    "请输入密码", minlength: jQuery.format("密码不能小于{0}个字符") }, confirm_password:
    { required:
    "请输入确认密码", minlength: "确认密码不能小于5个字符", equalTo: "两次输入密码不一致不一致" } } });
    });
    //messages处,如果某个控件没有message,将调用默认的信息
    <form id="Form1" method="get" action="">
    <p>
    <label for="firstname">
    Firstname
    </label>
    <input id="Text1" name="firstname" />
    </p>
    <p>
    <label for="email">
    E
    -Mail</label>
    <input id="Text2" name="email" />
    </p>
    <p>
    <label for="password">
    Password
    </label>
    <input id="password2" name="password" type="password" />
    </p>
    <p>
    <label for="confirm_password">
    确认密码
    </label>
    <input id="Password3" name="confirm_password" type="password" />
    </p>
    <p>
    <input class="submit" type="submit" value="Submit" />
    </p>
    </form>
    <form id="form2" method="get">
    <div data-valmsg-summary="true">
    <ul>
    </ul>
    </div>
    <div>
    <input type="text" id="tReq" name="tReq" data-val="true" data-val-required="不可空白" />
    <span data-valmsg-for="tReq"></span>
    </div>
    <div>
    <input type="text" id="tAccept" name="tAccept" value="a.doc" data-val="true" data-val-accept="檔名須為.jpg、.gif或.png"
    data
    -val-accept-exts="jpg|gif|png" />
    <span data-valmsg-for="tAccept"></span>
    </div>
    <div>
    <input type="text" id="tRegex" name="tRegex" value="123-ABC@" data-val="true" data-val-regex="車牌格式須為999-999"
    data
    -val-regex-pattern="[0-9A-Z]{3}-[0-9A-Z]{3}" />
    <span data-valmsg-for="tRegex"></span>
    </div>
    <div>
    <input type="text" id="tDigit" name="tDigit" value="-1234" data-val="true" data-val-digits="只接受數字字元" />
    <span data-valmsg-for="tDigit"></span>
    </div>
    <div>
    <input type="text" id="tNum" name="tNum" value="-1,234.56A" data-val="true" data-val-number="必須為有效數字" />
    <span data-valmsg-for="tNum"></span>
    </div>
    <div>
    <input type="text" id="tDate" name="tDate" value="X/01/X2000" data-val="true" data-val-date="必須為日期(僅粗略檢查)" />
    <span data-valmsg-for="tDate"></span>
    </div>
    <div>
    <input type="text" id="tEmail" name="tEmail" value="jeffrey @mail.com" data-val="true"
    data
    -val-email="必須為Email" />
    <span data-valmsg-for="tEmail"></span>
    </div>
    <div>
    <input type="text" id="tUrl" name="tUrl" value="http:// blog.darkthread.net" data-val="true"
    data
    -val-url="必須為有效網址" />
    <span data-valmsg-for="tUrl"></span>
    </div>
    <div>
    <input type="text" id="tLen" name="tLen" value="TTT" data-val="true" data-val-length="長度須介於4到8之間"
    data
    -val-length-min="4" data-val-length-max="8" />
    <span data-valmsg-for="tLen"></span>
    </div>
    <div>
    <input type="text" id="tRange" name="tRange" value="5" data-val="true" data-val-range="須介於10到100"
    data
    -val-range-min="10" data-val-range-max="100" />
    <span data-valmsg-for="tRange"></span>
    </div>
    <div>
    <input type="text" id="tEq" name="tEq" value="99" data-val="true" data-val-equalto="必須與上方欄位內容相同"
    data
    -val-equalto-other="tRange" />
    <span data-valmsg-for="tEq"></span>
    </div>
    <div>
    <input type="submit" id="send" value="Send" />
    </form>
  • 相关阅读:
    C++疑难杂症
    程序中的错误、异常处理框架设计
    客户端ARPG角色行为模型
    http协议
    MySQL数据库开发(2)
    MySQL数据库开发(1)
    网络编程进阶及并发编程
    网络编程-SOCKET开发
    面向对象编程
    常用模块
  • 原文地址:https://www.cnblogs.com/netact/p/2177854.html
Copyright © 2020-2023  润新知