Validate Custom Image FieldTemplatehttp://forums.asp.net/t/1758940.aspx/1?Validate+Custom+Image+FieldTemplateSun, 15 Jan 2012 02:57:14 -050017589404781268http://forums.asp.net/p/1758940/4781268.aspx/1?Validate+Custom+Image+FieldTemplateValidate Custom Image FieldTemplate <p>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:<br> <br> </p> <pre class="prettyprint">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 &gt; MaxSize || image.Length / 1024L &lt; MinSize) return false; return true; } }</pre> <pre class="prettyprint">&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Image_Edit.ascx.cs" Inherits="RastinArgham.CRM.Web.DynamicData.FieldTemplates.Image_Edit" %&gt; &lt;asp:FileUpload ID="FileUpload1" runat="server" OnDataBinding="FileUpload1DataBinding" /&gt; &lt;asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="DDControl DDValidator" ControlToValidate="FileUpload1" Display="Static" Enabled="false" /&gt; &lt;asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="FileUpload1" Display="Static" /&gt;<br /><br /><br /></pre> <pre class="prettyprint">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; } } } </pre> <p><br /><br /><br /><br />There are two problems:<br />1- when IsValid(object value) called value is always null!<br />2- when trying to upload a new image I always get "The value is not valid" Error on client side of validation.<br /><br />by the way here is my entity metadata code:<br /><br /></p> <pre class="prettyprint">[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; } </pre> 2012-01-13T13:58:33-05:004782795http://forums.asp.net/p/1758940/4782795.aspx/1?Re+Validate+Custom+Image+FieldTemplateRe: Validate Custom Image FieldTemplate <p>Hello</p> <p>In my mindsides checking the value is null or notyou should save the file posted onto the server and then show it</p> <p>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</p> <p>Please debug your app and check whether your FileBytes is null or notAnd check whether the column.Name is fitable or notmy suggestionn is that you can use a fixed string instead of the column.Name instead</p> <p>Regurads</p> <pre class="prettyprint"></pre> 2012-01-15T02:57:14-05:00