Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Custom ValidatorAttribute does not work

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Custom ValidatorAttribute does not work

    I am not able to make a custom ValidatorAttribute work for a property. This is a short version of my code:

    Code:
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
    sealed public class TestThisAttribute : ValidationAttribute
    {
       public TestThisAttribute()
       {
           NinjaScript.Log("TestThisAttribute::TestAttribute: constructor", LogLevel.Information);
       }
    
       public override bool RequiresValidationContext { get { return true; } }
    
       protected override ValidationResult IsValid(object value, ValidationContext validationContext)
       {
          NinjaScript.Log("TestThisAttribute::IsValid: called", LogLevel.Information);
          return new ValidationResult("Input invalid");
       }
    }
    
    
    [NinjaScriptProperty]
    [TestThis]
    [Display(ResourceType=typeof(Resource), Name="Test integer", GroupName="Test", Order=0)]
    public int testInt { get; set; }
    I can see that the constructor is being called from the log, but the IsValid method is never called. If it were, it would say that the input is not valid.

    Any idea on what I need to do to make a custom ValidationAttribute work for a property in NinjaTrader 8?
    Last edited by Zombie; 02-01-2021, 04:15 PM.

    #2
    Hello Zombie, thanks for your post.

    This topic falls outside of the realm of what our support team is able to assist with. This thread will remain open for any community members to assist. We have a collection of TypeConverters here that you may reference and see if you can find a solution from one of these:

    https://ninjatrader.com/support/help...r_to_custo.htm

    Best regards.

    Comment


      #3
      Hi Chris,

      thanks for the reply. My question is because the code is theoretically correct and follows the guidelines for implementing a custom ValidationAttribute.

      I am just wondering if there is something else required from the NT8 side to make it work.

      Comment


        #4
        Hello Zombie, thanks for your reply.

        We have never used this kind of property before, and there are no existing examples on how it would work within the platform so trial and error testing would be required to get it to work in the context of NinjaTrader.

        Please let me know if I can assist any further.

        Comment


          #5
          Thanks NinjaTrader_ChrisL

          Comment


            #6
            NinjaTrader_ChrisL

            I noticed that even built-in validation attributes from System.ComponentModesl.DataAnnotations are not working, except for Range.

            For instance, the code below should make sure that the string is non-null and its length does not exceed 5 characters, but the UI completely ignores both requirements:

            Code:
            #region Properties
            
            [Required]
            [StringLength(5, ErrorMessage = "The string value cannot exceed 5 characters. ")]
            [Display(ResourceType=typeof(Resource), Name="String test", GroupName="NinjaScriptSetup", Order=0)]
            public string StringTest { get; set; }
            
            #endregion
            Is this expected?

            Comment


              #7
              Hello Zombie, thanks for your reply.

              I apologize, but can not speculate. This item is beyond the scope of support I am able to provide, so I am not able to go further into testing it or getting it to work within the context of a NinjaScript object.

              Kind regards.

              Comment


                #8
                Hello Zombie: Did you solve this problem about validation string?

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                571 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                330 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                548 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                549 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X