1

I am trying to get a model property validation to work but allowing a null value in a string property.

The Property i'm trying to validate is a:

public string PhoneNumber { get; set;}

And i am validating it like this:

[Phone(ErrorMessage = "Invalid telephone number.")]
public string PhoneNumber { get; set;}

The validation works great in my case except for when a value is not sent in for Phone number, to the api with an object.

is there any thing like a: [AllowNullValue] attribute or how do i get null values pass the "Phone" attribute?

UPDATE (Anton Gorbunov's post):

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "The  field is not a valid phone number.",
    "ExceptionType": "System.ComponentModel.DataAnnotations.ValidationException",
    "StackTrace": "   at System.ComponentModel.DataAnnotations.ValidationAttribute.Validate(Object value, String name)\r\n   at RABE_BCV_API.Controllers.APIController.UpsertMember(MemberModel memberObject) in C:\\Users\\John\\Documents\\Visual Studio 2015\\Projects\\RABE_BCV_API\\RABE_BCV_API\\Controllers\\APIController.cs:line 29\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
H4p7ic
  • 1,669
  • 2
  • 32
  • 61

1 Answers1

0

It looks strange, because is the first instruction of IsValid Metod in PhoneAttribute is:

public override bool IsValid(object value) {
            if (value == null) {
                return true;
            }
...
}

Maybe you use [Required] or [BindRequired] attributes?

Anton Gorbunov
  • 1,414
  • 3
  • 16
  • 24
  • Where did you find that? when i right click and go to definition my attribute doesn't say that... it only states "public override bool IsValid(object value);" – H4p7ic Nov 21 '17 at 08:54
  • I set checkbox `Enable .NET Framework sources stepping` In `Tools -> Options -> Debugging -> General`. Create Instance of `PhoneAttribute` and explicit call `Validate(object, string)` method and Step into in debugger – Anton Gorbunov Nov 21 '17 at 09:20
  • May you give an example, how you use model into UpsertMember method/action? – Anton Gorbunov Nov 21 '17 at 10:32