SharePoint 2010 introduces a new feature to allow validation formula as shown in the following picture:
If you want to access to this through the object model, you have two additional properties on the SPField object in SharePoint 2010:
- ValidationFormula : string containing the formula (ex: ‘[MyColonne]=’SampleValue’’)
- ValidationMessage : string containing the message to display if the condition is not met (ex: ‘Sample Value is required’)
Interesting point to note regarding the object model is that there is another field named ValidationEcmaScript that contains the script automatically generated by SharePoint to implement this validation formula. Unfortunately, this field is readonly so you won’t be able to change it to enter a different script…
If you have a look at the ValidationEcmaScript value for a column that does not have any validation (or a validation formula that is not well formed), you will see the following script:
- function(x){return 1}
You will also see this function that returns 1 for site columns but as soon as you add this column to the list, here is the function that you will see:
- function(x){return SP.Exp.Calc.valid(SP.Exp.Node.f(‘EQ’,[SP.Exp.Node.a(0),SP.Exp.Node.v("SampleValue")]),x)}