Blog comment form validation

  • Hi all, I am new to Perch and was wondering if it is possible to add classes and attributes to invalid fields in the blog comment form, or to any Perch form.
    I want to add aria-invalid attributes to make the form validation more accessible.

    I know how to do it with Javascript, but for me this should not be done by Javascript alone, it should also be done on the server.

    Hopefully someone can help me on my way

    Kind Regards

  • drewm

    Approved the thread.
  • Hi,


    Perch-templated forms have conditional tags for errors (perch:error), you may be able to use them directly on perch:input like this:


    HTML
    1. <perch:input id="first_name" type="text" required <perch:error for="first_name" type="required>aria-invalid="true"</perch:error> >


    Although this is not very practical if the field has more than multiple validation criteria:


    HTML
    1. <perch:input id="email" type="email" required
    2. <perch:error for="email" type="required">aria-invalid="true"</perch:error>
    3. <perch:error for="email" type="format">aria-invalid="true"</perch:error>
    4. >


    If you are using the Pipit app, you can use pipit_form_set_error_vars() so you can use a single perch:if:


    HTML
    1. <perch:form id="comment" method="post" app="perch_blog">
    2. <perch:input id="commentEmail" type="email" required <perch:if exists="comment_error_commentEmail">aria-invalid="true"</perch:if> >
    3. <perch:form>



    I don't think Perch expects tags to be used alongside attributes like that which may prevents it from reading all the perch:input tag attributes when validating the submission. So ensure the conditional tags perch:error or perch:if are added at the end (after all the other perch:input tag attributes).