1

I am trying to validate two textboxes based on the URL that has been entered, the textboxes are for job title and military rank.

Generally for most of the routes I have created the job title textbox will be required only three URLs currently require military rank instead, in similar projects I have added a RequiredIf annotation using MVC Foolproof Validation which has been added to a view model, in the case I have tried using the annotation but because I am validating from a domain model project the Foolproof Validation is not working.

I have currently partially implemented custom validation in the domain model as a handler class using this code:

 if (paymentDetailsForm.UseRankInsteadOfJobTitle)
        {
            if (paymentDetailsForm.Rank == null)
            {
                yield return new ValidationResult(Resources.JobRankRequired);
            }
        }

essentially I have created a Boolean value called UseRankInsteadOfJobTitle which is set to either true or false depending on the URL entered.

to set this value as either true or false I have used this code in the controller:

if ((programme.Code == "AMAC") || (programme.Code == "AMACD") || (programme.Code == "AMACR"))
        {
            ViewData["UseRankInsteadOfJobTitle"] = true;
        }
        else
        {
            ViewData["UseRankInsteadOfJobTitle"] = false;
        }

And then created a hidden field on the view:

@Html.Hidden("UseRankInsteadOfJobTitle", @ViewData["UseRankInsteadOfJobTitle"])

the following HTML is then generated:

for false values

<input data-val="true" data-val-required="The Boolean field is required." id="UseRankInsteadOfJobTitle" name="UseRankInsteadOfJobTitle" type="hidden" value="False" />

for true values

<input data-val="true" data-val-required="The Boolean field is required." id="UseRankInsteadOfJobTitle" name="UseRankInsteadOfJobTitle" type="hidden" value="True" /> 

Currently the validation message is not being displayed for either job title or military rank, I need a way of ascertaining whether the value of USERankInsteadOfJobTitle is true or false.

I have tried using the formCollection class to get the value of the hidden field but so far this has not worked.

Any advice on how I could do this or better way of validating these textboxes would be appreciated.

CryoFusion87
  • 796
  • 1
  • 8
  • 28

1 Answers1

2

Have you tried using the Request["Fieldname"]?

Maybe something like someBoolean = Request["UseRankInsteadOfJobTitle"]?

William
  • 221
  • 1
  • 8
  • having got it working I was wondering if since I am now not using data annotations, is it possible to display an asterix or change the colour of a field to denote that it is required? The helper that I am using relies on the required data annotation to display these. – CryoFusion87 Jan 08 '13 at 12:02
  • I'm not sure how that would work tbh. You could leave the annotations just for that purpose, or you could look into changing the css of the field in your code. Such as on the post back you could have TextBox.BackColor = Color. – William Jan 08 '13 at 12:45
  • the only problem with annotating them as required is that it overrides the custom validation making them both required regardless of the URL entered. Plus I only want the asterix to display if the correct URL is entered – CryoFusion87 Jan 08 '13 at 13:01
  • How about putting an empty label next to the field. Then add an asterix when you validate the field value? – William Jan 08 '13 at 13:22