Editor - validation clears form

Editor - validation clears form

montoyammontoyam Posts: 568Questions: 136Answers: 5

In my asp.net project, when form data does not meet all validation rules, the messages show under the fields in red correctly, however, all the fields are blanked out and the user needs to retype all entries, even those without errors.

        var RegistrationEditor = new $.fn.dataTable.Editor({
            ajax: 'api/Users',
            table: '#login',
            fields: [
                { label: "Email Address:", name: "Users.WindowsLogin", def: userNameCookie, type:"readonly"},
                { label: "First Name", name: "Users.UserFirstName" },
                { label: "Last Name", name: "Users.UserLastName" },
                { label: "Email Address:", name: "Users.UserEmail" },
                {
                    label: "Department:",
                    name: "Users.UserDepartmentID",
                    type: "select",
                    placeholder: "<Select a Department>",
                    placeholderValue: null,
                    placeholderDisabled: false

                },
                { label: "IsAdmin", name: "Users.IsAdmin", def: 0, type: "hidden" },
                { label: "IsApproved", name: "Users.IsApproved", def: 0, type: "hidden" },
                {
                    label: "RecordAdded",
                    name: "Users.RecordAdded",
                    def: function () {
                        var d = new Date();
                        return d;
                    },
                    type: "hidden"
                }
            ]
        });
    public class UsersController : ApiController
    {

        [Route("api/Users")]
        [HttpGet]
        [HttpPost]
        public IHttpActionResult Users()
        {
            var request = HttpContext.Current.Request;
            var settings = Properties.Settings.Default;

            using (var db = new Database(settings.DbType, settings.DbConnection))
            {

                var response = new Editor(db, "Users", "WindowsLogin")
                    .Model<UsersModel>("Users")
                    .Field(new Field("Users.RecordAdded")
                        .Set(false)
                    )
                    .Field(new Field("Users.UserDepartmentID")
                        .Validator(Validation.Numeric())
                        .Validator(Validation.NotEmpty(new ValidationOpts
                        {
                            Empty = false,
                            Message = "You must select your Department"
                        }))
                        .Options(new Options()
                                    .Table("Departments")
                                    .Value("DepartmentID")
                                    .Label("DepartmentName")
                        )
                    )
                    .Field(new Field("Users.UserEmail")
                        .Validator(Validation.Numeric())
                        .Validator(Validation.Email(new ValidationOpts
                        {
                            Empty = false,
                            Message = "A valid e-mail address is required"
                        }))
                    )
                    .Field(new Field("Departments.DepartmentName")
                        .Validator(Validation.NotEmpty())
                    )
                    .Field(new Field("Users.IsApproved")
                        .Validator(Validation.NotEmpty())
                        .Validator(Validation.Numeric())
                        .Options(() => new List<Dictionary<string, object>>{
                            new Dictionary<string, object>{ {"value", "0"}, {"label", "No"} },
                            new Dictionary<string, object>{ {"value", "1"}, {"label", "Yes"} },
                        })
                    )
                    .Field(new Field("Users.IsAdmin")
                        .Validator(Validation.NotEmpty())
                        .Validator(Validation.Numeric())
                        .Options(() => new List<Dictionary<string, object>>{
                            new Dictionary<string, object>{ {"value", "0"}, {"label", "No"} },
                            new Dictionary<string, object>{ {"value", "1"}, {"label", "Yes"} },
                        })
                    )
                    .LeftJoin("Departments", "Departments.DepartmentID", "=", "Users.UserDepartmentID")
                    .Process(request)
                    .Data();
                return Json(response);
            }
        }
    }

Answers

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin

    I've never seen that effect before I'm afraid - I don't know what would be causing it. I've just tried to recreate the problem using this example. The first two fields need a value, so to recreate the problem I left them empty and filled in the other fields and clicked submit. Those values correctly remain when the response from the server comes back and the error messages are shown.

    Can you show me the response from the server to the edit where validation fails?

    I think very likely I will need to be able to access the page to be able to diagnose this one.

    Allan

This discussion has been closed.