More in this section

Forums / Developing with Sitefinity / Validation between different definitions in custom module

Validation between different definitions in custom module

4 posts, 0 answered
  1. Kronos
    Kronos avatar
    17 posts
    Registered:
    08 Apr 2011
    30 Jun 2011
    Link to this post
    Hello

    I am developing custom module for Sitefinity 4, and creating back-end edit controls with definitions.

    I have 2 fields - Quote and QuoteName:
    var quoteField = new HtmlFieldElement(container)
                {
                    ID = "quoteFieldControl",
                    Title = "Quote",
                    DataFieldName = (displayMode == FieldDisplayMode.Write) ? "Quote.PersistedValue" : "Quote",
                    DisplayMode = displayMode,
                    CssClass = "sfFormSeparator sfContentField",
                    WrapperTag = HtmlTextWriterTag.Li,
                    EditorContentFilters = Telerik.Web.UI.EditorFilters.DefaultFilters,
                    EditorStripFormattingOptions = (EditorStripFormattingOptions?)(EditorStripFormattingOptions.MSWord | EditorStripFormattingOptions.Css | EditorStripFormattingOptions.Font | EditorStripFormattingOptions.Span | EditorStripFormattingOptions.ConvertWordLists)
                };
    container.Add(quoteField);
     
    var quoteNameField = new TextFieldDefinitionElement(container)
                {
                    ID = "quoteNameFieldControl",
                    DataFieldName = (displayMode == FieldDisplayMode.Write) ? "QuoteName.PersistedValue" : "QuoteName",
                    DisplayMode = displayMode,
                    Title = "Quote Name",
                    CssClass = "sfTitleField",
                    WrapperTag = HtmlTextWriterTag.Li,
                };
    container.Add(quoteNameField);

    I need to create following validation: Quote is required if QuoteName value != null, and QuoteName is required if Quote value != null. If they are both null all ok.

    Please tell what is the best way to implement this validation behaviour? I am thinking about custom field where both controls will be presented in UI template, but as far as I know field can be mapped to only single data field.

    Thank you
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Sep 2017
    04 Jul 2011
    Link to this post
    Hi Kronos,

    The validation in a custom module is considered by design you can take a look at Productsdefinitions.cs in the products module.
    var quoteNameField = new TextFieldDefinitionElement(container)
                {
                    ID = "quoteNameFieldControl",
                    DataFieldName = (displayMode == FieldDisplayMode.Write) ? "QuoteName.PersistedValue" : "QuoteName",
                    DisplayMode = displayMode,
                    Title = "Quote Name",
                    CssClass = "sfTitleField",
                    WrapperTag = HtmlTextWriterTag.Li,
                };
    quoteNameField.ValidatorConfig = new ValidatorDefinitionElement(quantityField)
                {
                    Required = true,
                    MessageCssClass = "sfError",
                    RequiredViolationMessage = Res.Get<ProductsResources>().TitleCannotBeEmpty,
                    RegularExpression = "^[0-9]+[.]*[0-9]*$",
                    RegularExpressionViolationMessage = Res.Get<ProductsResources>().TheQuantityMustBeAPositiveNumber
                };
                mainSection.Fields.Add(quoteNameField);
    You define the text field element and under it you can add validation logic and then add this textfield to a section in the module.

    Best wishes,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Kronos
    Kronos avatar
    17 posts
    Registered:
    08 Apr 2011
    04 Jul 2011
    Link to this post
    Thanks, yes, I know about SINGLE field validation with ValidatorConfig, it's pretty good by the way. But my goal is to create following scenario where TWO fields participated (Quote and QuoteName):

    Quote is required if QuoteName value != null, and QuoteName is required if Quote value != null. If they are both null all ok.

    e.g. Quote validation state depend on QuoteName property also. Does it make sence? I know it's hard task and ask you where should I dig, I don't need a code, just an idea how can it be implemented on current Sitefinity infrastructure
  4. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Sep 2017
    06 Jul 2011
    Link to this post
    Hello Kronos,

    This should be done with the single validation on each Element. The elements should have a condition (if). If the condition is true validation logic will apply if it is not true it will not validate the elements ( if both are null).

    All the best,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
4 posts, 0 answered