Skip to content

Commit

Permalink
DataAnnotationsModelValidator.cs
Browse files Browse the repository at this point in the history
 ! FormatErrorMessage is not implemented in mono: Creating static strings
 ! RequiredAttribute is not implemented in mono: Implemented for objects and strings

Html/DefaultDisplayTemplates.cs
 ! System.Data.Entity is unavailable: removed modeltype check

Html/DefaultEditorTemplates.cs
 * Binary.operator== throws error: fixed
 ! System.Data.Entity is unavailable: removed modeltype check

Html/InputExtensions.cs
 * Binary.operator== throws error: fixed

ParamterInfoUtil.cs
 - RawDefaultValue is not implemented: returns null instead of default value

TypeDescriptorHelper.cs
 * AssociatedMetadataTypeTypeDescriptionProvider is unavailable in mono 2.4.4: backported from mono 2.6
  • Loading branch information
sztupy committed Apr 12, 2010
1 parent ae263c9 commit ffc6ece
Show file tree
Hide file tree
Showing 6 changed files with 658 additions and 244 deletions.
20 changes: 19 additions & 1 deletion Mvc/DataAnnotationsModelValidator.cs
Expand Up @@ -30,7 +30,9 @@ public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext c

protected internal string ErrorMessage {
get {
return Attribute.FormatErrorMessage(Metadata.GetDisplayName());
// FormatErrorMessage is not implemented in mono
return "Validation error (" + Attribute.GetType().ToString() + "): " + Metadata.GetDisplayName();
//return Attribute.FormatErrorMessage(Metadata.GetDisplayName());
}
}

Expand All @@ -45,6 +47,21 @@ public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext c
}

public override IEnumerable<ModelValidationResult> Validate(object container) {
// RequiredAttribute is not implemented in mono
// This is a small workaround that might work for some and might break for a lot of objects
if (IsRequired) {
if (ReferenceEquals(Metadata.Model,null)) {
yield return new ModelValidationResult
{
Message = ErrorMessage + " is null"
};
} else if (Metadata.Model.ToString() == "") {
yield return new ModelValidationResult
{
Message = ErrorMessage + " is empty"
};
}
} else
if (!Attribute.IsValid(Metadata.Model)) {
yield return new ModelValidationResult {
Message = ErrorMessage
Expand All @@ -53,3 +70,4 @@ public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext c
}
}
}

4 changes: 3 additions & 1 deletion Mvc/Html/DefaultDisplayTemplates.cs
Expand Up @@ -185,7 +185,8 @@ internal static class DefaultDisplayTemplates {
private static bool ShouldShow(ModelMetadata metadata, TemplateInfo templateInfo) {
return
metadata.ShowForDisplay
&& metadata.ModelType != typeof(EntityState)
// MONO: System.Data.Entity is unavailable
// && metadata.ModelType != typeof(EntityState)
&& !metadata.IsComplexType
&& !templateInfo.Visited(metadata);
}
Expand All @@ -202,3 +203,4 @@ internal static class DefaultDisplayTemplates {
}
}
}

6 changes: 4 additions & 2 deletions Mvc/Html/DefaultEditorTemplates.cs
Expand Up @@ -119,7 +119,7 @@ internal static class DefaultEditorTemplates {
object model = html.ViewContext.ViewData.Model;

Binary modelAsBinary = model as Binary;
if (modelAsBinary != null) {
if (((System.Object)modelAsBinary) != null) {
model = Convert.ToBase64String(modelAsBinary.ToArray());
}
else {
Expand Down Expand Up @@ -191,7 +191,8 @@ internal static class DefaultEditorTemplates {
private static bool ShouldShow(ModelMetadata metadata, TemplateInfo templateInfo) {
return
metadata.ShowForEdit
&& metadata.ModelType != typeof(EntityState)
// EntityState is unavailable on mono
//&& metadata.ModelType != typeof(EntityState)
&& !metadata.IsComplexType
&& !templateInfo.Visited(metadata);
}
Expand All @@ -211,3 +212,4 @@ internal static class DefaultEditorTemplates {
}
}
}

3 changes: 2 additions & 1 deletion Mvc/Html/InputExtensions.cs
Expand Up @@ -132,7 +132,7 @@ public static class InputExtensions {

private static MvcHtmlString HiddenHelper(HtmlHelper htmlHelper, object value, bool useViewData, string expression, IDictionary<string, object> htmlAttributes) {
Binary binaryValue = value as Binary;
if (binaryValue != null) {
if (((System.Object)binaryValue) != null) {
value = binaryValue.ToArray();
}

Expand Down Expand Up @@ -396,3 +396,4 @@ public static class InputExtensions {
}
}
}

9 changes: 8 additions & 1 deletion Mvc/ParameterInfoUtil.cs
Expand Up @@ -20,7 +20,13 @@ internal static class ParameterInfoUtil {
public static bool TryGetDefaultValue(ParameterInfo parameterInfo, out object value) {
// this will get the default value as seen by the VB / C# compilers
// if no value was baked in, RawDefaultValue returns DBNull.Value
object rawDefaultValue = parameterInfo.RawDefaultValue;
object rawDefaultValue = null;
try {
rawDefaultValue = parameterInfo.RawDefaultValue;
}
catch (NotImplementedException) {
rawDefaultValue = null;
}
if (rawDefaultValue != DBNull.Value) {
value = rawDefaultValue;
return true;
Expand All @@ -40,3 +46,4 @@ internal static class ParameterInfoUtil {

}
}

0 comments on commit ffc6ece

Please sign in to comment.