2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BaseValidator.cs
index 0d0f19565c6fc123bc5b5091e8862c5c51ccb0d7..d8f3da1309737ebdf4cad33fa355c11f0b735a13 100755 (executable)
@@ -1,19 +1,40 @@
-/**
- * Namespace: System.Web.UI.WebControls
- * Class:     BaseValidator
- *
- * Authors:  Gaurav Vaish, Gonzalo Paniagua Javier
- * Maintainer: gvaish@iitk.ac.in
- * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>, <gonzalo@ximian.com>
- * Implementation: yes
- * Status:  80%
- *
- * (C) Gaurav Vaish (2001)
- * (c) 2002 Ximian, Inc. (http://www.ximian.com)
- */
+//
+// System.Web.UI.WebControls.BaseValidator.cs
+//
+// Authors:
+//   Gaurav Vaish (gvaish@iitk.ac.in)
+//   Gonzalo Paniagua Javier (gonzalo@ximian.com)
+//   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+//
+// (c) 2002 Ximian, Inc. (http://www.ximian.com)
+// (C) Gaurav Vaish (2002)
+// (C) 2003 Andreas Nahr
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 
 using System;
 using System.ComponentModel;
+using System.ComponentModel.Design;
 using System.Web;
 using System.Web.UI;
 using System.Drawing;
