Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

How to create a custom form in MVC

Vote:
 

I have based a site on the Alloy MCV and even though i dont really like the structure of the code I've managed to live with it and work with it until now.

Is there any examples how you should do a form with validation on a page? The form and validation messages needs to be translated into several languages as well.

This is what I have come up with on my own but it seems like there should be a better way.

The page consists of two parts, one list of items and a order form. The list is part of the view model and I fill it with the index action and that part seems fine.

The order form is made using a separate model that is part of the view model. The order model has data annotations so that i can make it validate with the standard MVC way including javascript validation.

To translate the validation messages and fields I fell back on resources(resx) files and it seems to work but It feels quite stupid to introduce a third way of translating stuff (on page, xml files and now resource files).

So is there any working examples how to do something like this? Just a "simple" form with validation and translation.

#74366
Aug 26, 2013 14:17
Vote:
 

Maybe this could give some inspiration: http://www.tech-fellow.lv/2013/05/localized-episerver-model-validation-attributes/

I know that there have been complaints that built-in localization should be working -> but I couldn't manage to get that on my local Alloy instance.

#74371
Aug 26, 2013 17:24
Vote:
 

Are you trying to utilizing XForms to building out and save the form data? If not, you're probably just better off staying on the track you are doing... just using standard MVC to make the form, do validation, and your own logic to send/save the data. If you need to inject anything from EPiServer, you can do that using the API in the controller or the view. Then you can use EPiServer's LocalizationService (as Valdis uses in his blog post) to pull in translation data from an XML file, instead of using .RESX files.

http://cjsharp.com/blog/2013/04/11/working-with-localization-and-language-branches-in-episerver-7-mvc/

#74372
Edited, Aug 26, 2013 17:55
Vote:
 

I used the following code for required field validation. Just need to use resources xml and add a new attribute which inherits from RequiredAttribute :

    public class LocalizedRequiredAttribute : RequiredAttribute
    {
        private readonly string errorMessageKey;

        public LocalizedRequiredAttribute(string errorMessageKey)
        {
            this.errorMessageKey = errorMessageKey;
            ErrorMessage = errorMessageKey;
        }

        public override string FormatErrorMessage(string name)
        {
            return ServiceLocator.Current.GetInstance<LocalizationService>().GetString(this.errorMessageKey);
        }
    }

 

Then in form model:

        [LocalizedRequired("/common/firstnameerrormessage")]
        public virtual string FirstName { get; set; }

        [LocalizedRequired("/common/lastnameerrormessage")]
        public virtual string LastName { get; set; }

        [LocalizedRequired("/common/emailerrormessage")]
        [Email("/common/emailvalid")]
        public virtual string Email { get; set; }

Hopes some helpful for you

#74439
Edited, Aug 28, 2013 3:27
Vote:
 

Im not really fond of the xml way either since it means that the site can't be translated and changed without hacking xml and redistributing files. Ideally everything related to displayed text and images should be changeable by the editors without having to bother the developers. It also helps if you can read the language when you add translations to se if it looks reasonable.

Not a fan of umbraco when it comes to handle multiple languages but they do have a dictionary editor built in. It would be nice to be able to do something like that.

For this project I'll stick to resource files, but for my next project when there is more time I'll look into making a translation page that i can pull translations from.

 

#74469
Aug 28, 2013 14:22
Vote:
 

Take a look at this blog post. Sounds like something similar you are looking for: https://jstemerdink.wordpress.com/2013/06/20/a-custom-localization-provider-for-episerver-7-2/

#74470
Aug 28, 2013 14:26
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.