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

CompareValidator is not working on update

7 Answers 181 Views
Grid
This is a migrated thread and some comments may be shown as answers.
FreshOne
Top achievements
Rank 1
FreshOne asked on 10 Feb 2010, 02:17 PM
hi all,

i have a GridDateTimeColumn in a grid, i added compare validators for it in the code like the example here http://www.telerik.com/help/aspnet-ajax/grdvalidation.html, one to check the type, and another to compare it with Date.Now

the problem is that the validator that checks the type works only on insert but not on update! i can't find any logical reason for that..here is my code

<telerik:GridDateTimeColumn HeaderText="Start Date" SortExpression="startDate" UniqueName="startDate" AutoPostBackOnFilter="true" DataField="startDate" 
    FilterControlWidth="110px" PickerType="DatePicker" DataFormatString="{0:d/M/yyyy}" DataType="System.DateTime">  
</telerik:GridDateTimeColumn>   

    Private Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated  
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then 
            Dim item As GridEditableItem = CType(e.Item, GridEditableItem)  
 
            Dim startColumnEditor As GridDateTimeColumnEditor = TryCast(DirectCast(e.Item, GridEditableItem).EditManager.GetColumnEditor("startDate"), GridDateTimeColumnEditor)  
            Dim startCell As TableCell = DirectCast(startColumnEditor.PickerControl.Parent, TableCell)  
 
            Dim compareValidator1 As New CompareValidator()  
            compareValidator1.ID = "compareValidator1" 
            compareValidator1.ControlToValidate = startColumnEditor.PickerControl.ID  
            compareValidator1.[Operator] = ValidationCompareOperator.DataTypeCheck  
            compareValidator1.Type = ValidationDataType.[Date]  
            compareValidator1.ErrorMessage = "Invalid date. " 
            compareValidator1.Display = ValidatorDisplay.Dynamic  
            startCell.Controls.Add(compareValidator1)  
 
            Dim compareValidator3 As New CompareValidator()  
            compareValidator3.ID = "compareValidator3" 
            compareValidator3.ControlToValidate = startColumnEditor.PickerControl.ID  
            compareValidator3.[Operator] = ValidationCompareOperator.GreaterThanEqual  
            compareValidator3.Type = ValidationDataType.[Date]  
            compareValidator3.ErrorMessage = "Date can't be earlier than today" 
            compareValidator3.ValueToCompare = DateTime.Now.ToShortDateString()  
            compareValidator3.Display = ValidatorDisplay.Dynamic  
            startCell.Controls.Add(compareValidator3)  
        End If 
    End Sub 
 
    Private Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand  
        If e.CommandName = "Update" Then 
            If Page.IsValid Then
                e.Item.Edit = False 
            End If 
        ElseIf e.CommandName = "PerformInsert" Then 
            If Page.IsValid Then 
                e.Canceled = True 
                e.Item.OwnerTableView.IsItemInserted = False 
                e.Item.OwnerTableView.Rebind()  
            End If 
        ElseIf e.CommandName = "Delete" Then 
 
        End If 
    End Sub 

"compareValidator1" is the one that is not working on update, when i enter string characters in the date field and click on update button it performs updating process, it considers the page as valid

Any ideas?? wht am i doing wrong??

thnx in advance

7 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 11 Feb 2010, 05:14 PM
Hi,

Attached is a small sample where the validator works all right for the date-time column. Please, take a look, try to reproduce the problem on it and let us know of the result.

Reagrds,
Tsvetoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
FreshOne
Top achievements
Rank 1
answered on 14 Feb 2010, 08:32 AM
hi Tsvetoslav,
thnx for ur reply..i checked ur sample, i tried it but i still have the same problem..the validator works just fine unless its operator is 

DataTypeCheck, it doesn't work on update, it works only on insert! i know it sounds unlogical but that's what's happening

