اعتبارسنجی سفارشی سمت کاربر و سمت سرور در jqGrid
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۴/۲۷ ۱۲:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace jqGrid08.Models
{
public class User
{
public int Id { set; get; }
public string Name { set; get; }
public string Email { set; get; }
public string Password { set; get; }
public string SiteUrl { set; get; }
}
} colModel: [
{
name: '@(StronglyTyped.PropertyName<User>(x => x.Name))',
index: '@(StronglyTyped.PropertyName<User>(x => x.Name))',
align: 'right', width: 150,
editable: true, edittype: 'text',
editoptions: {
maxlength: 40
},
editrules: {
required: true,
custom: true,
custom_func: function (value, colname) {
if (!value)
return [false, "لطفا نامی را وارد کنید"];
if (value.length < 3 || value.length > 40)
return [false, colname + " باید بین 3 تا 40 حرف باشد"];
else
return [true, ""];
}
}
},
], $('#list').jqGrid({
// ...
}).navGrid(
'#pager',
//enabling buttons
{ add: true, del: true, edit: true, search: false },
//edit option
{
afterSubmit: showServerSideErrors
},
//add options
{
afterSubmit: showServerSideErrors
},
//delete options
{
});
});
function showServerSideErrors(response, postdata) {
var result = $.parseJSON(response.responseText);
if (result.success === false) {
//نمایش خطای اعتبار سنجی سمت سرور پس از ویرایش یا افزودن
//و همچنین جلوگیری از ثبت نهایی فرم
return [false, result.message, result.id];
}
return [true, "", result.id];
} [HttpPost]
public ActionResult AddUser(User postData)
{
//todo: Add user to repository
if (postData == null)
return Json(new { success = false, message = "اطلاعات دریافتی خالی است" }, JsonRequestBehavior.AllowGet);
if (_usersInMemoryDataSource.Any(
user => user.Name.Equals(postData.Name, StringComparison.InvariantCultureIgnoreCase)))
{
return Json(new { success = false, message = "نام کاربر تکراری است" }, JsonRequestBehavior.AllowGet);
}
if (_usersInMemoryDataSource.Any(
user => user.Email.Equals(postData.Email, StringComparison.InvariantCultureIgnoreCase)))
{
return Json(new { success = false, message = "آدرس ایمیل کاربر تکراری است" }, JsonRequestBehavior.AllowGet);
}
postData.Id = _usersInMemoryDataSource.LastOrDefault() == null ? 1 : _usersInMemoryDataSource.Last().Id + 1;
_usersInMemoryDataSource.Add(postData);
return Json(new { id = postData.Id, success = true }, JsonRequestBehavior.AllowGet);
} colModel: [
{
name: '@(StronglyTyped.PropertyName<User>(x => x.Email))',
index: '@(StronglyTyped.PropertyName<User>(x => x.Email))',
align: 'center', width: 150,
editable: true, edittype: 'text',
editoptions: {
maxlength: 250,
dir: 'ltr'
},
editrules: {
required: true,
email: true
},
formatter: 'email'
},
], colModel: [
{
name: '@(StronglyTyped.PropertyName<User>(x => x.Password))',
index: '@(StronglyTyped.PropertyName<User>(x => x.Password))',
align: 'center', width: 70,
editable: true, edittype: 'password',
editoptions: {
maxlength: 10,
dir: 'ltr'
},
editrules: {
//required: true ---> در این حالت خاص قابل استفاده نیست
//در حالت ویرایش رکورد، ورود کلمه عبور اختیاری است
//در حالت افزودن رکورد، ورود کلمه عبور اجباری است
}
},
], $('#list').jqGrid({
// ...
}).navGrid(
'#pager',
//enabling buttons
{ add: true, del: true, edit: true, search: false },
//edit option
{
/*, beforeSubmit: function (posdata, obj) {
//در حالت ویرایش رکورد، ورود کلمه عبور اختیاری است
return [true, ""];
}*/
},
//add options
{
beforeSubmit: function (postdata, obj) {
//در حالت افزودن رکورد، ورود کلمه عبور اجباری است
if (postdata.Password == null || postdata.Password == "" || postdata.Password == undefined)
return [false, "لطفا کلمه عبور را وارد کنید"];
return [true, ""];
}
},
//delete options
{
});
}); colModel: [
{
name: '@(StronglyTyped.PropertyName<User>(x => x.SiteUrl))',
index: '@(StronglyTyped.PropertyName<User>(x => x.SiteUrl))',
align: 'center', width: 150,
editable: true, edittype: 'text',
editoptions: {
maxlength: 1000,
dir: 'ltr'
},
editrules: {
required: true,
url: true
},
formatter: function (cellvalue, options, rowObject) {
return "<a href='" + cellvalue + "' >" + cellvalue + "</a>";
},
unformat: function (cellvalue, options, cell) {
return $('a', cell).attr('href');
}
},
], <meta http-equiv="Content-Language" content="fa" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> var colMdl = [];
colMdl.push({ name: 'id', index: 'id', hidden: true });
@if (User.IsInRole("myCustomRole")) {
<text>
colMdl.push(تعریف ستون اکشن در اینجا اضافه شود);
</text>
}