Crownpeak Input Validation Cheat Sheet

A cheat sheet in English by GregFinzer

Input Validation for Crownpeak

6,500 views 1,924 downloads 27 Sep 2018 History
3.7 avg · 0 ratings

Example input.aspx

Input.ShowHeader("Contact Us");
Input.ShowTextBox("Page Title", "page_title");
Input.ShowWysiwyg("Address", "address", ServicesInput.FullWYSIWYG());
Input.ShowTextBox("Customer Service Phone", "customer_service_phone");
Input.ShowTextBox("Email", "email");
Place in Projec­t\T­emp­lat­es­\Som­eTe­mpl­ateName

Example output.aspx

<h1><%= asset["page_title"] %></h1>
<p><%= asset["address"] %></p>
<p><%= asset["customer_service_phone"] %></p>
<p><a href='mailto:<%= asset["email"] %>'><%= asset["email"] %></a></p>
Place in Projec­t\T­emp­lat­es­\Som­eTe­mpl­ateName

Example PostIn­put­Helper

using System.Text.RegularExpressions;
using CrownPeak.CMSAPI;
/* Some Namespaces are not allowed. */
namespace Developer_Training.Project.Library
{
    public static class PostInputHelper
    {
        public static bool RequiredField(Asset asset, PostInputContext context, string field)
        {
            bool isValid = true;

            if (!context.InputForm.HasField(field))
            {
                context.ValidationErrorFields.Add(field, "This is a required field");
                isValid = false;
            }
            else if (!asset.Label.Equals(context.InputForm[field]))
            {
                asset.Rename(context.InputForm[field]);
            }

            return isValid;
        }

        public static bool ValidEmail(Asset asset, PostInputContext context, string field)
        {
            bool isValid = true;
            if (!Regex.IsMatch(context.InputForm[field], @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
            {
                context.ValidationErrorFields.Add(field, "Please enter a valid email address");
                isValid = false;
            }
            else if (!asset.Label.Equals(context.InputForm[field]))
            {
                asset.Rename(context.InputForm[field]);
            }

            return isValid;
        }
    }
}
Place in Projec­t\L­ibrary

Example post_i­npu­t.aspx

PostInputHelper.RequiredField(asset, context, "page_title");
PostInputHelper.ValidEmail(asset, context, "email");
Place in Projec­t\T­emp­lat­es­\Som­eTe­mpl­ateName

Result

Result

Badges

Nice Cheat Sheet

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.