# Validate on specified field and display specific error message

**URL:** https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572
**Category:** Questions
**Tags:** litium-7
**Created:** [July 27, 2020, 9:34am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572 "2020-07-27T09:34:40Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/artur/32/948_2.png) [@Artur](https://forum.litium.com/u/Artur)
#### Post date: [July 27, 2020, 9:34am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/1 "2020-07-27T09:34:40Z")

</div>

Hi,

I remember that we could do field validation in Litium BO by

1. General, showing error message above the whole page.
2. Doing validation on specified field and display message below it.

But how did you toggle if it should display the message on the field that did throw the error vs the whole page.

Tried to find it on;  
[https://github.com/LitiumAB/Education/tree/master/Developer%20Education/Tasks/Validation](https://github.com/LitiumAB/Education/tree/master/Developer%20Education/Tasks/Validation) and [https://docs.litium.com/documentation/architecture/validation](https://docs.litium.com/documentation/architecture/validation) to no avail.

Litium version: 7.2.3

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [July 27, 2020, 9:37am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/2 "2020-07-27T09:37:28Z")

</div>

The first parameter in the `AddError` method specifies where the error should be displayed, you pass the field id if you want it shown next to the field or you pass `*` if you want it shown at the top.

There’s an example here: [https://github.com/LitiumAB/Education/blob/master/Developer%20Education/Tasks/Validation/Resources/ValidateBookAuthor.cs](https://github.com/LitiumAB/Education/blob/master/Developer%20Education/Tasks/Validation/Resources/ValidateBookAuthor.cs)

```auto
if (!isAuthorTemplate)
{
	// Pass ID of a field as first parameter to display the error next to that field in backoffice-UI
	result.AddError("AuthorField", "Only Author-pages can be selected as author");

	// ...and/or pass "*" to display the validation error in the header.
	result.AddError("*", "Author page validation failed");
}

```

---

<div class="post-metadata">

### Author: ![Artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/artur/32/948_2.png) [@Artur](https://forum.litium.com/u/Artur)
#### Post date: [July 27, 2020, 9:47am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/3 "2020-07-27T09:47:02Z")

</div>

Hm,

Then my code was right, is this feature available in 7.2.3?  
Tried without the ToLower() but it always displays the message on top.

validationResult.AddError(“Id”.ToLower(), “already exists!”);  
validationResult.AddError(“Id”, “already exists!”);

Tried also using “\_name” but that also just appends the message to top and not on the actual field.

Update:  
To be clear here, i’m using `ValidationRuleBase<Page>`  
And validating page in Websites BO.

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [July 27, 2020, 10:23am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/4 "2020-07-27T10:23:14Z")

</div>

Could you try performing the validation on the `DraftPage` entity instead?

---

<div class="post-metadata">

### Author: ![Artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/artur/32/948_2.png) [@Artur](https://forum.litium.com/u/Artur)
#### Post date: [July 27, 2020, 10:29am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/5 "2020-07-27T10:29:49Z")

</div>

Changed `ValidationRuleBase<DraftPage>` though same issue.  
Though \_name is working now

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [July 27, 2020, 10:46am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/6 "2020-07-27T10:46:56Z")

</div>

And the original field id that you pass to `AddError` is correct?

I’m not able to reproduce the issue. In my testing, when saving or trying to publish my draft page the validation error is shown next to the field if a field with that id exists in the template. If not, I only see _Validation error_ at the top.

Could you share your code?

---

<div class="post-metadata">

### Author: ![Artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/artur/32/948_2.png) [@Artur](https://forum.litium.com/u/Artur)
#### Post date: [July 27, 2020, 1:48pm UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/7 "2020-07-27T13:48:01Z")

</div>

Hi,

Correct we are using constants for everything.

// FieldDefinitionSetup

```auto
new FieldDefinition<WebsiteArea>(PageFieldNameConstants.CustomId, SystemFieldTypeConstants.LimitedText)
{
     MultiCulture = false 
},

```

// Template

```auto
new FieldTemplateFieldGroup()
{
    Id = "General",
    Collapsed = false,
    Fields =
    {
        SystemFieldDefinitionConstants.Name,
        SystemFieldDefinitionConstants.Url,
        PageFieldNameConstants.CustomId,
    }
},
....

```

// Validation

```auto
// Does not work
validationResult.AddError(PageFieldNameConstants.CustomId, "already exists");
validationResult.AddError(PageFieldNameConstants.CustomId.ToLower(), "already exists");

// Works
validationResult.AddError(SystemFieldDefinitionConstants.Name,"already exists");

```

**I have a feeling the issue is that it’s using LimitedText**  
Tried on all other fields on the template and it works when using draftpage.

**Update;**  
After testing multiple fields / values it seems to be an issue with `multiculture` vs `non multiculture`.

Created a field Text with multiculture = false via backoffice and tried to throw validation and it would just hoist it up with validation error.

Created a field Text with multiculture = true via backoffice and tried to throw validation and it would display as expected.

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [July 27, 2020, 3:24pm UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/8 "2020-07-27T15:24:41Z")

</div>

That’s strange. I tested now with a LimitedText field and MultiCulture set to false and it works as expected. Does it happen on a fresh page? So there’s no faulty data left from testing.

```auto
new FieldDefinition<WebsiteArea>("Custom", SystemFieldTypeConstants.LimitedText)
{
	 MultiCulture = false
},

```

```auto
public override ValidationResult Validate(DraftPage entity, ValidationMode validationMode)
{
	var result = new ValidationResult();
	var value = entity.Fields.GetValue<string>("Custom");
	if (value == "test")
	{
		result.AddError("Custom", "Can't be test");
	}

	return result;
}

```

![image](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/fdc1c493ef7fab9553926c0365c6142bdd30e689.png)

---

<div class="post-metadata">

### Author: ![Artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/artur/32/948_2.png) [@Artur](https://forum.litium.com/u/Artur)
#### Post date: [July 27, 2020, 5:19pm UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/9 "2020-07-27T17:19:10Z")

</div>

Hm, I wonder if this is some kind of casing issue;  
Tried to replicate this again and suddenly it started working on the new fields but not on the one i want it to work on.

Here is a testcase.  
3 fields, Custom, CCustom, CCCustom.  
Then i add 3 errors on all of them.  
Look how it will display error only on the first 2

Constants

```auto
public const string CustomId = "CustomId";
public const string CCustomId = "CCustomId";
public const string CCCustomId = "CCCustomId";

```

Definition

```auto
new FieldDefinition<WebsiteArea>(PageFieldNameConstants.CustomId,SystemFieldTypeConstants.LimitedText){ MultiCulture = false},
new FieldDefinition<WebsiteArea>(PageFieldNameConstants.CCustomId,SystemFieldTypeConstants.LimitedText){ MultiCulture = false},
new FieldDefinition<WebsiteArea>(PageFieldNameConstants.CCCustomId,SystemFieldTypeConstants.LimitedText){ MultiCulture = false},

```

Validation

```auto
validationResult.AddError(PageFieldNameConstants.CustomId, $"error custom");
validationResult.AddError(PageFieldNameConstants.CCustomId, $"error custom");
validationResult.AddError(PageFieldNameConstants.CCCustomId, $"error custom");

```

See that the last one does not trigger.  
Tried to change the order of AddError just to be sure and it seems that it’s some casing issue. Tried ToLower() on all then none of them will trigger, maybe thats why the Test did not work. But this seems to be reproducable consistently.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/9168e7e2f9d9c800205950d1afef5c6ed1b76c74.png)

Result onsave

```auto
{
    "message": "The request is invalid.",
    "modelState": {
        "$type": "System.Web.Http.HttpError, System.Web.Http",
        "ccCustomId": {
            "$type": "System.String[], mscorlib",
            "$values": ["error custom"]
        },
        "cCustomId": {
            "$type": "System.String[], mscorlib",
            "$values": ["error custom"]
        },
        "customId": {
            "$type": "System.String[], mscorlib",
            "$values": ["error custom"]
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)
#### Post date: [July 28, 2020, 7:30am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/10 "2020-07-28T07:30:34Z")

</div>

Yeah, the field id returned is `ccCustomId` while the input field has the id `cCCustomId`. Same thing happens in 7.4.2. Can you please report a bug on Docs for that? Thanks!

Is your original use case working now or does it hit this issue?

---

<div class="post-metadata">

### Author: ![Artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/artur/32/948_2.png) [@Artur](https://forum.litium.com/u/Artur)
#### Post date: [August 7, 2020, 11:26am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/11 "2020-08-07T11:26:19Z")

</div>

Hi, sorry for slow reply, i do hit this issue. But i worked around it so it will display as general error message.

I’ll create a bug about this.

---

<div class="post-metadata">

### Author: ![khoa.vu](https://avatars.discourse-cdn.com/v4/letter/k/96bed5/32.png) [@khoa.vu](https://forum.litium.com/u/khoa.vu)
#### Post date: [August 11, 2020, 10:04am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/12 "2020-08-11T10:04:29Z")

</div>

Hi, you can work around by using `FieldId.ToCamelCase()` while waiting for a fix.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)
#### Post date: [September 8, 2020, 10:04am UTC](https://forum.litium.com/t/validate-on-specified-field-and-display-specific-error-message/1572/13 "2020-09-08T10:04:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
