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?
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 theMobileNumber
field because it supposes to benullable
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 usingif
and throwing anexception
or usingFluentValidation
.