net core handling form submitting in two validation ways

60 views Asked by At

I'm using razor page to build my application and i'm stucking with a situation in form validation that is :
i have create a one page that contains many input fields,
one of them is mobile number and for this field it should be maximum 10 digits and it is required,

and for that form, there are two buttons,
one is for submitting the form only if all the fields is valid,
and the second button is for 'save for later', here also all fields must be valid except the required attribute it's optional, i mean for mobile number field, it's okay if it is empty but it should display an error if it exceed 10 digits.

i know i can use different pages with different view models and validation but i want to handle it in one page.

so what is the best way to handle this situation?

1

There are 1 answers

0
iman safari On

You can help me to give a better answer if you share your model and controllers. But at first touch, I think you have to remove the Required attribute for the MobileNumber field because it supposes to be nullable in your database.

After removing the Required attribute, try to handle the null check manually in your first scenario, you have a bunch of solutions, and one of the simple solutions is using if and throwing an exception or using FluentValidation.