More in this section

Forums / Developing with Sitefinity / Enable or disable RequiredFieldValidator based upon form dropdown list selection

Enable or disable RequiredFieldValidator based upon form dropdown list selection

4 posts, 0 answered
  1. Saroj Rout
    Saroj Rout avatar
    31 posts
    Registered:
    14 Apr 2011
    28 Dec 2012
    Link to this post
    Hi 

    I have a form that has a drop down list with two options: Email & Phone

    Depending upon which of these is selected, I would like to disable/enable required field validaiton  for text boxes textboxes.

    How would I go about doing this?

    Thanks
    Saroj
  2. Lilia Messechkova
    Lilia Messechkova avatar
    42 posts
    Registered:
    13 Jul 2016
    02 Jan 2013
    Link to this post
    Hello Saroj, 

     

    It is possible to achieve similar solution to this. There are two ways to achieve it. First one is to change the validation group of the text box each time you change the selection of the dropdown list. Which means to reload the page on each change. The other way is much more optimized you just check which element of the drop down is selected and validate the textbox according to it.

    In order to do it you need to create a custom Form that inherits from FormsControl.

    Handle BeforeFormSave event and check form`s fields. This event is cancelable and if your requirements are not satisfied you can cancel form submission and display an error message.

    A sample code for this:

    void MyForm_BeforeFormSave(object sender, System.ComponentModel.CancelEventArgs e)
            {
                var isRequired = false;
                var isEmpty = false;
                var fieldControls = ((MyForm)(sender)).FieldControls;
                foreach (var fieldControl in fieldControls)
                {
                    if (fieldControl is FormDropDownList)
                    {
                        var choices = (FormDropDownList)fieldControl;
     
                        if (choices.Value.ToString() == "Phone")
                        {
                           isRequired = true;
                        }
                    }
                    if (fieldControl is FormTextBox)
                    {
                        var textBox = (FormTextBox)fieldControl;
                        string text = textBox.Value.ToString();
     
                        if (text.Length == 0)
                        {
                            isEmpty = true;
                        }
                    }
                }
     
                if(isEmpty && isRequired)
                {
                    ((MyForm)sender).ErrorsPanel.Visible = true;
                    ((MyForm)sender).ErrorsPanel.GroupingText = "This is a required field!";
                    e.Cancel = true;
                }
               
            }

    My form declaration contains TextBox, Dropdown list and submit button.



    All the best,
    Lilia Messechkova
    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. Saroj Rout
    Saroj Rout avatar
    31 posts
    Registered:
    14 Apr 2011
    12 Jan 2013 in reply to Lilia Messechkova
    Link to this post
    Hi
    how can i resgister or call this event void MyForm_BeforeFormSave(object sender,
    System.ComponentModel.CancelEventArgs e) in the custom form. Is that event availabele BeforeFormSave. Please tell me how to call .
  4. Venkata Koppaka
    Venkata Koppaka avatar
    67 posts
    Registered:
    24 Sep 2012
    15 Jan 2013
    Link to this post
    Hello Saroj,

     To do so, make a new class which you have to inherit FormsControl. Like below -

    public class MyForm : FormsControl

    And then in the InitializeControls method you can register the call to the event handler. Like below -
    protected override void InitializeControls(GenericContainer container)
            {
                if (!SystemManager.IsDesignMode)
                {
                    base.InitializeControls(container);
                    this.BeforeFormSave += new EventHandler<CancelEventArgs>(MyForm_BeforeFormSave);
                }
          }

    Greetings,
    Venkata Koppaka
    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