Validate Custom Sharepoint field if Empty?

2.3k views Asked by At

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

There are 1 answers

9
MishaU On

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”;
        }
    }
}