Inline Edit not saving data when Server Side Validation entered

Inline Edit not saving data when Server Side Validation entered

ginacargileginacargile Posts: 17Questions: 3Answers: 0
edited September 2016 in DataTables 1.8

I have a table I'm updating inline. I added validation in the controller and it works when adding a new user. It also shows the error in the inline editor if it is invalid- but, it won't save it. No errors are thrown either. I can put the original value in and it acts like it saves that value - but anything different it just sits there with the submit button still visible.
Is there something different I have to do when adding server side validation?

$('#webuser').on('click', 'tbody td:not(:first-child)', function (e) {
    cell = this;
    idx = table.cell(this).index();
    //skip columns
    if (idx.column == 4 || idx.column == 5 || idx.column == 6) {
        return;
    }
        //Skip checkbox controls
        if ($(this).hasClass('dt-body-center')) {
            return;
        }
    
        editor.inline(this, {
            buttons: {
                label: '>', fn: function () {
                    if (idx.column == 3)
                    {
                        var hsh = CreateHash(editor.field('Password').val());
                        editor.field('PasswordHash').val(hsh);
                    }
                this.submit(); 
            } }
        });
});
[HttpGet, HttpPost, Route("api/webuser/{clientname?}")]
public IHttpActionResult WebUser(string clientname)
{
    var request = HttpContext.Current.Request;
    string dbtype = "sqlserver";
    string conn = DBServer.Replace("dbname", clientname);
    DataTables.DtResponse response = new DtResponse();
    try
    {
        using (var db = new Database(dbtype, conn))
        {
            response = new Editor(db, "tblIPadUsers", "ipadUserID")
                .Model<tblIpadUser>()
                .TryCatch(true)
                .Field(new Field("ipadUserID").Set(false))
                .Field(new Field("FullName").Validator(Validation.Required(new ValidationOpts { Message = "This field must be entered." })))
                .Field(new Field("UserName").Validator(Validation.MinMaxLen(8, 50, new ValidationOpts { Message = "UserName must be between 8 and 50 characters long.This field must be entered." }))
                    .Validator(Validation.Required(new ValidationOpts { Message = "This field must be entered." }))
                    .Validator((val, d, host) => Convert.ToString(val).Contains(" ") ? "Username must not contain spaces" : null))
                .Field(new Field("Password").Validator(Validation.MinMaxLen(8, 50, new ValidationOpts { Message = "Password must be between 8 and 50 characters long.This field must be entered." }))
                    .Validator(Validation.Required(new ValidationOpts { Message = "This field must be entered." }))
                    .Validator((val, d, host) => Convert.ToString(val).Contains(" ") ? "Username must not contain spaces" : null))
                .Field(new Field("PasswordHash"))
                .Field(new Field("LockOut"))

                .Process(request)
                .Data();
        }
    }
    catch (Exception ex)
    {
        ErrorSignal.FromCurrentContext().Raise(ex);
    }
    return Json(response);
}

Thanks!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Hi,

    Could you use your browser's developer tools to check what the JSON response from the server is? I suspect there might be another field which it is reporting in error (that isn't shown in inline editing since it assumes the other fields are valid!).

    Regards,
    Allan

  • ginacargileginacargile Posts: 17Questions: 3Answers: 0

    Here is the response after trying to inline edit/save one of the tables. (I picked a smaller one, not doing as much)

    fail

    {"draw":null,"data":[],"recordsTotal":null,"recordsFiltered":null,"error":null,"fieldErrors":[{"name":"ActionOrder","status":"This field must be entered."},{"name":"ActionLevel","status":"This field must be entered."}],"id":null,"meta":{},"options":{},"files":{},"upload":{"id":null}}
    

    When I take out the validation and edit the same field:
    success

    {"draw":null,"data":[{"DT_RowId":"row_4","ID":4,"Action":"Contact Vendortest","ActionOrder":40,"ActionLevel":1}],"recordsTotal":null,"recordsFiltered":null,"error":null,"fieldErrors":[],"id":null,"meta":{},"options":{},"files":{},"upload":{"id":null}}
    
  • ginacargileginacargile Posts: 17Questions: 3Answers: 0
    edited September 2016

    Since it is erroring on the other fields because it isn't submitting all of them at once. How do I modify it to edit one field at a time for the inline edit or send them all- and also have them all for the New record?

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    Could you add:

    submit: 'allIfChanged'
    

    to the object that you pass into the second parameter for inline()? That will cause Editor to submit all fields.

    The other option is to modify the validation options so that they are only applied when these data is actually submitted.

    Allan

  • ginacargileginacargile Posts: 17Questions: 3Answers: 0

    GREAT!! Thank you!

    I tried it as an inline param and it didn't seem to make a difference. But it worked when I put it as a formOption!

     editor = new $.fn.dataTable.Editor( {
                ajax: "/api/action/" + client,
                table: "#actiontable",
                formOptions: {
                    inline: {
                        submit: 'allIfChanged'
                    }
                },
                fields: [
    
This discussion has been closed.