Why can't my Sitecore custom validator read the item as it has been edited in Content Editor? - validation

I have created a custom validator in Sitecore. It executes correctly, and I can debug it and step through it.
I extended my validator from StandardValidator, which in turn extends from BaseValidator.
In my validator, I get the item:
var item = GetItem();
GetItem is a method from BaseValidator. Based on my reading, this is the standard method to get the item to be validated.
The problem: the item I get back doesn't reflect the values that have been edited in Content Editor. Instead, it appears to simply be the item from the database (so, what it looked like when it first loaded in Content Editor, without reflecting any changes that might have been made to it).
Thus, I cannot validate. To validate the data an editor might have changed in Content Editor, I need to be able to see those changes. Seeing how the item sits in the database right now doesn't help me.
I did some extended debugging, which may or may not be helpful --
I decompiled the kernel, and walked through both StandardValidator and BaseValidator.
The GetItem method from BaseValidator does what I suspect -- it gets the item from the database. But then it runs it through a method called UpdateItem which appears be intended to overlay the inputted values from Content Editor on top of the stored values (thus returning an object that accurately reflects the data which is currently sitting in the editor). This is clearly what I want.
However, UpdateItem only appears to overlay anything if a property called ControlToValidate has a value...and it never does. I've tried this validator as both a Field Validator and an Item Validator. I've initiated it by both saving an item and by tabbing off a field. I put a breakpoint in Visual Studio and a watch on ControlToValidate. One time (inexplicably) it had a value of FIELD165901047 (which corresponded to a field ID in content editor), but in all other cases, it's been null.
What this means is that UpdateItem effectively does nothing, and the item is simply returned as it currently sits in a database, which doesn't help -- I'm trying to validator values entered in Content Editor before saving to the database.
Regardless of my investigation (I think I understand UpdateItem, but I concede that I might be misinterpeting it, and I'm not accounting for potential decompilation errors), I still have this basic problem:
var item = GetItem();
That never seems to return the values from Content Editor. It returns the item directly from the database, which doesn't help me.

In my validator (a field type validator) I've used
var field = this.GetField();
to get new value of the field to validate. The same should work for a field validator. It's probably a bit different for an item validator but I've never written an item validator.
It's also a method of the BaseValidator and returns a value of the type Field. You can assign this directly to the type of the target field you want, e.g. HtmlField (Sitecore has implemented the casting operators so you can do this):
HtmlField htmlField = field;

I managed to create a work-around for this, and it seems to be working for me. I'm not that happy that I don't know why controlToValidate never has a value, but my workaround manages to get the new value that hasn't yet been saved.
Basically, I've overridden the code that runs GetItem(), and I find the new value in the page's SaveArgs as shown below. The magic happens on the line that has:
field.Value = ((((SaveArgs) ((ClientPage) page)?.CurrentPipelineArgs)?.Items[0].Fields) ?? Array.Empty<SaveArgs.SaveField>()).FirstOrDefault(x => x.ID == field.ID)?.Value ?? field.Value;
protected override Item GetItem()
{
var obj = base.GetItem();
if (obj == null || obj.Versions.Count == 0)
return null;
UpdateItem(obj);
return obj;
}
private void UpdateItem(Item item)
{
Assert.ArgumentNotNull(item, nameof(item));
using (new SecurityDisabler())
item.Editing.BeginEdit();
var page = (Page)null;
var current = HttpContext.Current;
if (current != null)
page = current.Handler as Page;
foreach (Field field in item.Fields)
{
var controlToValidate = ControlToValidate;
if (string.IsNullOrEmpty(controlToValidate) || field.ID != FieldID)
{
field.Value = ((((SaveArgs) ((ClientPage) page)?
.CurrentPipelineArgs)?.Items[0].Fields) ?? Array.Empty<SaveArgs.SaveField>()).FirstOrDefault(x => x.ID == field.ID)
?.Value ?? field.Value;
}
else
{
var str = current != null ? RuntimeValidationValues.Current[controlToValidate] : null;
if (page != null && str != null)
{
var control = page.FindControl(controlToValidate);
if (control != null)
{
if (control is IContentField contentField)
str = contentField.GetValue();
else if (ReflectionUtil.GetAttribute(control, typeof(ValidationPropertyAttribute)) is ValidationPropertyAttribute attribute)
str = ReflectionUtil.GetProperty(control, attribute.Name) as string;
}
}
if (str == null && current != null)
str = current.Request.Form[controlToValidate];
if (str != null && str != "__#!$No value$!#__")
field.Value = str;
}
}
}

Related

Update Dynamics form field when item is selected on another field

I have a Dynamics entity that has a relationship to another entity with a Main form in UX. I have a lookup control on the form to select that related entity. Doing so correctly writes the ID of that related entity record to the current record being edited. I need help grabbing the name value of that linked entity and writing it to the name value of the current record when the lookup selection changes. The form designer makes this so much more difficult than it needs to be! Is there a set of basic jscript libraries that I can add to my solution and call for such simple tasks?
After literally HOURS of hunting down bits and pieces, I finally got this to work!
function updateName(){
var name = "";
var lookupField = Xrm.Page.getAttribute("lookupentityproperty");
// Verify the field does exist on the form and has a selected value
if (lookupField != null && lookupField.getValue() != null && lookupField.getValue()[0] != null) {
name = lookupField.getValue()[0].name;
}
else { name = null; }
var nameField= Xrm.Page.getAttribute("nameproperty");
nameField.setValue(name);
}

Is there a way to make a field in Dynamics CRM as one time entry field?

Basically, what I want is that the user should be able to select the dropdown and not be able to change it after making and saving the selection. So it will be a one-time entry field.
Below is a screenshot of the field I want to apply this property.
So this field has a Yes or No selection. And to make the business logic from failing I have to make it a one-time entry field only.
I looked up the form editor for possible things but couldn't find anything that would let me achieve this.
UPDATE #1
Below is my onload function:
function Form_onload() {
var formType = Xrm.Page.ui.getFormType();
var p = Xrm.Page.getAttribute("opportunityid");
--------------NEW CODE--------------------------------
if(formType ==2){ //form type 2 means the form is a saved form. form type 1 is new form.
alert(formType);
var myattribute = Xrm.Page.getAttribute("var_internal");
var myname = myattribute.getName();
if (Xrm.Page.getControl(myname) != null) {
//alert(myname);
Xrm.Page.getControl(myname).setDisabled(true);
}
}
--------------NEW CODE---------------------------
if (formType == 1 && p != null && p.getValue() != null) {
alert('Child Opportunities can only be created by clicking the Create Child Opportunity button in the Opportunity ribbon.');
window.top.close();
}
}
No code solution: I think you could use an Option set with a Yes/No option and a default of Unassigned Value. Then add that field to Field Level Security with "Allow Update" set to No.
When updating the FLS field permissions, be sure that the profile is associated with the organization "team" so that all users can see the field:
Arun already gave you an hint how to proceed, I just tried this req on one of my instance.
Create one extra field (dummy field) in my case I call it new_hasfieldbeenchanged1
This field will hold data when field is changed. Lock this field (always) and keep this field on form (but visibile =false)
Now you need 2 trigger Onload and OnSave. Below function will do your work. Let me know if this helps.
function onLoad(executionContext) {
debugger;
var formContext;
if (executionContext && executionContext.getFormContext()) {
formContext = executionContext.getFormContext();
//executionContext.getEventSource()
if (formContext.getAttribute("new_hasfieldbeenchanged1") && formContext.getAttribute("new_hasfieldbeenchanged1").getValue()!=null) {
if (formContext.getControl("new_twooptionfield")) {
formContext.getControl("new_twooptionfield").setDisabled(true);
}
}
}
}
function onSave(executionContext) {
debugger;
var formContext;
if (executionContext && executionContext.getFormContext()) {
formContext = executionContext.getFormContext();
//executionContext.getEventSource()
if(formContext.getAttribute("new_hasfieldbeenchanged1") && formContext.getAttribute("new_twooptionfield") && formContext.getAttribute("new_twooptionfield").getIsDirty()){
formContext.getAttribute("new_hasfieldbeenchanged1").setValue((new Date()).toString());
if (formContext.getControl("new_twooptionfield")) {
formContext.getControl("new_twooptionfield").setDisabled(true);
}
}
}
}
Due to environment-specific settings in my DEV, I was not able to reproduce what was suggested by #Eccountable. Although, his solution worked in other environments.
#AnkUser has a good answer as well but I was looking to shorten the code and make things as simple as possible.
My solution
I was able to handle this using Javascript on client-side. using the XRM toolbox.
In the XRM toolbox, I located the javascript for the opportunity and observed field changes in formType when the Opportunity was New and when the Opportunity was Existing. This variable (formType) was =1 when the Opportunity was New and =2 when it was Existing/Saved.
Using this piece of information I was able to leverage my Javascript as follows in Form_onload()
function Form_onload() {
if (formType == 2) {
var myattribute = Xrm.Page.getAttribute("internal");
var myname = myattribute.getName();
if (Xrm.Page.getControl(myname) != null) {
Xrm.Page.getControl(myname).setDisabled(true);
}
}
}
There’s no OOB configuration for such custom requirements, but we can apply some script logic.
Assuming this is a picklist with default null and not a two-option bool field, we can use onLoad form script to check if it has value & lock it. No need to have onChange function.
If it’s a bool field, then it’s hard to achieve. You have to track the initial value & changes made to implement the logic you want. Or through some unsupported code.

Trying to use [Description] data annotation attribute with existing code

SLIGHT UPDATE BELOW
I am trying to use the [Description] data annotation attribute with enums in order to display a friendly name. I've searched around a lot and cannot get anything implemented. Right now I have code that will display an enum as a string (using an extension), but I am not liking ThisIsAnEnum as an enum name (which is spaced out by the string extension) and it prohibits me from having longer names (which I need to maintain) such as for a radio button item. My goal is to have longer descriptions for radio button items without having to write really long enums. An extension/helper will probably be the right way to go, but I need to "fit" it into the code I am using, which is where I failed using the many examples out there.
The code I am using is generic, in that depending upon some logic either a radio button list, check box list, drop down list, select list or regular text boxes are displayed. For multi-item lists enum's are used, and the enum name is what is displayed (after using the string extension).
Here is the particular code that displays the enum:
public static IEnumerable<SelectListItem> GetItemsFromEnum<T>
(T selectedValue = default(T)) where T : struct
{
return from name in Enum.GetNames(typeof(T))
let enumValue = Convert.ToString((T)Enum.Parse(typeof(T), name, true))
select new SelectListItem
{
Text = name.ProperCase(),
Value = enumValue,
Selected = enumValue.Equals(selectedValue)
};
}
ProperCase is the class that changes the enum to something readable.
I found something that almost worked:
public static string GetEnumDescription<TEnum>(TEnum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if ((attributes != null) && (attributes.Length > 0))
return attributes[0].Description;
else
return value.ToString();
}
in which case I changed code from Text = name.ProperCase(), to Text = name.GetEnumDescription(...) but if I put value in the parenthesis I get a "does not exist in the current context" message (which I tried fixing but just made the problem worse). If I leave it blank I get the "No overload for ... takes 0 arguments" (again, understandable - but I don't know how to fix). And if I put name in the parenthesis the code compiles but upon viewing the page I get the "Object reference not set..." error on this line:
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes
(typeof(DescriptionAttribute), false);
I've spent a lot of time on this and know that my stumbling block is the
Text = name.ProperCase(),
code. Any ideas/help? Thanks in advance.
UPDATE:
If I do:
Text = GetEnumDescription(selectedValue),
I actually DO get the [Description] text, however, it just displays for the first enum. So, if I have 5 enums all with different [Description]'s the code just repeats the [Description] for the first enum 5 times instead of displaying differently for each. I hope that makes sense and gets to narrow down the problem.
I'd recommend you the Display attribute:
public static IEnumerable<SelectListItem> GetItemsFromEnum<T>(T selectedValue = default(T)) where T : struct
{
return
from name in Enum.GetNames(typeof(T))
let enumValue = Convert.ToString((T)Enum.Parse(typeof(T), name, true))
select new SelectListItem
{
Text = GetEnumDescription(name, typeof(T)),
Value = enumValue,
Selected = name == selectedValue.ToString()
};
}
public static string GetEnumDescription(string value, Type enumType)
{
var fi = enumType.GetField(value.ToString());
var display = fi
.GetCustomAttributes(typeof(DisplayAttribute), false)
.OfType<DisplayAttribute>()
.FirstOrDefault();
if (display != null)
{
return display.Name;
}
return value;
}
and then you could have:
public enum Foo
{
[Display(Name = "value 1")]
Value1,
Value2,
[Display(Name = "value 3")]
Value3
}
And now you could have:
var foo = Foo.Value2;
var values = GetItemsFromEnum(foo);
Also notice that I have modified the Selected clause in the LINQ expression as yours is not correct.
This being said, personally I would recommend you staying away from enums on your view models as they don't play nicely with what's built-in ASP.NET MVC and you will have to reinvent most of the things.

How can I create temporary records of Linq-To-Sql types without causing duplicate key problems?

I have code that generates records based on my DataGridView. These records are temporary because some of them already exist in the database.
Crop_Variety v = new Crop_Variety();
v.Type_ID = currentCropType.Type_ID;
v.Variety_ID = r.Cells[0].Value.ToString();
v.Description = r.Cells[1].Value.ToString();
v.Crop = currentCrop;
v.Crop_ID = currentCrop.Crop_ID;
Unfortunately in this little bit of code, because I say that v.Crop = currentCrop,
now currentCrop.Crop_Varieties includes this temporary record. And when I go to insert the records of this grid that are new, they have a reference to the same Crop record, and therefore these temporary records that do already exist in the database show up twice causing duplicate key errors when I submit.
I have a whole system for detecting what records need to be added and what need to be deleted based on what the user has done, but its getting gummed up by this relentless tracking of references.
Is there a way I can stop Linq-To-Sql from automatically adding these temporary records to its table collections?
I would suggest revisiting the code that populates DataGridView (grid) with records.
And then revisit the code that operates on items from a GridView, keeping in mind that you can grab bound item from a grid row using the following code:
public object GridSelectedItem
{
get
{
try
{
if (_grid == null || _grid.SelectedCells.Count < 1) return null;
DataGridViewCell cell = _grid.SelectedCells[0];
DataGridViewRow row = _grid.Rows[cell.RowIndex];
if (row.DataBoundItem == null) return null;
return row.DataBoundItem;
}
catch { }
return null;
}
}
It is also hard to understand the nature of Crop_Variety code that you have posted. As the Crop_Variety seems to be a subclass of Crop. This leads to problems when the Crop is not yet bound to database and potentially lead to problems when you're adding Crop_Variety to the context.
For this type of Form application I normally have List _dataList inside form class, then the main grid is bound to that list, through ObjectBindingList or another way. That way _dataList holds all data that needs to be persisted when needed (user clicked save).
When you assign an entity object reference you are creating a link between the two objects. Here you are doing that:
v.Crop = currentCrop;
There is only one way to avoid this: Modify the generated code or generate/write your own. I would never do this.
I think you will be better off by writing a custom DTO class instead of reusing the generated entities. I have done both approaches and I like the latter one far better.
Edit: Here is some sample generated code:
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="RssFeed_RssFeedItem", Storage="_RssFeed", ThisKey="RssFeedID", OtherKey="ID", IsForeignKey=true, DeleteOnNull=true, DeleteRule="CASCADE")]
public RssFeed RssFeed
{
get
{
return this._RssFeed.Entity;
}
set
{
RssFeed previousValue = this._RssFeed.Entity;
if (((previousValue != value)
|| (this._RssFeed.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._RssFeed.Entity = null;
previousValue.RssFeedItems.Remove(this);
}
this._RssFeed.Entity = value;
if ((value != null))
{
value.RssFeedItems.Add(this);
this._RssFeedID = value.ID;
}
else
{
this._RssFeedID = default(int);
}
this.SendPropertyChanged("RssFeed");
}
}
}
As you can see the generated code is establishing the link by saying "value.RssFeedItems.Add(this);".
In case you have many entities for wich you would need many DTOs you could code-generate the DTO classes by using reflection.

How to ensure a boolean field to be set in Grails?

I want to ensure that one of two form fields representing a boolean value is checked. But there is no appropriate constraint to do this. nullable: false does not work.
class Organisation {
Boolean selfInspecting
static constraints = {
selfInspecting(nullable: false)
}
}
How can I check whether one of the two fields is checked or not?
Perhaps the simplest approach is to use a form that ensures a value is picked. As such, creating a radio buttons rather than checkboxes is a better solution. It would directly represent your intent as well.
You can also check this in the Controller, e.g.
if (params.checkBox1 != 'on' && params.checkBox2 != 'on')
flash.error = 'At least one value must be checked.'
return ...
you can write your own custom validator.
something like
selfInspecting(validator: {val, obj -> /*test selfInspecting here*/})
EDIT -- in response to the other answer -- you can handle this on the form, but you should also handle it on the server.
ANOTHER EDIT -- It was suggested in a comment that you might want to validate one of two fields on your Domain class. This is also easily accomplished with a custom validator. With the signature above for the custom validator closure, the val is the value selfInspecting, and obj is the domain object instance. So you could have
{ val, obj ->
if (val == null) return false // if you want to ensure selfInspecting is not null
else return true
... or ...
// if you want to check that at least 1 of two fields is not null
def oneOrTheOther = false
if (obj.field1 != null || obj.field2 != null)
oneOrTheOther = true
return oneOrTheOther
}

Resources