Asp.Net MVC 3 validate 2 dependent fields - asp.net

Say I have a form that has fields to gather data about future conferences (not the real domain but will suffice for the purposes of this question). Part of the validation is that a new event can only happen once in that year so for instance if you have a "stackoverflow getmessyandrunk conference" that happens in 2012 - the user shouldn't be allowed to enter a new event with the same name and year...
Now - what I have tried so far is to add a remote validator to each of the fields:
one for the event name field that uses the AdditionalFields property to include the year field and one for the year field which uses the AdditionalFields to include the event name field.
it doesn't work - if I try to add an event with the same name and year it adds an error to just the year field, correcting it fixes it, then trying again it adds an error to the name field and year field, correcting the name field to make the combination unique only removes the error from the name field - it goes on like this...
How are others doing this?

If you prefer using data annotations for validation, I would recommend that you just use the Foolproof validation library that is available on Codeplex: https://foolproof.codeplex.com/
It supports, amongst others, the following "requiredif" validation attributes / decorations:
[RequiredIf]
[RequiredIfNot]
[RequiredIfTrue]
[RequiredIfFalse]
[RequiredIfEmpty]
[RequiredIfNotEmpty]
[RequiredIfRegExMatch]
[RequiredIfNotRegExMatch]
To get started is easy:
Download the package from the provided link
Add a reference to the included .dll file
Import the included javascript files
Ensure that your views references the included javascript files from within its HTML for unobtrusive javascript and jquery validation.
If you don't like mixing your domain and and validation logic by decorating your domain classes with validation attributes, you can always opt for the powerful fluent validation. You can find the library for it here:
https://fluentvalidation.codeplex.com/
It is unfortunately server-side only, but a suitable option if seperation of concerns is of importance to you... and context is not lost due to the validation being tied to a specific field. (See here: http://www.codeproject.com/Articles/326647/FluentValidation-and-Unity)

Related

How can I use the field I've added to the field group? AX7 Dynamics 365

All of my codes are in extensions.
I've added a field into the table, and also added that field into the assigned field group.
Now it is visible on my Design.
and I want to place a code into the event of it where it is modified.
So I've created a new class, then get the event handler.
But when I paste it, there's an error and I checked the intelisense and it can't see the field I placed, well in fact, it is there:
How can I use my newly added field? Is there something I am missing?
I can reproduce this problem in Platform Update 8. It seems to be a limitation of the product when working with controls added through field groups.
Nevertheless the solution is simple - use Modified event of the datasource field, not the individual control. That's what you should do in most cases anyway, because one field may be displayed at several places, but it should do the same thing when modified from either of them.
You can use the registerExtensionInstance method as described in this blog post and register override of the required control's method from method new.
You can see how the registerExtensionInstance pattern is used in a number of classes in AX:
CaseDetailEventHandler
DirPartyQuickCreateFormApplicationSuiteEventHandlers
EssWorkspaceFormExtensionAppSuiteEventHandler
EssWorkspaceFormExtensionExpenseEventHandler
FormControlQuickPersonalizationTileExtension
HCMCaseDetailCreateEventHandler
HCMCaseDetailEventHandler
HcmCaseDetailEventHandler_HcmWL
HcmEnrollmentDateManagerFormExtensionEventHandler
HcmJobFormExtensionRetailHandler
HcmPositionActionDetailFormExtensionPayrollHandler
HcmPositionDateManagerFormExtensionPayrollHandler
HcmPositionFormExtensionPayrollHandler
HcmPositionMassUpdateFormEventHandler
HcmWorkerDateManagerFormExtensionPayrollHandler
HcmWorkerEnrollmentFormExtensionPayrollEventHandler
HcmWorkerFormExtensionPayrollHandler
HierarchyDetailEventHandler
HRMCourseInstructorFormExtensionAppSuiteHandler
smmActivitiesEventHandler
smmContactPersonFormApplicationSuiteEventHandlers
SysGenBlankWorkspaceOptionsPowerBIExtension

How do I find out a field's type information for a 'record' in appmaker in Server script?

I'm trying to create a re-usable script for capturing record changes onSave with Server-side scripting. To do that, I need the model information for a given table, including what type each field is.
I have figured out how to get the model for my table and details for the fields:
var table = "Clients";
var myObject = app.models[table];
// Dump the properties of the 2nd field in the model
console.log("Field 2 properties: " + JSON.stringify(myObject["L"]["fields"]["1"]));
I see this:
{"name":"Client",
"key":"zzzkS1spSPKkRXMn",
"displayName":null,
"description":"Short name for client (must be unique)",
"type":{},
"required":false,
"uid":false,
"defaultValue":null,
"minLength":0,
"maxLength":null,
"integer":false,
"sortable":true,
"minValue":null,
"maxValue":null,
"regexp":null,
"regexpError":null,
"possibleValues":null,
"aggregationType":null
}
"type" looks like an empty property here and I can't seem to figure out how to get any reference to it to tell me what I need.
How do I get usable type information for a given field in a model?
Right now, App Maker doesn't expose an API to access the model metadata.
You snippet is actually accessing App Maker's internal state and might break in future releases (the "L" property is actually obfuscated by a JS compiler and not designed to be accessed from user land).
We know this kind of meta-programming is handy and this is something we might add in the future based on user feedback. Please feel free to submit a request feature in our issue tracker (https://developers.google.com/appmaker/support).

How can I add server-side validation to an unknown quantity of controls?

I have a form with a variable quantity of questions based on a database value.
The form is generated at runtime. All the questions are in the same multi-line format.
Question 1
Textbox for answer
Question 2
Textbox for answer
...
For each answer, I must validate the output and provide a customized validation message. e.g.
You have provided the following invalid characters: (...)
I was able to generate a custom validator for each of the controls for client-side validation.
I was trying to reuse the concept for server-side validation, but the options don't seem to be available. I don't see how to write a Validator_ServerValidate function if I don't know the name or quantity of validators before runtime.
How can I add server-side validation to an unknown quantity of controls?

Creating blank dummy Components which contain mandatory Fields with the SDL Tridion 2011 Core Service

I wanted to create a blank Component in SDL Tridion 2011 using the Core Service. The only information I have at the start of the process is the Schema URI. The Schema may contain any kind of field (text, rtf, number date, embedded etc), some of which may be mandatory.
I understand that for the mandatory fields, I will need to save some dummy value in them, and this is acceptable as they will be changed manually later.
How can i achieve this?
First - you make sure all fields are set to optional in the schema, otherwise this will never work.
Second - You save.
When an optional field has no value, it will have no XML representation. If you have a schema that defines a component like this:
Field1
Field2
Field3
When all fields are optional and you save a value in Field 2, Tridion will store the following:
<Content xmlns="yourNamespace"><Field2>SomeValue</Field2></Content>
If one of your fields is not mandatory, then you'll have to provide a value. If you're using the CoreService then you can use ReadSchemaFields class to get the fields and some information about them - what type, mandatory/optional, etc.
Looking at your question/requirement to understand what you're exactly looking for, so we can answer the best possible and relevant.
Are you asking for "How can you write a generic code for component creation using core service?" instead of creating a component with a specific schema knowing all the fields upfront.
If that is what you are looking for, here is what you need to do:
You need to read the schema fields with CoreService (since you know the schema URI)
Now you know what type of fields (embedded/component link etc) you need to create content for
use the links pointed by "Puf" in his answer.
Please note that, if the field is marked as required in Tridion Schema you must have to fill a value and it has to match the field type defined in schema.
Reading schema fields via Core Service sample code can be found here
Updating a Component's field through the Core Service is already answered here: Updating Components using the Core Service in SDL Tridion 2011
That post points to a helper class you can find here: Updating Components using the Core Service in SDL Tridion 2011
If those don't help you in creating a Component, I suggest you post your code instead of asking us to write it for you.
We ask about use case, because code to fill in specific fields for a specific schema only works in one environment. Code that can automatically determine fields is re-usable.
If the use case is for an Tridion setup that has Inline Editing (Experience Manager or SiteEdit), then the correct approach is content/component types. These define a reference component with "junk defaults," instructions to the author, and even save location context.
If the use case is to allow authors the ability to create dummy components, this is out-of-the box with:
CTRL+C
CTRL+V
One-time setup required to create a "reference component." Of course we can mimic this behavior (in case "Copy of Untitled" isn't an appropriate name) by copying items with the core service.
In that case, I'll also do a copy--see a general solution for creating Tridion items using the Core Service.
Fields that require a default can have an actual default in the schema.
"Junk values" don't help authors much, always consider good defaults such as an appropriate selection or instructions in the case of fields (maybe). A 10 second change costs development practically nothing, but impacts all future components and the authors that create them.

Grails date binding

In Grails, one can 'bindData' in controller:
Book b = new Book()
bindData(b, params)
What if I have a date field with specific format (e.g. yyyy-MM-dd) from user input? In Spring, we can use registerCustomEditor(). How about Grails?
With Grails 1.1.1, you can implement a PropertyEditorRegistrar and use that to specify a format. See http://grails.1312388.n4.nabble.com/Grails-1-1-1-change-in-binding-date-properties-td1323105.html
Have you already come across the Extended Data Binding Plugin?
From the documentation on the site, it appears to offer both aspects which you are referring to
Allow customization of the DataBinder that will be used to parse user-defined input and populate objects (typically domain objects) with custom PropertyEditors on both application-wide and controller-specific levels.as String.
Extend controllers with dynamic methods to allow data binding and bean wrapping.

Resources