Throwing a message when Updating/Validating Part contents/data - validation

Crosspost: https://orchard.codeplex.com/discussions/455498
When updating a Part, in the DriverResult Editor, how can we manually throw a warning message when implementing custom validation?
I want to add a custom validation for some date fields, where values are dependent on some other field in the Content Item itself (dates in the part need to be between a specific date in the content item's field).
Which leads me to another question, can we set validation in model properties between one another? Like if there are two properties StartDate and EndDate, is it doable in the model to declare that end date must be after start date?

You can add model validation errors using the "updater" parameter being passed in:
updater.AddModelError("StartDate", T("Please enter a date in the future"));

Related

Business rule: Add other field value to date?

I'm trying to programmatically set the Due Date field of a custom activity. I want take the value of another date-time field that the users enters and add the value from an integer field. But it seems that I can only select a static value to add using the business rules as shown here:
Is there a way to do it using business rules? Or do I have to use a Javascript instead?
You can use calculated field for these scenarios, but it is not a physical field to store. If you have to store the value, then Javascript or plugin or WF is better.
Business rules are limited in these situations.
This should be quite easy using PowerApps expression language; no need for PowerApps "Rules" (which are slated to be deprecated).
To your app:
Add a DatePicker control
Add a TextBox control
Add a Label control
Set the Label control's Text property to:
DateAdd(
DatePicker.Selected.Value,
Value(TextBox.Text),
Days
)
Bingo!

How to autogenerate Primary Name field in custom Common Data Service Entity?

Primary Name field is by default "Required". How to fill this field by GUID or random number on save?
Codeless solution: You can mark that field as not a Business Required, so you will be unblocked. You can use a codeless UI workflow to populate the Primary Name field with some fixed text. This WF can be triggered on post-create.
Code solution: Plugin can be triggered on pre-create & custom C# code can be used to pre-populate some autonumber/text in this field.
Pro tip: Model driven app is nothing but classic Dynamics CRM, so while searching for solutions use that keyword :)
There is a GUID() function in PowerApps that generates a random GUID. By default, its result is a GUID type of data (text, value, date, etc.).
Primary Name is a text field. To coerce the result of the GUID function to be a text string, wrap Text() around it:
Text(GUID())
So in your Patch statement or in the Update property of the field for PrimaryName, you can use the expression above to put in a GUID as a string.

Apply Sitecore validation rule to a field type?

I have created a validation rule in Core DB, and I have created a custom Sitecore field called 'Single Select Treelist'. I want to apply this validation rule to all 'Single Select Treelist' fields in all templates in my Sitecore instance.
I know we can individually apply the validation rule to each template field of type 'Single Select Treelist'. Is there a way to achieve this automatically for all 'Single Select Treelist' fields without any custom coding?
I see the list of field type validation rules available in Master:
How are these connected to the respective field types in Core?
Please advise.
If you edit the field type item there is a validation section that contains fields to set validation rules for the Quick Action Bar, the Validate Button, the Validator Bar and finally Workflow.
Once you set the validation rules, depending on which field you set, Sitecore will display the validation messages in different ways. See this blog post for examples: http://sitecoreworld.blogspot.com/2014/12/sitecore-validation-examples.html
The link between the Field Type Validation Rule and the actual Field Type seems to be based simply on the name of the Field Type.
Just add a new item with the template "Field Type Validation Rules" in the Validation Rules/Field Types folder that you show in your picture. Give it the exact same name as the field type you want it to validate. Select the rules you need inside the Validation Rules section of that new object.
For example, I just needed to add some validation to the Single-Line Text type, as you can see in the image. We struggled with it for a little while because we missed the dash.
Image - Sitecore Tree with new Field Type Validation

Joomla Component Dynamic Custom Field based on other Field Value

I am creating a new Joomla Component. I am able to create custom fields for the component backend forms - but I am not able to create Dynamic Custom Fields.
What I would like to do is have a field that is dynamical populated based on the value of a previous field. The easiest way to explain this is the simple country,state,city breakdown.
Field 1 = Country
Field 2 = State (Based on what the user selected as as Country in Field 1)
Field 3 = City (Based on what the user selected as State in Field 2)
The fields would of course need to be refreshed, reset as the user picks a different country etc.
The data to populate the fields would also all need to come from a database based on the previous fields value.
I am guessing this needs to be done via ajax or javascript or something? But wondering if there is an official way? Especially since there are database calls involved.
Please let me know if there is anything I can explain better..
David
I ended up just creating text fields but then validating the input to make sure the values added are correct.

MS CRM - getting the field that caused a form save

I have a MS CRM 4 form where when certain fields are changed, I need those fields to be written to an excel sheet.
So, I edited the form field onchange event to call crmform.save() which triggers a plugin to run that writes the field value to a named range (1 cell) of an excel sheet.
However, I don't know which field caused the save. Is there a way to get that information? (Not all fields on the form need to go to the excel sheet)
If I use this: DynamicEntity target = (DynamicEntity)Context.InputParameters[ParameterName.Target];
I can look at specific fields, but I have no way of knowing which ones changed.
Any suggestions?
It's my experience that only the changed fields will have value (in the DynamicEntity) when you update an entity. So let's say you have an entity with a FirstName and Lastname. If you only change the LastName and save. The LastName will have a value however FirstName with be null (unless you have som javascript code that forces the FirstName to be submitted).
If your plugin is triggered on the Pre-Event of the Update on your entity, you should be able to compare the IPluginExecutionContext.PreEntityImages to your DynamicEntity that you've fetched from the Target.
Or if your plugin is triggered on the Post-Event of the Update, you should be able to compare the IPluginExecutionContext.PreEntityImages with IPluginExecutionContext.PostEntityImages.
Looks like you are already going a different way, but another suggestion is to create a hidden field - "FieldThatChanged." In your field on-change javascript, set that field to the name of the field that changed, then access that in your plugin.
Yep, totally changed the way I approached this. I'm using jquery in an iframe to pass all the exact fields (name/value pairs) I want to a method of an ASPX file running in the ISV folder that takes the neccesary action. Thanks for the suggestions all!
I recommend using the Pre and Post Images as suggested by #Forgotten Semicolon. And in addition set the Filtering Attributes so the Plugin is triggered only on the change of the fields you care about.

Resources