Using contentfield in a codedpart

Topics: General, Troubleshooting
Feb 15, 2011 at 10:07 AM

I've a situation where i want to use a contentfield in a part that exists in code. I could do that in the migration to add the contentfield as dynamic property using .WithField(...).WithSettings(...)
But... That way i can't use any Validation attributes like [Required] or [Regex(myMinimaldateRegex)]

So i see 2 possible solutions

1) adding the field in code to the part

using MyFieldNamespace;

public class MyPart : ContentPart<MyPart> {
   public string Name {get; set;}
   public DateTimeField {get; set;}

but i wonder if this works, because i can't set the settings this way (date only setting for example)

2) Adding validation attributes dynamicly to the dynamic field

Any idea's ??

I would like to use contentfields because of the functionality they provide, the conceptual idea seems so nice to me.

Feb 15, 2011 at 6:54 PM

Why does it have to be a field?

Also, the validation parameters should be settings on the field. If the field you want to use doesn't have such options, build your own.

Feb 15, 2011 at 7:09 PM

A field because i like the basic idea of a field.
It would be nice to provide a suite of fields for parts to use

In our situation we're (going) to generate everything from a custom tool that defines to xml and that xml is going to be parsed with t4 to modules, controllers, drivers, handlers, parts, models, viewmodels, viewtemplates, etc, etc....
It is imho not an option to generate fields, they can be complex and the idea is to provide them to the custom tool to be used in a viewtemplate.
On the other side there is a business rule engine with rules, again defined in the custom tool, that are mostly going to be DataAnnotation's and custom validations.
Attaching DataAnnotations to a field or property seems like a good idea to me, except that fields aren't useable in code??

The're other solutions i've in mind:

- using another ModelBinder (noone replied...) where modelerrors are added, including validations for the field..
- create a htmlhelper foreach field and use the field that way
- using the [Shape] attribute above a function
- or just regenerate the stuff over and over..

Feb 15, 2011 at 7:41 PM

So you did not consider adding the settings that you need to the field types?

Feb 15, 2011 at 7:46 PM

You mean write a field that parses it settings and adds generate them along in html, jQuery etc?
A field would act differendly in another part, being required in one part and in the other part it won't be

.WithPart("MyPart").WithField(MyField).WithSettings("required", "true") for example

I did consider it, it would mean that we can only use own writen fields then.

My question was just if it would be possible to use a field declared in code as in 1) adding the field in code to the part

Feb 15, 2011 at 8:19 PM

Yes, that's what I meant. Generic validation would be hard to do as we as a framework have no idea what's in there. It really should be the responsibility of the author of the field type. Even if it's not your own field, depending on the license, you should be able to improve existing ones.

Feb 15, 2011 at 8:48 PM

perhaps, but i think it won't be easy then.
For example a DateTimeField is nothing more then a input element with javascript, provided by jQuery, to make it al fancy.
Normally i would use a DisplayFormat attribute above the property so mvc build-in support renders javascript along with the input including data-val attributes.

Somehow that mvc build-in support should still be rendered if there is a input being rendered, how should the orchard field declare that, any idea?

Feb 15, 2011 at 8:56 PM

Ofcourse i could add an own DataAnnotationsModelValidatorProvider and register own validators foreach custom orchardfield. No idea if it would work, it's just an idea.. keeping client side validation for the future in mind.