Registration Page Validation

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hello,

We just upgraded our store to 2.6 and an issue we are running into is that even though we have more required fields checked in the administration area (such as address, city, state, etc) it's only validating for First Name, Last Name, Email, and Password.  It displays the * next to the fields showing it is indeed required, but when you hit the register button it only shows the message for those fields I listed.  How do we change this so that it validates ALL the fields we have set as required and shows the validation message when you hit the register button for all of them if they aren't filled in?

Just for additional information, AFTER you fill in the First Name, Last Name, Email, and Password and hit the register button it then validates the remaining required fields.  But we would like it to validate everything at once.  How can we accomplish that?

Thanks,
Chris
11 years ago
Nop.Web.Validators.Customer.RegisterValidator

Add your rules there.
11 years ago
I appreciate the quick response.  That file was actually the first place I looked because that's where we did it in previous versions.  But it seemed to already be in there for 2.6.  I did just now notice though that there is a difference between the first 4 rules and the "form fields" section.  Do you think removing the ".When(x => customerSettings.FaxRequired && customerSettings.FaxEnabled)" from each item would fix the issue even though we definitely have them checked in the administration section?

And again, the "*" is showing up correctly and showing that the fields are indeed all required.  However when you click the Register button, the validation message is not showing for any fields other than First Name, Last Name, Email, and Password.


public RegisterValidator(ILocalizationService localizationService, CustomerSettings customerSettings)
        {
            RuleFor(x => x.Email).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Email.Required"));
            RuleFor(x => x.Email).EmailAddress().WithMessage(localizationService.GetResource("Common.WrongEmail"));
            RuleFor(x => x.FirstName).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.FirstName.Required"));
            RuleFor(x => x.LastName).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.LastName.Required"));


            RuleFor(x => x.Password).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Password.Required"));
            RuleFor(x => x.Password).Length(customerSettings.PasswordMinLength, 999).WithMessage(string.Format(localizationService.GetResource("Account.Fields.Password.LengthValidation"), customerSettings.PasswordMinLength));
            RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ConfirmPassword.Required"));
            RuleFor(x => x.ConfirmPassword).Equal(x => x.Password).WithMessage(localizationService.GetResource("Account.Fields.Password.EnteredPasswordsDoNotMatch"));


            //form fields
            RuleFor(x => x.Company).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Company.Required"))
                .When(x => customerSettings.CompanyRequired && customerSettings.CompanyEnabled);
            RuleFor(x => x.StreetAddress).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.StreetAddress.Required"))
                .When(x => customerSettings.StreetAddressRequired && customerSettings.StreetAddressEnabled);
            RuleFor(x => x.StreetAddress2).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.StreetAddress2.Required"))
                .When(x => customerSettings.StreetAddress2Required && customerSettings.StreetAddress2Enabled);
            RuleFor(x => x.ZipPostalCode).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.ZipPostalCode.Required"))
                .When(x => customerSettings.ZipPostalCodeRequired && customerSettings.ZipPostalCodeEnabled);
            RuleFor(x => x.City).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.City.Required"))
                .When(x => customerSettings.CityRequired && customerSettings.CityEnabled);
            RuleFor(x => x.Phone).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Phone.Required"))
                .When(x => customerSettings.PhoneRequired && customerSettings.PhoneEnabled);
            RuleFor(x => x.Fax).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Fax.Required"))
                .When(x => customerSettings.FaxRequired && customerSettings.FaxEnabled);
        }}


Thanks,
Chris
11 years ago
RuleFor(x => x.Company).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Company.Required"));


Try it without the .When()?  I have the above line in mine and it shows up along with the others if I just load the page and hit Register.
11 years ago
Thanks Andy! That worked like a charm for the textboxes.  How about the dropdown boxes though?  I tried the code below but it doesn't seem to display the validation messages like the other fields.


RuleFor(x => x.AvailableStates).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.StateProvince.Required"));
            RuleFor(x => x.AvailableCountries).NotEmpty().WithMessage(localizationService.GetResource("Account.Fields.Country.Required"));


Thanks again,

Chris
11 years ago
Try .NotEqual(0) (not NotEmpty method as you do now)
11 years ago
Andrei,

I went ahead and tried that and I'm getting an error during compile.  Here's the replaced code:

RuleFor(x => x.AvailableStates).NotEqual(0).WithMessage(localizationService.GetResource("Account.Fields.StateProvince.Required"));
            RuleFor(x => x.AvailableCountries).NotEqual(0).WithMessage(localizationService.GetResource("Account.Fields.Country.Required"));


Here's the error:

Error  1  'FluentValidation.IRuleBuilderInitial<Nop.Web.Models.Customer.RegisterModel,System.Collections.Generic.IList<System.Web.Mvc.SelectListItem>>' does not contain a definition for 'NotEqual' and the best extension method overload 'FluentValidation.DefaultValidatorExtensions.NotEqual<T,TProperty>(FluentValidation.IRuleBuilder<T,TProperty>, System.Linq.Expressions.Expression<System.Func<T,TProperty>>, System.Collections.IEqualityComparer)' has some invalid arguments


Thanks,

Chris
11 years ago
Any other ideas for validating dropdowns?

Thanks,

Chris
11 years ago
Check the CountryId, not the AvailableCountries

RuleFor(x => x.CountryId).NotEqual(0).WithMessage(localizationService.GetResource("Account.Fields.Country.Required"));
11 years ago
There are no errors this time, but the validation message is not displaying along with all the other textboxes =/

I double checked and it does set the value of the dropdowns to 0 in the CustomerController.cs file.  It seems like something this simple should be much easier :)

I appreciate your help through this.

Edit:
Also, just to clarify, here is the code in my view:
@if (Model.CountryEnabled && Model.StateProvinceEnabled)
                            {
                                <tr class="row">
                                    <td class="item-name">
                                        @Html.LabelFor(model => model.StateProvinceId):
                                    </td>
                                    <td class="item-value">
                                        @Html.DropDownList("StateProvinceId", Model.AvailableStates)
                                        @Html.RequiredHint()
                                        <span id="states-loading-progress" style="display: none;">@T("Common.Wait...")
                                            <img src='@Url.Content("~/Content/Images/ajax_loader_small.gif")' alt="@T("Common.Wait...")" />
                                        </span>
                                         @Html.ValidationMessageFor(model => model.AvailableStates)
                                    </td>
                                </tr>
                            }


And here is my validation code:
RuleFor(x => x.StateProvinceId).NotEqual(0).WithMessage(localizationService.GetResource("Account.Fields.StateProvince.Required"));

I also tried changing:
@Html.ValidationMessageFor(model => model.AvailableStates)
to:
@Html.ValidationMessageFor(model => model.StateProvinceId)

and I didn't have any luck.

Thanks a ton,
Chris
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.