1

I have a form in which I want to disable the required validation on one of the @Html.TextBox elements. The front end is as follows:

@model Models.NewUser

@using (Html.BeginForm("Register", "Account"))
{
    <div class="field">
        <span class="textOverField absolute em1-2" style="top:6px;left:4px;">Key</span>
        @Html.TextBox("SchoolGroupKey", "", new { @class = "textField" }) 
    </div>
    <div class="field">
        <span class="textOverField absolute em1-2" style="top:6px;left:4px;">Name</span>
         @Html.TextBox("Names", "", new { @class = "textField" }) <div class="absolute validationMessage">@Html.ValidationMessageFor(u => u.Names)</div>
    </div>
    <div class="field">
        <span class="textOverField absolute em1-2" style="top:6px;left:4px;">Last Name</span>
        @Html.TextBox("LastNames", "", new { @class = "textField" }) <div class="absolute validationMessage">@Html.ValidationMessageFor(u => u.LastNames)</div>
    </div>
    <div class="em0-9" style="margin-bottom:20px;">
        <input class="submitButton cursorHand" style="margin-top:10px;" type="submit" value="Registrarme" />
    </div>
}

And the code for the model is as follows:

public class NewUser
{
    [Required(ErrorMessage = "Name required")]
    public string Names { get; set; }

    [Required(ErrorMessage = "Last name required")]
    public string LastNames { get; set; }

    public System.Guid SchoolGroupKey { get; set; }
}

I'm guessing that the @Html.TextBox has a default validation even though I don't specify it. Is there someway to remove it?, I tried removing the html tags data-val-required and data-val, but still no results.

Thanks

1 Answer 1

1

Guid is a structure that behave as a simple type (just like int or DateTime). As such it is automaticly validated. If you want to make a workaround change your property type from Guid to nullable Guid?. Example:

public System.Guid? SchoolGroupKey { get; set; }

Of course you will have to change places when you use this property, to access the guid value throught Value property of nullable type.

If the mapping fails you will not get any validation errors then - just null value inside of your SchoolGroupKey property.

Sign up to request clarification or add additional context in comments.

6 Comments

It shouldn't be complaining about SchoolGroupKey, since it is not marked as required. It should just pass a new Guid to the action method.
Did you ever tried to pass wrong value into simple type like int or Guid? Of course it will return a validation error.. That's the diffrence between mapping those types and referenced ones..
Never thought about it being nullable in the model, let me check it and get back to you if this works
@triangulito if you don't want to use a Nullable<Guid> you can capture the Guid as a string and then validate it in the controller or domain service.
@Xander - true, but with Guid? he does not have to handle Guid parsing on his own..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.