Customer Gender - Required

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
How can we force the Customer Gender (Female or Male) to be a REQUIRED field while registering or editing?

Note: We are using nopCommerce (no-source-code version) ver 4.0 so changes must be done within the cshtml pages.
5 years ago
Without changing the code you can not display a validation message if not selected
Best you can do is make the field required and display the * near the field
In the register.cshtml (similarly in info.cshtml & _CreateOrUpdate.Info.cshtml) you could change the code to
    @if (Model.GenderEnabled)
    {
    <div class="inputs">
        <label>@T("Account.Fields.Gender"):</label>
        <div class="gender">
            <span class="male">
                <input type="radio" asp-for="Gender" value="M" checked="@(Model.Gender == "M")" id="gender-male" required/>
                <label class="forcheckbox" for="gender-male">@T("Account.Fields.Gender.Male")</label>
            </span>
            <span class="female">
                <input type="radio" asp-for="Gender" value="F" checked="@(Model.Gender == "F")" id="gender-female" />
                <label class="forcheckbox" for="gender-female">@T("Account.Fields.Gender.Female")</label>
            </span>
            <nop-required />
        </div>
    </div>
    }
5 years ago
Thanks YIDNA, it worked however and in order to have it perfectly, is there a way to display a message notifying about the required like in the email "Email is required" say in this case... "Gender is required" ?
Where can we specify that text on the input or validation field?


Yidna wrote:
Without changing the code you can not display a validation message if not selected
Best you can do is make the field required and display the * near the field
In the register.cshtml (similarly in info.cshtml & _CreateOrUpdate.Info.cshtml) you could change the code to
    @if (Model.GenderEnabled)
    {
    <div class="inputs">
        <label>@T("Account.Fields.Gender"):</label>
        <div class="gender">
            <span class="male">
                <input type="radio" asp-for="Gender" value="M" checked="@(Model.Gender == "M")" id="gender-male" required/>
                <label class="forcheckbox" for="gender-male">@T("Account.Fields.Gender.Male")</label>
            </span>
            <span class="female">
                <input type="radio" asp-for="Gender" value="F" checked="@(Model.Gender == "F")" id="gender-female" />
                <label class="forcheckbox" for="gender-female">@T("Account.Fields.Gender.Female")</label>
            </span>
            <nop-required />
        </div>
    </div>
    }
5 years ago
Following Yidna solution I was able to almost finish it an even more... make it similar to the other fields with this lines:

    <div class="col-sm-4 col-md-4 col-xs-12">
       <div class="visible-lg visible-md visible-sm form-title-rquired"><nop-required /></div>
       <span asp-validation-for="Gender"></span>
    </div>

However, the only thing that is not working is that the validation text displayed when you do not select either Male or Female.... is "This field is required" instead of "Gender is required".

What is missing on my validation declaration?
I would like that field validation to display "Gender is required"


Yidna wrote:
Without changing the code you can not display a validation message if not selected
Best you can do is make the field required and display the * near the field
In the register.cshtml (similarly in info.cshtml & _CreateOrUpdate.Info.cshtml) you could change the code to
    @if (Model.GenderEnabled)
    {
    <div class="inputs">
        <label>@T("Account.Fields.Gender"):</label>
        <div class="gender">
            <span class="male">
                <input type="radio" asp-for="Gender" value="M" checked="@(Model.Gender == "M")" id="gender-male" required/>
                <label class="forcheckbox" for="gender-male">@T("Account.Fields.Gender.Male")</label>
            </span>
            <span class="female">
                <input type="radio" asp-for="Gender" value="F" checked="@(Model.Gender == "F")" id="gender-female" />
                <label class="forcheckbox" for="gender-female">@T("Account.Fields.Gender.Female")</label>
            </span>
            <nop-required />
        </div>
    </div>
    }
5 years ago
Another option would be in Customer settings (in Customer form field tab) to disable the standard "Gender" field and create a Custom customer attribute "Gender" which can be set as Required
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.