I've created a custom Image Field Template which works fine. I decided to create some kind of validation for image sizes so I've created a custom validation attribute. here is the code:
public class ImageSizeValidator : ValidationAttribute
{
public long MaxSize { get; set; }
public long MinSize { get; set; }
public override bool IsValid(object value)
{
if (value == null)
return true;
byte[] image = (byte[]) value;
if (image.Length / 1024L > MaxSize || image.Length / 1024L < MinSize)
return false;
return true;
}
}
public partial class Image_Edit : System.Web.DynamicData.FieldTemplateUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SetUpValidator(RequiredFieldValidator1);
SetUpValidator(DynamicValidator1);
}
protected override void ExtractValues(IOrderedDictionary dictionary)
{
if (FileUpload1.PostedFile == null || String.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream.Length == 0)
return;
dictionary[Column.Name] = FileUpload1.FileBytes;
}
public override Control DataControl
{
get
{
return FileUpload1;
}
}
}
There are two problems: 1- when IsValid(object value) called value is always null! 2- when trying to upload a new image I always get "The value is not valid" Error on client side of validation.
In my mind,sides checking the value is null or not,you should save the file posted onto the server and then show it。
The reason why your object is always null is that the file won't be submitted until you click a submit button or something like this……
Please debug your app and check whether your FileBytes is null or not。And check whether the colum!n.Name is fitable or not(my suggestionn is that you can use a fixed string instead of the column.Name instead)。
mani47
0 Points
1 Post
Validate Custom Image FieldTemplate
Jan 13, 2012 01:58 PM|LINK
I've created a custom Image Field Template which works fine. I decided to create some kind of validation for image sizes so I've created a custom validation attribute. here is the code:
public class ImageSizeValidator : ValidationAttribute { public long MaxSize { get; set; } public long MinSize { get; set; } public override bool IsValid(object value) { if (value == null) return true; byte[] image = (byte[]) value; if (image.Length / 1024L > MaxSize || image.Length / 1024L < MinSize) return false; return true; } }public partial class Image_Edit : System.Web.DynamicData.FieldTemplateUserControl { protected void Page_Load(object sender, EventArgs e) { SetUpValidator(RequiredFieldValidator1); SetUpValidator(DynamicValidator1); } protected override void ExtractValues(IOrderedDictionary dictionary) { if (FileUpload1.PostedFile == null || String.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream.Length == 0) return; dictionary[Column.Name] = FileUpload1.FileBytes; } public override Control DataControl { get { return FileUpload1; } } }There are two problems:
1- when IsValid(object value) called value is always null!
2- when trying to upload a new image I always get "The value is not valid" Error on client side of validation.
by the way here is my entity metadata code:
[Display(Name = "ServicePhoto", Order = 410, GroupName = "Misc")] [HideColumnIn(PageTemplate.List)] [UIHint("Image")] [ImageSizeValidator(ErrorMessage = "Invalid Photo Size", MinSize = 30, MaxSize = 300)] public object ServicePhoto { get; set; }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Validate Custom Image FieldTemplate
Jan 15, 2012 02:57 AM|LINK
Hello:)
In my mind,sides checking the value is null or not,you should save the file posted onto the server and then show it。
The reason why your object is always null is that the file won't be submitted until you click a submit button or something like this……
Please debug your app and check whether your FileBytes is null or not。And check whether the colum!n.Name is fitable or not(my suggestionn is that you can use a fixed string instead of the column.Name instead)。
Regurads!