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

Validate in RadDataForm

10 Answers 365 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Andreas Decke
Top achievements
Rank 1
Andreas Decke asked on 06 Apr 2011, 03:22 PM
Hello,

i work with the last internal Build of DataForm (405), my problem, how can i do
validate? I use a RadWindow to edit data from a RadGrid.

DataFormLocation.ValidateItem() is allways false.

<telerik:RadWindow x:Class="SDLManager.Controls.WindowEditLocation"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"                  
    mc:Ignorable="d"
    d:DesignHeight="420" d:DesignWidth="400" >
 
  <Grid x:Name="LayoutRoot" Background="White" Width="350">
    <telerik:RadDataForm Name="DataFormLocation" EditEnded="DataFormLocation_EditEnded" AutoGenerateFields="True" AutoGeneratingField="DataFormLocation_AutoGeneratingField" Margin="4">
    </telerik:RadDataForm>
  </Grid>
</telerik:RadWindow>

using System;
using System.Windows;
using SDLManager.Web;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Data.DataForm;
 
namespace SDLManager.Controls
{
  public partial class WindowEditLocation : RadWindow
  {
 
    #region Propeties
 
    public SDLmedia_Location NewLocation { get; set; }
    public bool IsNew { get; set; }
 
    #endregion
 
 
    public WindowEditLocation()
    {
      InitializeComponent();
    }
 
    public WindowEditLocation(SDLmedia_Location location, string userName, bool isNew)
    {
      InitializeComponent();
      IsNew = isNew;
      if (location != null)
      {
        NewLocation = location;
        NewLocation.CreatedByUser = userName;
        NewLocation.CreatedDate = DateTime.Now;
      }
      else
      {
        NewLocation = new SDLmedia_Location();
        NewLocation.CreatedByUser = userName;
        NewLocation.CreatedDate = DateTime.Now;
        NewLocation.Enabled = true;
      }
 
      DataFormLocation.CurrentItem = NewLocation;
      DataFormLocation.BeginEdit();
    }
 
    private void DataFormLocation_EditEnded(object sender, EditEndedEventArgs e)
    {
      if(e.EditAction == EditAction.Cancel)
      {
        NewLocation = null;
        DataFormLocation.CancelEdit();
        DialogResult = false;
        Close();
      }
      if(e.EditAction == EditAction.Commit)
      {
        if(DataFormLocation.ValidateItem())
        {
          DialogResult = true;
          Close();                             
        }
        else
        {
          DataFormLocation.CancelEdit();
          DialogResult = false;                   
        }
      }
    }
 
    private void DataFormLocation_AutoGeneratingField(object sender, AutoGeneratingFieldEventArgs e)
    {
      e.DataField.Width = 340;
      if(IsNew)
      {
        if(e.PropertyName.Equals("LocationID"))
        {
          e.DataField.Visibility = Visibility.Collapsed;
        }
      }
      else
      {
        if (e.PropertyName.Equals("LocationID"))
        {
          e.DataField.IsEnabled = false;
        }       
      }
 
      if(e.PropertyName.Equals("CreatedDate"))
      {
        e.DataField.IsEnabled = false;
      }
      else if (e.PropertyName.Equals("CreatedByUser"))
      {
        e.DataField.IsEnabled = false;
      }
 
    }
 
  }
}


10 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 11 Apr 2011, 03:40 PM
Hi Richard ,

It seems you have hit a bug in RadDataForm . Thanks for reporting the issue. It has been addressed immediately and the fix will be present in the latest internal build . The build will be available for download in your account later today or tomorrow.

One more time thank you for the feedback ! I am updating your Telerik points.

Regards,
Pavel Pavlov
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
Nathaniel
Top achievements
Rank 1
answered on 15 Apr 2011, 04:48 PM
OK, I just downloaded the latest build and ValidateItem() is always true!

I'm testing with a RadDomainDataSource using metadata annotations to set a required field as required.  When I delete the data in the field and submit, calling ValidateItem() in either the EditEnding or EditEnded events returns true, even though the RadDataForm highlights the missing field as needing to be required.

Thoughts?

Edited to add:

It works correctly if I AutoGenerate the columns, but not when I declare which columns I want to use.  Perhaps I'm missing an attribute in my XAML?
0
Pavel Pavlov
Telerik team
answered on 21 Apr 2011, 12:46 PM
Hi ,