@@ -21,7 +42,7 @@ using System.Drawing;
 namespace System.Web.UI.WebControls
 {
        [DefaultProperty("ErrorMessage")]
-       //TODO: [Designer("??")]
+       [Designer ("System.Web.UI.Design.WebControls.BaseValidatorDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
        public abstract class BaseValidator: Label, IValidator
        {
                private bool isValid;
@@ -39,6 +60,9 @@ namespace System.Web.UI.WebControls
                        renderUplevel = false;
                }
 
+               [DefaultValue (""), WebCategory ("Behavior")]
+               [TypeConverter (typeof (ValidatedControlConverter))]
+               [WebSysDescription ("The ID of the control to validate.")]
                public string ControlToValidate
                {
                        get
@@ -56,6 +80,8 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+               [DefaultValue (typeof (ValidatorDisplay), "Static"), Bindable (true), WebCategory ("Appearance")]
+               [WebSysDescription ("Determines how the validator is displayed.")]
                public ValidatorDisplay Display
                {
                        get
@@ -73,10 +99,12 @@ namespace System.Web.UI.WebControls
                                {
                                        throw new ArgumentException();
                                }
-                               ViewState["ValidatorDisplay"] = value;
+                               ViewState["Display"] = value;
                        }
                }
 
+               [DefaultValue (true), WebCategory ("Behavior")]
+               [WebSysDescription ("Determines if client script is activated on uplevel browsers.")]
                public bool EnableClientScript
                {
                        get
@@ -108,6 +136,8 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+               [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
+               [WebSysDescription ("An error message that is displayed if the control validates to false.")]
                public string ErrorMessage
                {
                        get
@@ -125,6 +155,7 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+               [DefaultValue (null)]
                public override Color ForeColor
                {
                        get
@@ -137,6 +168,9 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+               [DefaultValue (true), Browsable (false), WebCategory ("Misc")]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               [WebSysDescription ("Indicates if the control validated to false.")]
                public bool IsValid
                {
                        get { return isValid; }
@@ -158,7 +192,9 @@ namespace System.Web.UI.WebControls
                        if(!Visible || (Visible && !Enabled))
                        {
                                IsValid = true;
+                               return;
                        }
+
                        Control ctrl = Parent;
                        while(ctrl != null)
                        {
@@ -239,7 +275,7 @@ namespace System.Web.UI.WebControls
 
                        if(enabled)
                        {
-                               Enabled = false;
+                               base.Enabled = false;
                        }
                }
 
@@ -269,7 +305,6 @@ namespace System.Web.UI.WebControls
                        return true;
                }
 
-               [MonoTODO]
                protected virtual bool DetermineRenderUplevel()
                {
                        Page page = Page;
@@ -280,11 +315,11 @@ namespace System.Web.UI.WebControls
 
                        if(EnableClientScript)
                        {
-                               // By now, return false
-                               //return false;
-                               throw new NotImplementedException();
-                               ////TODO: I need to get the (Browser->Dom_version_major >= 4 &&
-                               ////                         Brower->Ecma_script_version >= 1.2)
+                               if(page.Request.Browser.MSDomVersion.Major > 4)
+                               {
+                                       return page.Request.Browser.EcmaScriptVersion.CompareTo(new Version(1,2)) >= 0;
+                               }
+                               return false;
                        }
                        return false;
                }
@@ -307,12 +342,14 @@ namespace System.Web.UI.WebControls
                                PropertyDescriptor pd = GetValidationProperty(ctrl);
                                if(pd != null)
                                {
-                                       object item = pd.GetValue(ctrl);
-                                       if(item is ListItem)
-                                       {
-                                               return ((ListItem)item).Value;
-                                       }
-                                       return item.ToString();
+                                       object item = pd.GetValue (ctrl);
+                                       if (item is ListItem)
+                                               return ((ListItem) item).Value;
+
+                                       if (item == null)
+                                               return String.Empty;
+
+                                       return item.ToString ();
                                }
                        }
                        return null;
@@ -345,21 +382,29 @@ namespace System.Web.UI.WebControls
                        base.OnUnload(e);
                }
 
-               [MonoTODO("What_do_I_have_to_do")]
+               [MonoTODO("Damn_This_Is_Really_Frustrating___by_Gaurav")]
                protected void RegisterValidatorCommonScript()
                {
-                       // Keep going
-                       //throw new NotImplementedException();
+                       if(Page.ClientScript.IsClientScriptBlockRegistered("ValidatorIncludeScript"))
+                               return;
+                       
+                       //string jsDirectory = System.Web.UI.Utils.GetScriptLocation(Context);
+                       //string jsFile = jsDirectory + "/WebUIValidation.js";\r
+                        
+                       //TODO: Ok, now add the <script language="javascript"> etc
+                       //FIXME: Should I check for 'Explorer'? MS-Net seems to do it!
+                       throw new NotImplementedException();
                }
 
                [MonoTODO("I_have_to_know_javascript_for_this_I_know_it_but_for_ALL_browsers_NO")]
                protected virtual void RegisterValidatorDeclaration()
                {
-                       throw new NotImplementedException();
-                       //TODO: Since I have to access document.<ClientID> and register
-                       // as page validator. Now this is Browser dependent :((
-
-                       // This does not render anything on Mozilla, and does on IE
+                       //FIXME: How to make is more abstract?
+                       //Browser Info... but future browsers???
+                       //I'm confused! This will make it work, at least on IE
+                       string val = "document.all[\"" + ClientID;
+                       val += "\"]";
+                       Page.ClientScript.RegisterArrayDeclaration("Page_Validators", val);
                }
 
                [MonoTODO("Render_ing_always_left")]
@@ -385,17 +430,16 @@ namespace System.Web.UI.WebControls
 
                                ValidatorDisplay dis = Display;
                                if (RenderUplevel) {
-                                       //FIXME: by now, don't do client-side validation
+                                       //FIXME: as of now, don't do client-side validation
+                                       throw new NotImplementedException();
                                }
-                               
-                               if (!valid) {
+
+                               if (!valid && dis != ValidatorDisplay.None) {
                                        RenderBeginTag (writer);
-                                       if (Text.Trim ().Length > 0 || HasControls ()) {
+                                       if (Text.Trim ().Length > 0 || HasControls ())
                                                RenderContents (writer);
-                                               RenderEndTag (writer);
-                                       } else {
+                                       else
                                                writer.Write (ErrorMessage);
-                                       }
                                        RenderEndTag (writer);
                                        return;
                                }
@@ -407,4 +451,3 @@ namespace System.Web.UI.WebControls
                protected abstract bool EvaluateIsValid();
        }
 }
-