2

I'm using Sharepoint 2010 .. with a custom field in visual studio 2010.

I created a custom field. This particular one is a datetime field ("Termination Date"). I want it to fail validation if it is blank and another field ( "Contract Terminates" is equal to yes ).

So I had previously did this with a calculated field. And that works but it puts the validation error at the top of the edit form, not next to the "Termination Date" field where I want it.. like it would normally be if the field failed validation using GetValidatedString in a custom field.

So because it's in the wrong place, I made a custom field. But because the date is blank, it never hits GetValidatedString method. Am I missing something? is there another way to have it fail validation and be next to the 'Termination Date' field if the 'Termination Date' field is blank?

I'm tried using an event receiver solution also.. the problem there is that it would also put the error message on the top.. not next to the Termination Date field.

Suggestions?

1 Answers1

2

For custom field you could override FieldRenderingControl, write your own FieldControl. If you don't use this custom field in Whereabouts list you could inherited your fieldcontrol from DateTimeField and override Validate method e.g:

public override void Validate()  
{
    base.Validate();
    if (IsValid)
    {
        if (!(your validation))
        {
            IsValid = false;
            ErrorMessage = “youe message”;
        }
    }
}
MishaU
  • 708
  • 6
  • 14
  • ok.. I'll lookup how to do that.. I added the following .. 1, a reference to System.Web 2, using Microsoft.SharePoint.WebControls; 3, The Override : public override BaseFieldControl FieldRenderingControl { get { return base.FieldRenderingControl; } } ... but I will still look up writing my own field control.. I'm hoping your suggestion is write and it will let me put the valdate an emtpy date field... – Benjamin Callin Oct 26 '11 at 13:33
  • public override BaseFieldControl FieldRenderingControl { get { BaseFieldControl control = null; control = new YourDateTimeField(); /*inherited from DateTimeField*/ control.FieldName = base.InternalName; return control; } } – MishaU Oct 26 '11 at 13:40
  • looks like you are saying put this in the custom field class.. mine is TerminationDateValidation.cs .. it inherits from SPFieldDateTime.. (class TerminationDateValidation : SPFieldDateTime)... but when I use this line: control = new YourDateTimeField(); I get error that it doesn't take 0 arguments.. it wants fields, fieldname .. if I use those exact arguments.. like (ieldControl = new TerminationDateValidation(fields, fname);) .. then it says those arguments don't exist in the current context.. – Benjamin Callin Oct 26 '11 at 14:44
  • If Instead, I create a TerminationDateValidationFieldControl.cs class under the project, and it inherits from BaseFieldContrl.. then I can put this in my custom field class... : – Benjamin Callin Oct 26 '11 at 14:51
  • [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)] public override BaseFieldControl FieldRenderingControl { get { BaseFieldControl fieldControl = null; fieldControl = new TerminationDateValidationFieldControl(); return fieldControl; } } I'm not sure what the point of that SharePointPermission line is, or if I need it.. – Benjamin Callin Oct 26 '11 at 14:52
  • if all that works, what needs to go in the TerminationDateValidationFieldControl.cs class.. currently it only has.. : using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint.WebControls; namespace ContractsTerminationDateValidationField { class TerminationDateValidationFieldControl : BaseFieldControl { } } – Benjamin Callin Oct 26 '11 at 14:52
  • What about the rendering template.. ascx control in a mapped ControlTemplates folder.. do I need that? – Benjamin Callin Oct 26 '11 at 14:54
  • Please read this great article on MSDN http://msdn.microsoft.com/en-us/library/bb862248.aspx and read again my answer. You need two classes: YourCustomField, inherited from SPFieldDateTime and YourFieldControl inherited from DateTimeField – MishaU Oct 26 '11 at 20:37
  • ok.. I read through the article several times.. and there were some additional hurdles, but I got it to work.. – Benjamin Callin Oct 31 '11 at 21:09