Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

16.9 KB
Raw Blame
@inject IEnumTranslationService EnumTranslationService;
@model AddressModel
@{
    var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    // the view-model this form binds to; the JSON island at the bottom publishes
    // it under the same name, and the island below declares it
    var addressVmName = $"createorupdateaddress{prefix}";
}
<input asp-for="Id" type="hidden"/>
@*
    Deliberately NOT an island, however tempting: <validation-observer> wraps this
    partial from the host view, and the <validation-provider>s are inside it.
    Making this element an island puts the providers in a separate Vue app, and
    provide/inject does not cross apps - the observer then registers zero fields
    and handleSubmit reports valid whatever the form contains. It submitted an
    empty required address that way.

    So the `createorupdateaddress<prefix>` view-model has to be declared on
    whichever island already contains the observer, in the host view.
*@
<div class="edit-address row">
    @if (Model.NameEnabled)
    {
        <div class="form-group col-sm-12 col-md-12">
            <label asp-for="Name" class="col-form-label">@Loc["Address.Fields.Name"]:</label>
            <input asp-for="Name" v-model="createorupdateaddress@{@prefix}.Name" v-bind:class="['form-control']"/>
        </div>
    }
    <div class="form-group col-sm-12 col-md-6">
        <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
            <label asp-for="FirstName" class="col-form-label">@Loc["Address.Fields.FirstName"]:</label>
            <span class="required">*</span>
            <input asp-for="FirstName" v-model="createorupdateaddress@{@prefix}.FirstName" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.FirstName.Required"]/>
            <span class="field-validation-error">{{ errors[0] }}</span>
        </validation-provider>
    </div>
    <div class="form-group col-sm-12 col-md-6">
        <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
            <label asp-for="LastName" class="col-form-label">@Loc["Address.Fields.LastName"]:</label>
            <span class="required">*</span>
            <input asp-for="LastName" v-model="createorupdateaddress@{@prefix}.LastName" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.LastName.Required"]/>
            <span class="field-validation-error">{{ errors[0] }}</span>
        </validation-provider>
    </div>
    <div class="form-group col-sm-12">
        @* Same rule as Account/Info: do not validate the format of a field the
           visitor cannot edit - it could only block the form, never help. *@
        <validation-provider tag="div" rules="@(Model.DisallowUsersToChangeEmail ? "required" : "required|email")" v-slot="{ errors, classes }">
            <label asp-for="Email" class="col-form-label">@Loc["Address.Fields.Email"]:</label>
            <span class="required">*</span>
            <input asp-for="Email" v-model="createorupdateaddress@{@prefix}.Email" v-bind:class="[classes , 'form-control']" asp-readonly="@(Model.DisallowUsersToChangeEmail)" data-val-required=@Loc["Address.Fields.Email.Required"] data-val-email=@Loc["Common.WrongEmail"]/>
            <span class="field-validation-error">{{ errors[0] }}</span>
        </validation-provider>
    </div>
    @if (Model.CompanyEnabled)
    {
        <div class="form-group col-sm-12">
            @if (Model.CompanyRequired)
            {
                <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                    <label asp-for="Company" class="col-form-label">@Loc["Address.Fields.Company"]:</label>
                    <span class="required">*</span>
                    <input asp-for="Company" v-model="createorupdateaddress@{@prefix}.Company" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.Company.Required"]/>
                    <span class="field-validation-error">{{ errors[0] }}</span>
                </validation-provider>
            }
            else
            {
                <label asp-for="Company" class="col-form-label">@Loc["Address.Fields.Company"]:</label>
                <input asp-for="Company" class="form-control"/>
            }
        </div>
    }
    @if (Model.VatNumberEnabled)
    {
        <div class="form-group col-sm-12">
            @if (Model.VatNumberRequired)
            {
                <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                    <label asp-for="VatNumber" class="col-form-label">@Loc["Address.Fields.VatNumber"]:</label>
                    <span class="required">*</span>
                    <input asp-for="VatNumber" v-model="createorupdateaddress@{@prefix}.VatNumber" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Account.Fields.VatNumber.Required"]/>
                    <span class="field-validation-error">{{ errors[0] }}</span>
                </validation-provider>
            }
            else
            {
                <label asp-for="VatNumber" class="col-form-label">@Loc["Address.Fields.VatNumber"]:</label>
                <input asp-for="VatNumber" class="form-control"/>
            }
        </div>
    }
    @if (Model.CountryEnabled)
    {
        <div class="form-group col-sm-12 col-md-6">
            <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                <label asp-for="CountryId" class="col-form-label">@Loc["Address.Fields.Country"]:</label>
                <span class="required">*</span>
                <select asp-for="CountryId" asp-items="Model.AvailableCountries" data-val-required=@Loc["Address.Fields.Country.Required"] v-bind:class="[classes , 'form-control form-select']" v-model="createorupdateaddress@{@prefix}.CountryId">
                </select>
                <span class="field-validation-error">{{ errors[0] }}</span>
            </validation-provider>
        </div>
    }

    @if (Model.CountryEnabled && Model.StateProvinceEnabled)
    {
        <div class="form-group col-sm-12 col-md-6">
            <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                <label asp-for="StateProvinceId" class="col-form-label">@Loc["Address.Fields.StateProvince"]:</label>
                <span class="required">*</span>
                <select asp-for="StateProvinceId" data-val-required="@Loc["Address.Fields.StateProvince.Required"]" v-bind:class="[classes , 'form-control form-select']" v-model="createorupdateaddress@{@prefix}.StateProvinceId">
                    <option v-for="s in createorupdateaddress@{@prefix}.AvailableStates" :key="s.id" :value="s.id">{{s.name}}</option>
                </select>
                <span class="field-validation-error">{{ errors[0] }}</span>
            </validation-provider>
        </div>
    }
    @if (Model.CityEnabled)
    {
        <div class="form-group col-sm-12">
            @if (Model.CityRequired)
            {
                <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                    <label asp-for="City" class="col-form-label">@Loc["Address.Fields.City"]:</label>
                    <span class="required">*</span>
                    <input asp-for="City" v-model="createorupdateaddress@{@prefix}.City" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.City.Required"]/>
                    <span class="field-validation-error">{{ errors[0] }}</span>
                </validation-provider>
            }
            else
            {
                <label asp-for="City" class="col-form-label">@Loc["Address.Fields.City"]:</label>
                <input asp-for="City" class="form-control"/>
            }
            <span asp-validation-for="City"></span>
        </div>
    }
    @if (Model.StreetAddressEnabled)
    {
        <div class="form-group col-sm-12 col-md-6">
            @if (Model.StreetAddressRequired)
            {
                <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                    <label asp-for="Address1" class="col-form-label">@Loc["Address.Fields.Address1"]:</label>
                    <span class="required">*</span>
                    <input asp-for="Address1" v-model="createorupdateaddress@{@prefix}.Address1" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.Address1.Required"]/>
                    <span class="field-validation-error">{{ errors[0] }}</span>
                </validation-provider>
            }
            else
            {
                <label asp-for="Address1" class="col-form-label">@Loc["Address.Fields.Address1"]:</label>
                <input asp-for="Address1" class="form-control"/>
            }
        </div>
    }
    @if (Model.StreetAddress2Enabled)
    {
        <div class="form-group col-sm-12 col-md-6">
            @if (Model.StreetAddress2Required)
            {
                <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                    <label asp-for="Address2" class="col-form-label">@Loc["Address.Fields.Address2"]:</label>
                    <span class="required">*</span>
                    <input asp-for="Address2" v-model="createorupdateaddress@{@prefix}.Address2" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.Address2.Required"]/>
                    <span class="field-validation-error">{{ errors[0] }}</span>
                </validation-provider>
            }
            else
            {
                <label asp-for="Address2" class="col-form-label">@Loc["Address.Fields.Address2"]:</label>
                <input asp-for="Address2" class="form-control"/>
            }
        </div>
    }
    @if (Model.ZipPostalCodeEnabled)
    {
        <div class="form-group col-sm-12">
            @if (Model.ZipPostalCodeRequired)
            {
                <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                    <label asp-for="ZipPostalCode" class="col-form-label">@Loc["Address.Fields.ZipPostalCode"]:</label>
                    <span class="required">*</span>
                    <input asp-for="ZipPostalCode" v-model="createorupdateaddress@{@prefix}.ZipPostalCode" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.ZipPostalCode.Required"]/>
                    <span class="field-validation-error">{{ errors[0] }}</span>
                </validation-provider>
            }
            else
            {
                <label asp-for="ZipPostalCode" class="col-form-label">@Loc["Address.Fields.ZipPostalCode"]:</label>
                <input asp-for="ZipPostalCode" class="form-control"/>
            }
        </div>
    }
    @if (Model.PhoneEnabled)
    {
        <div class="form-group col-sm-12 col-md-6">
            @if (Model.PhoneRequired)
            {
                <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                    <label asp-for="PhoneNumber" class="col-form-label">@Loc["Address.Fields.PhoneNumber"]:</label>
                    <span class="required">*</span>
                    <input asp-for="PhoneNumber" v-model="createorupdateaddress@{@prefix}.PhoneNumber" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.PhoneNumber.Required"]/>
                    <span class="field-validation-error">{{ errors[0] }}</span>
                </validation-provider>
            }
            else
            {
                <label asp-for="PhoneNumber" class="col-form-label">@Loc["Address.Fields.PhoneNumber"]:</label>
                <input asp-for="PhoneNumber" class="form-control"/>
            }
        </div>
    }
    @if (Model.FaxEnabled)
    {
        <div class="form-group col-sm-12 col-md-6">
            @if (Model.FaxRequired)
            {
                <validation-provider tag="div" rules="required" v-slot="{ errors, classes }">
                    <label asp-for="FaxNumber" class="col-form-label">@Loc["Address.Fields.FaxNumber"]:</label>
                    <span class="required">*</span>
                    <input asp-for="FaxNumber" v-model="createorupdateaddress@{@prefix}.FaxNumber" v-bind:class="[classes , 'form-control']" data-val-required=@Loc["Address.Fields.FaxNumber.Required"]/>
                    <span class="field-validation-error">{{ errors[0] }}</span>
                </validation-provider>
            }
            else
            {
                <label asp-for="FaxNumber" class="col-form-label">@Loc["Address.Fields.FaxNumber"]:</label>
                <input asp-for="FaxNumber" class="form-control"/>
            }
        </div>
    }
    @if (Model.NoteEnabled)
    {
        <div class="form-group col-sm-12 col-md-12">
            <label asp-for="Note" class="col-form-label">@Loc["Address.Fields.Note"]:</label>
            <input asp-for="Note" class="form-control"/>
            <span asp-validation-for="Note"></span>
        </div>
    }
    @if (Model.AddressTypeEnabled && !Model.HideAddressType)
    {
        <div class="form-group col-sm-12 col-md-6">
            <label asp-for="AddressTypeId" class="col-form-label">@Loc["Address.Fields.AddressType"]:</label>
            <select asp-for="AddressTypeId" asp-items="@EnumTranslationService.ToSelectList(AddressType.Any, false).ToList()" class="form-control form-select"></select>
        </div>
    }
    @if (Model.CustomAddressAttributes.Any())
    {
        <partial name="Partials/AddressAttributes" model="Model.CustomAddressAttributes"/>
    }
    <div id="errors-address"></div>
