CryoFusion87
CryoFusion87

Reputation: 796

validating textboxes based on the URL entered

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.

Upvotes: 1

Views: 225

Answers (1)

William
William

Reputation: 221

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

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

Upvotes: 2

Related Questions