Enable or disable RequiredFieldValidator based upon form dr

Posted by Community Admin on 04-Aug-2018 11:49

Enable or disable RequiredFieldValidator based upon form dropdown list selection

All Replies

Posted by Community Admin on 28-Dec-2012 00:00

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

Posted by Community Admin on 02-Jan-2013 00:00

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

Posted by Community Admin on 12-Jan-2013 00:00

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 .

Posted by Community Admin on 15-Jan-2013 00:00

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

This thread is closed