If I understand the problem correctly, then setting the  ValidatesOnExceptions attribute of the DataMember binding to true should do the trick .

For example :
<telerik:DataFormDataField DataMemberBinding="{Binding MyProperty, Mode=TwoWay,ValidatesOnExceptions=True}"
Regards,
Pavel Pavlov
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
Pavel Pavlov
Telerik team
answered on 21 Apr 2011, 12:59 PM
Hi,

Just a small addition , actually there are two attributes(ValidatesOnExceptions and NotifyOnValidationError )  to set here in order to have validation on filed level and validation in summary :
<telerik:DataFormDataField DataMemberBinding="{Binding MtProperty, Mode=TwoWay,ValidatesOnExceptions=True, NotifyOnValidationError=True}"


Regards,
Pavel Pavlov
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
john
Top achievements
Rank 1
answered on 21 Apr 2011, 11:51 PM
Thanks Guys, I just ran into this problem 2 days ago and found this post.  I was waiting for the update since it was critical to my program.  Now the only issue I have is an addnew Item.  For some reason unless a field with validation on it is changed, the form responds as through it is valid.

The code myRadDataForm.ValidateItem() returns true even though a field is blank.  Now if I type a character in bound text box and then delete it and try to save, it comes back with the validation error "Required Field".  So it's only responding if I change a value for a required field, even though it's final value is technically null again.

Can you help me get the validator to check even if nothing has been typed in by the user on a new record?

Oh, it might help to let you know that i'm using myRadDataForm.AddNewItem() function.  Would it make a difference if I manually created an entity object and added it to the currentItem in the form.

Thanks,
Luke
0
Pavel Pavlov
Telerik team
answered on 22 Apr 2011, 01:46 PM
Hello john,

We have recently found this inconsistency with the required attribute. We are working on solving the issue.  Meanwhile there is a workaround I may offer.
You may handle the Validating event. Inside the event handler explicitly check if the required fields are blank , and id so set e.cancel = true. This will prevent the form from exiting edit mode , when required fields are not entered.

Regards,
Pavel Pavlov
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
William
Top achievements
Rank 1
answered on 04 May 2011, 05:07 PM
Pavel,
    Per your suggestion of checking the required fields, is there anyway to interate throught the fields on the dataform to check if they are required?  I cannot find a collection of DataFormDataFields. 

When autogenerating my dataform, I am setting some datafields as required, and want to find those during validation.

Thanks,
    Bill
0
Ivan Ivanov
Telerik team
answered on 05 May 2011, 02:28 PM
Hi William,

You might like to try this approach:
private void testDataForm_ValidatingItem(object sender, CancelEventArgs e)
       {
           foreach (var pi in typeof(Employee).GetProperties().Where(p => p.GetCustomAttributes(true).Contains(new RequiredAttribute())))
           {
               if (pi.GetValue(testDataForm.CurrentItem, null) == null)
                   e.Cancel = true;
           }
       }
where Employee is my business object. In this way you can check the properties, which have Reuires attribute set.

Kind regards,
Ivan Ivanov
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
William
Top achievements
Rank 1
answered on 06 May 2011, 06:21 PM
Ivan,
    When I the data form is being auto generate, I am looking an some other factors to determine if the datafield is required and with in the autogeneratingfield event of the data form I am setting e.DataField.IsRequired to true.  I do not believe this makes it back to my business object, as the code that you have suggested does not pick up these datafields that I set to required.

During AutoGenerate, I can produce a list of fields that were flagged as required and using logic similar to yours I can identify which fields were flagged as required and set e.cancel to true in the validateitems event, but I cannot figure out how to mark the datafield in the dataform as having a validation issue. 

In grid validation, I can call markcell and place text in the validation error. I cannot seem to do this in the dataform.

Thanks,
    Dave
0
William
Top achievements
Rank 1
answered on 06 May 2011, 07:12 PM
Ivan,
    Never mind that last post...i figured out that you can add to the validation errors collection of the business object and that will display.

It would just be very helpful if things worked consistantly...I would have expected that the validation for the dataform would have worked much like the grid since they are typically used hand in hand.

Bill
Tags
DataForm
Asked by
Andreas Decke
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Nathaniel
Top achievements
Rank 1
john
Top achievements
Rank 1
William
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Share this question
or