locked
Best place to trim my string properties RRS feed

  • Question

  • User-540818677 posted

    I am working on an asp.net MVC web application and I am using entity framework.

    Currently I have some model properties such as ip address, mac address, where I want to trim user input when adding , editing objects. Currently I am doing this inside an Ivalidatableobject as follow:-

     public class FirewallJoin :IValidatableObject
        {
            //code goes here
            public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
            {
              
                    if (!String.IsNullOrEmpty(IPAddress))
                    {
                        IPAddress= IPAddress.Trim();
                    }
    
                   //code goes here
    
                }
    
            }
         
        }

    But not sure if this is the right place to do my trim operation?

    Thanks

    Thursday, July 3, 2014 5:32 AM

Answers

  • User-2007877324 posted

    but is there a problem of doing the trimming on the Validate(ValidationContext validationContext) method , as i am doing now ?

    Technically no.

    But your function name says Validate, one expects that this function validates the inputs and may miss that it is changing the inputs (like removing the spaces). Personally I will have a another function which does the trimming.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 4, 2014 5:55 AM

All replies

  • User-2007877324 posted

    ModelBinder would be a right place for this. You can define a custom model binder and attach it to string. See this StackOverflow post for similar question

    http://stackoverflow.com/questions/1718501/asp-net-mvc-best-way-to-trim-strings-after-data-entry-should-i-create-a-custo

    Thursday, July 3, 2014 6:16 AM
  • User-540818677 posted

    ModelBinder would be a right place for this. You can define a custom model binder and attach it to string. See this StackOverflow post for similar question

    http://stackoverflow.com/questions/1718501/asp-net-mvc-best-way-to-trim-strings-after-data-entry-should-i-create-a-custo

    but using a custom model binder will trim all the strings, while in my case i need specific fields to be trimmed , and not all the strings fields...

    Thursday, July 3, 2014 6:29 AM
  • User-2007877324 posted

    In that case just trim those strings where you need it, like passing to the EntityFrameWork. You can even write a function which trims those strings.

    Thursday, July 3, 2014 6:34 AM
  • User-540818677 posted

    In that case just trim those strings where you need it, like passing to the EntityFrameWork. You can even write a function which trims those strings.

    but is there a problem of doing the trimming on the Validate(ValidationContext validationContext) method , as i am doing now ?

    Thanks

    Thursday, July 3, 2014 7:17 AM
  • User-2007877324 posted

    but is there a problem of doing the trimming on the Validate(ValidationContext validationContext) method , as i am doing now ?

    Technically no.

    But your function name says Validate, one expects that this function validates the inputs and may miss that it is changing the inputs (like removing the spaces). Personally I will have a another function which does the trimming.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 4, 2014 5:55 AM