1

Cannot get into a custom validator I built on a Sitecore 8 MVC Web Form for Marketers form.

Here are my steps I used to build the validators:

1) I created a C# app (.NET 4.5), and build the dll for file validations. Built, copied the .DLL to the Bin Directory. Works fine in a Sitecore 6.6 site we have, sitecore 8.0 site it does not work.

Here is the validation code (I modified to set args.IsValid to false for testing).

namespace SitecoreUtilities.UploadFile
{
    public class RestrictSize : FormCustomValidator
    {
        public int MaxFileSize
        {
            get
            {
                int maxSize;
                if (int.TryParse(base.classAttributes["MaxFileSize"], out maxSize))
                {
                    return maxSize;
                }
                return 0;
            }
            set
            {
                base.classAttributes["MaxFileSize"] = value.ToString();
            }
        }

        public RestrictSize()
        {
            this.ServerValidate += this.OnSizeValidate;
        }

        private void OnSizeValidate(object source, ServerValidateEventArgs args)
        {
            FileUpload fileUpload = FindControl(ControlToValidate) as FileUpload;
            if (!fileUpload.HasFile)
            {
                args.IsValid=true;
                return;
            }
            args.IsValid = fileUpload.PostedFile.ContentLength <= MaxFileSize;
            args.IsValid = false;  // TESTING to confirm getting in here
        }
    }
}

2) created new validator(s):
Location: sitecore/system/Modules/Web Forms for Marketers/Settings/Validation/FileUpload/AllowedFileSize

  • assembly: SitecoreUtilities
  • Class: SitecoreUtilities.UploadFile.RestrictSize
  • MVC Type: blank
  • Validator Display: Dynamic
  • Enable Client Script: checked
  • Parameters: empty as I put on the actual item
  • Localized Parameters: empty
  • Inner Control: checked

3) Added Validator to the Field Types / Simple Types / File Upload Item

enter image description here

4) Added the file upload to the form field:

enter image description here

5) Saved, published.. cannot get to work on the site.

BTW, I set up a local.test.org site in IIS 7.5 on my machine and went into the project in visual studio and attached to W3Wp but it did not get into the application.

If I missed a step on this deploy please advise.

Update 1: Added MVC to the title
Update 2: Added the C# code per code difference between my code and https://doc.sitecore.net/web_forms_for_marketers/working_with_actions_and_validations/validations/walkthrough_create_and_assign_a_custom_fieldtype_validator.

4
  • Is your form a MVC form or not?
    – Gatogordo
    Jul 9, 2016 at 7:48
  • If your code works in v6, it's likely not to work in v8 as some things changed between those version in wffm... Can you check doc.sitecore.net/web_forms_for_marketers/… and see whether it conforms with your code and setup?
    – Gatogordo
    Jul 9, 2016 at 7:52
  • Thx Gatogordo for responding. 1) yes MVC (I updated the subject). if inquiring as I left that field blank in the validator, I just modeled after other validators as it is empty. 2) Thanks for sending that link. I just made some adjustments to the code, even forced args.IsValid to false, still not hitting edit.
    – DanO
    Jul 9, 2016 at 12:32
  • For MVC forms you should read this: ggullentops.blogspot.be/2016/05/… It works a bit different compared to webforms..
    – Gatogordo
    Jul 9, 2016 at 13:38

1 Answer 1

2

For WFFM MVC forms you need to work with validation attributes. I wrote blog post with an example here. The example used is also for a file upload, so it should be relevant. It comes down to creating an attribute, inherited from DynamicValidationBase. You do need to create a new field as well, just to add the attributes - you don't need to write new code there.

The code in the blog is for Sitecore 8.1. There might be some small differences with 8.0.

7
  • Thx Gatogordo, this seems like the solution will work for 8.1 however I do not see Sitecore.Forms.Mvc.ViewModels.Fields in the Sitecore.Forms.Mvc 8.0.0 .dll . When I look @ the logs in Sitecore I see this message: Message: Could not load type 'Sitecore.Forms.Mvc.ViewModels.Fields.FileUploadField' from assembly 'Sitecore.Forms.Mvc, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null'.
    – DanO
    Jul 11, 2016 at 21:33
  • In 8.0 they are located in Sitecore.Forms.Mvc.Models.Fields
    – Gatogordo
    Jul 12, 2016 at 7:15
  • Any idea what namespace IViewModel is in. receiving The type or namespace name 'IViewModel' could not be found (are you missing a using directive or an assembly reference?).. Also, if you know of the best method of finding this please share.
    – DanO
    Jul 12, 2016 at 13:20
  • Use any decompiler.. I use dotPeek but there are others that will do the job as well.
    – Gatogordo
    Jul 12, 2016 at 13:30
  • 1
    8.1 is more stable - if you can upgrade that would be a good choice
    – Gatogordo
    Jul 12, 2016 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.