</div>
@{
    var addressVmData = new Dictionary<string, object> {
        ["Name"] = Model.Name,
        ["FirstName"] = Model.FirstName,
        ["LastName"] = Model.LastName,
        ["Company"] = Model.Company,
        ["Address1"] = Model.Address1,
        ["Address2"] = Model.Address2,
        ["ZipPostalCode"] = Model.ZipPostalCode,
        ["City"] = Model.City,
        //empty string, never null: these drive v-model on a <select>, and Vue
        //matches the option by value - null matches nothing and clears the
        //selection, where "" selects the "Select country" placeholder. The old
        //inline script always produced a string because it interpolated into a
        //JS literal.
        ["CountryId"] = Model.CountryId ?? "",
        ["StateProvinceId"] = Model.StateProvinceId ?? "",
        ["AvailableStates"] = Model.AvailableStates.Select(s => new { id = s.Value, name = s.Text }),
        ["PhoneNumber"] = Model.PhoneNumber,
        ["FaxNumber"] = Model.FaxNumber,
        ["VatNumber"] = Model.VatNumber,
        ["Email"] = Model.Email,
        ["prefix"] = prefix
    };
    foreach (var item in Model.CustomAddressAttributes)
    {
        var key = $"address_attribute_{item.Id}";
        switch (item.AttributeControlType)
        {
            case AttributeControlType.DropdownList:
                addressVmData[key] = item.Values.FirstOrDefault(v => v.IsPreSelected)?.Id ?? "";
                break;
            //A checkbox group binds to an array and a radio group to a scalar, and
            //the required rule checks the bound value. The empty-string filler the
            //view used to emit for every unselected option made both look answered:
            //["", ""] is neither empty nor shorter than one item, so client-side
            //validation passed with nothing selected and only the server refused
            //the address - with no message on the field.
            case AttributeControlType.Checkboxes:
            case AttributeControlType.ReadonlyCheckboxes:
                addressVmData[key] = item.Values.Where(v => v.IsPreSelected).Select(v => v.Id).ToList();
                break;
            case AttributeControlType.RadioList:
                addressVmData[key] = item.Values.FirstOrDefault(v => v.IsPreSelected)?.Id ?? "";
                break;
            case AttributeControlType.TextBox:
            case AttributeControlType.MultilineTextbox:
                addressVmData[key] = item.DefaultValue ?? "";
                break;
        }
    }
}
@*Data island for the address form; behaviour lives in vueapp/src/views/country-state-form.js.*@
<script type="application/json" data-grand-vm="countryStateForm">@Json.Serialize(new {
                                                               name = addressVmName,
                                                               data = addressVmData,
                                                               routes = new { getStates = Url.RouteUrl("GetStatesByCountryId") },
                                                               elements = new {
                                                                   countrySelectId = Html.IdFor(m => m.CountryId),
                                                                   stateSelectId = Html.IdFor(m => m.StateProvinceId)
                                                               },
                                                               res = new { warning = Loc["common.warning"].ToString() }
                                                           })</script>