here is my code after the changes

    Private Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated  
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then 
            Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)  
 
            Dim startEditor As GridDateTimeColumnEditor = TryCast(item.EditManager.GetColumnEditor("startDate"), GridDateTimeColumnEditor)  
            Dim startCell As TableCell = DirectCast(startEditor.PickerControl.Parent, TableCell)  
            Dim startDatePicker As Control = item("startDate").Controls(0)  
 
            Dim compareValidator1 As New CompareValidator()  
            compareValidator1.ControlToValidate = startDatePicker.ID  
            compareValidator1.[Operator] = ValidationCompareOperator.DataTypeCheck  
            compareValidator1.Type = ValidationDataType.[Date]  
            compareValidator1.ErrorMessage = "Invalid date. " 
            compareValidator1.Display = ValidatorDisplay.Dynamic  
            startCell.Controls.Add(compareValidator1)  
 
            Dim compareValidator3 As New CompareValidator()  
            compareValidator3.ControlToValidate = startDatePicker.ID  
            compareValidator3.[Operator] = ValidationCompareOperator.GreaterThanEqual  
            compareValidator3.Type = ValidationDataType.[Date]  
            compareValidator3.ErrorMessage = "Date can't be earlier than today" 
            compareValidator3.ValueToCompare = DateTime.Now.ToShortDateString()  
            compareValidator3.Display = ValidatorDisplay.Dynamic  
            startCell.Controls.Add(compareValidator3)  
 
            Dim endEditor As GridDateTimeColumnEditor = TryCast(item.EditManager.GetColumnEditor("endDate"), GridDateTimeColumnEditor)  
            Dim endCell As TableCell = DirectCast(endEditor.PickerControl.Parent, TableCell)  
            Dim endDatePicker As Control = item("endDate").Controls(0)  
 
            Dim compareValidator2 As New CompareValidator()  
            compareValidator2.ControlToValidate = endDatePicker.ID  
            compareValidator2.[Operator] = ValidationCompareOperator.GreaterThanEqual  
            compareValidator2.Type = ValidationDataType.[Date]  
            compareValidator2.ErrorMessage = "End date can't be earlier than today or earlier than start date" 
            compareValidator2.ValueToCompare = DateTime.Now.ToShortDateString()  
            compareValidator2.ControlToCompare = startDatePicker.ID  
            compareValidator2.Display = ValidatorDisplay.Dynamic  
            endCell.Controls.Add(compareValidator2)  
        End If 
    End Sub 

and when i put the following it creates the validators twice, and the problem remains
    Private Sub Page_Init(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Init  
        AddHandler RadGrid1.ItemCreated, AddressOf RadGrid1_ItemCreated  
    End Sub 

any ideas plz??
0
Tsvetoslav
Telerik team
answered on 17 Feb 2010, 03:47 PM
Hi,

The problem is that you are comparing the types of what is entered and what is expected. And when you enter an invalid date into the picker, the old one is retained as the one for the SelectedValue property which is of the right type - System.DateTime. This allows the validation to pass. 

I hope this helps.

Regards,
Tsvetoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
FreshOne
Top achievements
Rank 1
answered on 18 Feb 2010, 07:27 AM
hi Tsvetoslav, thnx for ur reply
then how can i make the validator sees the new value and validate it instead of the old one?
or is there another approach to do that?
0
Tsvetoslav
Telerik team
answered on 18 Feb 2010, 11:27 AM
Hello,

You can just remove the type comparison for the validator so that it compare the values of the controls (i.e. remove the follow lines:

compareValidator1.[Operator] = ValidationCompareOperator.DataTypeCheck 
compareValidator1.Type = ValidationDataType.[Date]

The same goes for the other two validators.

Regards,
Tsvetoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
FreshOne
Top achievements
Rank 1
answered on 18 Feb 2010, 03:03 PM
hi Tsvetoslav, thnx for ur reply
if i removed those lines, how the validator will know how to validate the field according to wht rule??
when i tried it, the validator became invalid all the time even when i entered a correct date
0
Tsvetoslav
Telerik team
answered on 23 Feb 2010, 10:25 AM
Hi FreshOne,

The validator control is capable of internally deferring the type of the controls to validate. However, I am not sure what exactly the problem on your side is. Did you get the chance to run the sample I sent you before as it is working all right.

If the issue persists, please, open up a formal support ticket and send a small runnable sample for a closer inpsection.

Regards,
Tsvetoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
FreshOne
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
FreshOne
Top achievements
Rank 1
Share this question
or