Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Feb 10, 2010 @ 09:37
    Jeroen Breuer
    1

    Linked mandatory datatypes

    Hello,

    Is it possible to make a datatype mandatory depending on a different datatype? For example if I have a checkbox datatype if it's checked I'd like other datatypes to become mandatory on the page. Is this possible? Could this be done creating some custom datatypes? Hope someone has experience with this.

    If this is not possible does someone know how I can make a custom datatype mandatory? For example in the axendo form designer you have a Axendo Form Mailer datatype which only becomes mandatory when the checkbox active is checked.

    Hope someone can help me.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 10, 2010 @ 10:52
    Dirk De Grave
    2

    No solution out of the box, but event handlers may be an option by cancelling out a save until all mandatory fields have been filled in (not the most elegant solution, but would work)

    I do like the idea of having options to make custom datatypes mandatory, should be possible to extend current datatype core to include a Valid property, and have the property implemented by custom datatypes.

    Cheers,

    /Dirk

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Feb 10, 2010 @ 17:59
    Jeroen Breuer
    0

    Thanks to Ron Brouwer who build the Axendo Form Designer this problem was solved. He sended me a little sample code how the Axendo Form Designer uses Validation. This is done by creating a custom validator which does the checking. Here is a sample I'm currently using:

    Front-end:

    <table>
        <tr>
            <td class="tdStyle">Verstuur e-mail</td>
            <td><asp:CheckBox ID="CheckEmail" runat="server"/></td>
        </tr>
        <tr>
            <td class="tdStyle">Zender naam</td>
            <td>
                <asp:TextBox ID="TxtSenderName" runat="server" CssClass="txtStyle"></asp:TextBox>
                <asp:CustomValidator ID="CustomValidatorSenderName" runat="server" ControlToValidate="TxtSenderName" Text="Zender naam"  OnServerValidate="ValidateWebControl" ValidateEmptyText="true" Display="Dynamic"></asp:CustomValidator>
            </td>
        </tr>
    </table>

    Back-end:

       /// <summary>
            /// Validate a WebControl with a custom validator. If the checkbox is checked the WebControl is mandatory.
            /// </summary>
            /// <param name="source"></param>
            /// <param name="args"></param>
            protected void ValidateWebControl(object source, ServerValidateEventArgs args)
            {
                args.IsValid = true;
                if (!CheckEmail.Checked)
                {
                    //Skip validation if the checkbox is not checked.
                    return;
                }
    
                if (args.Value == string.Empty)
                {
                    //Set validation to false if no value is entered into the WebControl.
                    args.IsValid = false;
    
                    //Get the CustomValidator.
                    CustomValidator validator = (CustomValidator)source;
    
                    if (!string.IsNullOrEmpty(validator.Text))
                    {
                        //Display the errormessage in the same style Umbraco does.
                        validator.ErrorMessage = validator.Text + " is een verplicht veld.";
                        validator.Text = null;
                    }
                }
            }

    You don't need to use a validation group because Umbraco checkes all validators. With this code you can make webcontrols mandatory if the checkbox is checked.

Please Sign in or register to post replies

Write your reply to:

Draft