Field required Conditionally

2 posts, 0 answers
  1. Jed
    Jed avatar
    1 posts
    Member since:
    May 2013

    Posted 30 May 2013 Link to this post

    Hello,

    I am working on a requirement where I have four fields that are required when a check box on the form is checked.


    These fields are read only until the check box is checked, at which time I would like them to be required. The main problem is that I need validation to occur on fields other than the one that has just been interacted with.

    I have tried a few different attempts but the ones that seem to function either partially work or require extra steps from the user to clear the validation error before proceeding.

    Using the item level attribute-based validation requires marking a fields as required in code before hand and whether the field is set to Read Only, or not visible on the form, if the field does not have information then it causes an error.

    Using the validation properties for a custom field in the View Model gets me the Conditional requirement, but I have not been able to find a good way to make this work. Basically I throw the ValidationException on the check box field which disabled the OK button and displays the validation errors. After populating the fields the check box must be cleared an reset. I tried to work around this by rechecking the box in the setter for the fields, but that clears the validation error from the check box. In this case, if I add the Validation to the bindings for the fields it will show the errors there, but it will not clear the error on each field as I update them. Having each field fire the validation of the others lead to an infinite loop of each field starting each other field and so on.

    In the EndEdit method for the IEditableObject, I placed the logic for the validation and I have not found a good way to remain in the edit state of the form if the item does not meet the validation requirements.

    I tried using the ValidateProperty with a ValidationContext, but this caused mainpage.xaml to start throwing exceptions when Initialized.

    This is what I have attempted. I do have to say, that revisiting the documentation for what I have tried, I noticed that I have been skipping over the ValidatingItem event handler. This does appear to be promising, but I don't see a way of remaining in the edit mode of the form, or triggering the Validation errors if there the item does not meet the criteria.

    Any help would be appreciated.



    Update:

    I further explored using the ValidatingItem event handler and it has something that will suite my purposes.
    private void DataSourceForm_ValidatingItem(object sender, System.ComponentModel.CancelEventArgs e)
    {
        bool isValid = false;
        //Validation logic
     
        if (!isValid)
        {
            e.Cancel = true;
        }
    }
    This allows me to remain in the Edit state of the form. I am still curious if there is something that will allow me to add Validation Errors on the form when it fails the Validation logic. Currently I'm planning on having a popup message listing the problem.
  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 04 Jun 2013 Link to this post

    Hi Jed,

     Basically, a good way to implement cross-field validation is the DataAnnotations CustomValidationAttribute. Would it be a viable option for you, to use it with RadDataForm's Item level validation? I will be glad to provide additional assistance in case it is needed.

    Regards,
    Ivan Ivanov
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
Back to Top