Cross Column Validation - C#

Cross Column Validation - C#

a2ztecha2ztech Posts: 19Questions: 2Answers: 1
edited July 2016 in Free community support

Problem:
"data" dictionary only has "column" value for the current column as against to the current "row". I am trying to compare tow dates here. Can you please suggest on how to go about it?

Controller Snippet in C#:

 .Field(new Field("Session.SessionStart")
                      .Validator(Validation.DateFormat(
                            "h:mm tt",
                            new ValidationOpts { Message = "Please enter time in the format h:mm A" }
                            ))
                        .GetFormatter(Format.DateTime("hh:mm", "h:mm tt"))
                        .SetFormatter(Format.DateTime("h:mm tt", "hh:mm"))
                    )
.Field(new Field("Session.SessionEnd")
                       .Validator(Validation.DateFormat(
                            "h:mm tt",
                            new ValidationOpts { Message = "Please enter time in the format h:mm A" }
                            ))
                        .GetFormatter(Format.DateTime("hh:mm", "h:mm tt"))
                        .SetFormatter(Format.DateTime("h:mm tt", "hh:mm"))
                        .Validator((val,data,opts) => DateTime.Compare((DateTime)val,(DateTime)(data["Session"]["SessionStart"])) == 0 ? "false" : null)

Headers in the browser developer tools:

action:edit
data[4][Session][SessionEnd]:1:58 AM

reference documents:
https://datatables.net/forums/discussion/33753/cross-field-validation-how-to-do-it-in-datatables-editor-two-examples
https://editor.datatables.net/manual/net/validation (#Custom validators Section)

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Replies

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    Hi,

    I think your C# aspect is spot on here - you are using the second parameter passed into the validator which will give the whole of the data set submitted.

    The issue is that Editor is only submitting the changed value(s). My guess is that you are using inline editing - if so, you can instruct Editor to send all parameters using the submit option of the form-options object - e.g.:

    editor.inline( this, {
      submit: 'allIfChanged'
    } );
    

    More details, in addition to the above reference docs, are available in the 1.5. upgrade notes.

    Allan

  • a2ztecha2ztech Posts: 19Questions: 2Answers: 1

    that helps. thanks @allan

  • a2ztecha2ztech Posts: 19Questions: 2Answers: 1

    Hi Allan,
    Please refer to the code example above (using .NET in-line editing).
    Few of my records has start date/end date as NULL in the database.
    When the grid loads, instead of showing the start date/end date columns blank I would like to show some default values (eg. 12:00 AM).
    I tried using GetFormatter but in vain. Any example will help here.
    Thanks!

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    A GetFormatter is indeed the way to do it. You would check to see if the value read is null, and if so return the default string. Otherwise, you would need to format the date using the .NET date formatter options (you can only use a single get formatter for each field, so you wouldn't be able to use the built in date time formatter as well as your custom formatter).

    Happy to provide a custom example under the support options.

    Allan

  • a2ztecha2ztech Posts: 19Questions: 2Answers: 1

    thanks Allan. I managed to pull it of.

This discussion has been closed.