This is a migrated thread and some comments may be shown as answers.

RadComboBox Validation with Filter Text Entered

4 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 23 Apr 2013, 05:51 PM
I have a RadComboBox in an edit template of a grid with a RequiredFieldValidator. The combo box is set to to Filter="Contains", with an empty message of "--Select--". The RequiredFieldValidator will stop them from submitting if they have not entered any text, however it will not stop them if they have not selected at item. If they try filtering by a value that is not in the list and hit save without selecting it will not prevent this action. 

I tried using a CompareValidator but I can't get it to fire when nothing is selected. Also I believe this would only compare the text they have entered as it grabs the Text field and not the Value field.

Is there a Validator I can use to verify they have actually selected an item when allowing filtering, or will I need to write some javascript validation for this?

<telerik:GridTemplateColumn Display="false" UniqueName="colClass" HeaderText="Class">
  <ItemTemplate>
    <asp:Label runat="server" ID="lblClass" Text=<%# Eval("Class") %> ></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
    <telerik:RadComboBox runat="server" ID="cbbClass" DataSourceID="classesDS" OnSelectedIndexChanged="cbbClass_SelectedIndexChanged" AutoPostBack="true" EmptyMessage="--Select a Class--" Filter="Contains" DataValueField="Class_ID" DataTextField="Class" SelectedValue = <%# Eval("Class_ID") %> ></telerik:RadComboBox>
    <asp:RequiredFieldValidator runat="server" ID="cbbClassValidator" ValidationGroup="SessionSave" ErrorMessage="You must select a Class!" Text="*" Display="Dynamic" ControlToValidate="cbbClass" ></asp:RequiredFieldValidator>
    <asp:CompareValidator runat="server" ID="cbbClassValidator2" ValueToCompare=""
                    Operator="NotEqual" ControlToValidate="cbbClass" Display="Dynamic" ErrorMessage="You must select a Class2!" Text="*" ValidationGroup="SessionSave" />
  </EditItemTemplate>
</telerik:GridTemplateColumn>

Thanks,
James

4 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 26 Apr 2013, 03:45 PM
Hi James,

I am afraid there isn't such Validator and should be manually created on the client.

I hope this information helps.

Kind regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
James
Top achievements
Rank 1
answered on 26 Apr 2013, 05:03 PM
Hi Kostadan,

I've found a way to do it via javascript, it requires a CustomValidator with a ClientValidationFunction. 

function operatorCheck(sender, args) {
  var comboID = (sender.id).replace("cbbOperatorValidator", "cbbOperator");
  var combo = $find(comboID);
  if (combo.get_selectedItem() == null) {
    args.IsValid = false;
  } else {
    args.IsValid = true;
  }
  return;   
}

<telerik:GridTemplateColumn HeaderStyle-Width="40%" ItemStyle-Width="40%" HeaderText="Operator">
  <ItemTemplate>
    <asp:Label runat="server" ID="lblOperatorName" Text=<%# Eval("Name") %> ></asp:Label>
  </ItemTemplate>
  <EditItemTemplate>
    <telerik:RadComboBox Width="100%" runat="server" ID="cbbOperator" SelectedValue=<%# Eval("Session_Operator_User_ID") %> DataTextField="Name" Filter="Contains" EmptyMessage="--Search Operators--" DataValueField="User_ID" DataSourceID="opertatorsDS" ></telerik:RadComboBox>
    <asp:CustomValidator runat="server" ID="cbbOperatorValidator" Text="You must select an Operator!" Display="Dynamic" ValidationGroup="operatorSave" ClientValidationFunction="operatorCheck" ></asp:CustomValidator>
  </EditItemTemplate>
</telerik:GridTemplateColumn>


This would be a bit more simple if the combobox was outside of the radgrid, then you could get the client ID of the RadComboBox directly instead of needing to the replace function to get the proper rows ID prefix.

If there is an easier or built in way to access the edited rows other controls please let me know.

Thanks,
James
0
Kostadin
Telerik team
answered on 01 May 2013, 10:54 AM
Hi James,

You could use the following approach to get the currently edited row.
var editRow = $find('RadGrid1').get_masterTableView().get_editItems()[0];

Then you could access them by the column unique name:
$find("RadGrid1").get_masterTableView().getCellByColumnUniqueName(editRow, "ColumnUniqueName")

I hope this helps.

Greetings,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Steve
Top achievements
Rank 1
answered on 26 Nov 2014, 03:29 PM

Since I'm using a FormTemplate, I was able to add a custom validator to the form.

protected void CustomValidatorCombo_ServerValidate(object source, ServerValidateEventArgs args)
        {
            try
            {
                var validator = (source as CustomValidator);
                string controlToValidate = validator.ControlToValidate;
                RadComboBox combo = validator.NamingContainer.FindControl(controlToValidate) as RadComboBox;
 
                if (combo.Visible)
                {
                    args.IsValid = combo.Text.Equals(combo.SelectedValue);
                }
                else
                {
                    args.IsValid = true;
                }
            }
            catch (Exception ex)
            {
                LogManager.LogExceptionAndThrow(this.SessionUser, ex, this);
            }
        }

Hope this helps.

 

 

 

 

Hope this helps.

 

Tags
Grid
Asked by
James
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
James
Top achievements
Rank 1
Steve
Top achievements
Rank 1
Share this question
or