Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have created a custom listform template. In this custom listform template I have removed the default <SharePoint:ListFieldIterator runat="server"/> and used my own created user control <CustomOverrideControls:CustomListFieldIterator runat="server"/>. In this user control I overwrite the CreateChildControls() method. In this method I do some logic to set some fields readonly.

public class CustomListFieldIterator : ListFieldIterator
{
    protected override void CreateChildControls()
    {
        this.Controls.Clear();
        if (this.ControlTemplate == null)
        {
            throw new ArgumentException("Could not find ListFieldIterator control template.");
        }
        for (int i = 0; i < base.Fields.Count; i++)
        {
            SPField field = base.Fields[i];
            if (!this.IsFieldExcluded(field))
            {
                var child = new CompositeField();
                child.ControlMode = base.ControlMode;
                child.FieldName = field.InternalName;
                var auditors = SPContext.Current.Web.Groups["Auditors"].ID;
                bool isAuditor = SPContext.Current.Web.IsCurrentUserMemberOfGroup(auditors);

                //if field is "Title", we in edit mode and user is not Auditor, make field readonly
                if (field.InternalName.Equals("Title") && ControlMode == SPControlMode.Edit && !isAuditor)
                {
                    child.ControlMode = SPControlMode.Display;
                }
                this.Controls.Add(new LiteralControl("<tr>"));
                this.Controls.Add(child);
                Controls.Add(new LiteralControl("</tr>"));
            }
        }

    }
}

Now I have the following problem. I have some fields which are required. If let an required field empty and press on Save, I got 2 validation warnings.

Some extra information. I am using also the ItemAdded event and do some logic. I have set the ItemAdded event to Asynchronous. I have did this because the page was not refreshing after adding a new item.

<Receivers ListTemplateId="101">
      <Receiver>
        <Name>something</Name>
        <Type>ItemAdded</Type>
        <Assembly>something</Assembly>
        <Class>something</Class>
        <SequenceNumber>10000</SequenceNumber>
        <Synchronization>Asynchronous</Synchronization>
      </Receiver>
  </Receivers>

What is the reason why I got 2 validation warnings on my form after saving with empty fields.

UPDATE: I have implemented the solution of Andrey Markeev, but it doesnt work. See below for some new information:

I have implemted your solution but it doesnt fix the duplicate validation warnings. I have undo all my changes like removing custom listform template and use the default template, change the itemadded event to asynchronous, remove my usercontrol and add the sharepoint listFieldIterator control back. I have the same problem. Do you see here something wrong:

This field has no duplicate validation error:

<Field ID="{A5A5CA1A-6CF4-4BA7-B650-7F06910EA0CD}" Name="RFCAppliesTo" StaticName="RFCAppliesTo" DisplayName="Betrekking op" Type="Text" Required="TRUE" />

These fields has duplicate validation erros. What is the problem?

<Field ID="{47705f89-4883-4ad5-8495-98ce1d4b5c35}" Name="RFCDescriptionCurrent" StaticName="RFCDescriptionCurrent" DisplayName="Omschrijving huidige situatie" Type="Note" NumLines="4" Required="TRUE" RichText="TRUE" RestrictedMode="TRUE" RichTextMode="FullHtml" IsolateStyles="TRUE" AppendOnly="FALSE" />
      <Field ID="{F61E42BC-AEC0-43AA-9AD2-AD7B36C60B22}" Name="RFCDescriptionNew" StaticName="RFCDescriptionNew" DisplayName="Omschrijving nieuwe situatie" Type="Note" NumLines="4" Required="TRUE" RichText="TRUE" RestrictedMode="TRUE" RichTextMode="FullHtml" IsolateStyles="TRUE" AppendOnly="FALSE" />
      <Field ID="{97CD7687-4F16-4AC5-9156-1F178037B5D1}" Name="RFCRisks" StaticName="RFCRisks" DisplayName="Risico's" Type="Note" NumLines="4" Required="TRUE" RichText="TRUE" RestrictedMode="TRUE" RichTextMode="FullHtml" IsolateStyles="TRUE" AppendOnly="FALSE" />
      <Field ID="{D424D1B4-4CEA-4E47-B843-6B746EAD1E35}" Name="RFCDemands" StaticName="RFCDemands" DisplayName="Eisen, randvoorwaarden, afhankelijkheden" Type="Note" NumLines="4" Required="TRUE" RichText="TRUE" RestrictedMode="TRUE" RichTextMode="FullHtml" IsolateStyles="TRUE" AppendOnly="FALSE"/>
share|improve this question
add comment

2 Answers 2

The field controls aren't correctly created in your example. That's why some of the markup is most likely created twice, and that's why you end up with duplicate validator controls on the page.

Try this code:

protected override void CreateChildControls()
{
  this.Controls.Clear();
  if (this.ControlTemplate == null)
    throw new ArgumentException("Could not find ListFieldIterator control template.");

  var auditors = SPContext.Current.Web.Groups["Auditors"].ID;
  bool isAuditor = SPContext.Current.Web.IsCurrentUserMemberOfGroup(auditors);

  for (int index = 0; index < this.Fields.Count; ++index)
  {
    SPField field = this.Fields[index];
    if (!this.IsFieldExcluded(field))
    {
      TemplateContainer templateContainer = new TemplateContainer();
      this.Controls.Add((Control) templateContainer);

      //if field is "Title", we in edit mode and user is not Auditor, make field readonly
      if (field.InternalName.Equals("Title") && this.ControlMode == SPControlMode.Edit && !isAuditor)
      {
          templateContainer.ControlMode = SPControlMode.Display;
      }
      else
          templateContainer.ControlMode = this.ControlMode;

      templateContainer.FieldName = field.InternalName;
      this.ControlTemplate.InstantiateIn((Control) templateContainer);
    }
  }
}

This is based on original code from ListFieldIterator.

share|improve this answer
    
So the only differance is to use TemplateContainer and not CompositeField ? –  KHA Feb 27 '13 at 13:18
    
Yes, you have to use TemplateContainer and instantiate it correctly in the ControlTemplate. The thing is, if you open Microsoft.SharePoint.WebControls.ListFieldIterator in Reflector you will see that the original control uses exactly this way of adding controls to the page. I didn't test the code but I think it worths a try. –  Andrey Markeev Feb 27 '13 at 13:41
    
It doesnt fix my problem. See my comments above for an update.. –  KHA Feb 27 '13 at 22:48
add comment

I have fixed my problem as folow:

Remove RichTextMode="FullHtml" Change RichText="TRUE" to RichText="FALSE"

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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