Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:27:24+00:00 2026-06-10T16:27:24+00:00

I have a custom markup for my zend_form and i try to set a

  • 0

I have a custom markup for my zend_form and i try to set a css class (class=”hasErrors”) to my input field. My rendered form elements look like this:

<div id="teamname-element" class="formRow">
    <input type="text" name="teamname" id="teamname" value="" placeholder="Teamname">
</div>

with errors it looks like this:

<div id="teamname-element" class="formRow">
    <input type="text" name="teamname" id="teamname" value="" placeholder="Teamname">
    <ul class="errors">
        <li>Your teamname is to short!</li>
    </ul>
</div>

is it possible to set a css class to the input field without using javascript?

<input class="hasErrors" type="text" name="teamname" id="teamname" value="" placeholder="Teamname">

A form element is created like this:

$this->addElement(new Nut_Form_Element_Text('teamname', array(
    'Label'      => 'Teamname',
    'validators' => array( array('StringLength', FALSE, array(1, 25)), $TeamRegex),
    'filters'    => array('StringTrim', 'StripTags'),
)));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:27:26+00:00Added an answer on June 10, 2026 at 4:27 pm

    Here we create a form class that extends Zend_Form. We override the isValid() method. We loop through the form->elements and add a class of “error” if the hasErrors() method returns true. We also do a check to see if a class attribute already exists using the getAttrib() method and if so we append the “error” class using a space to separate.

    class MyForm extends Zend_Form
    {
        public function isValid($data)
        {
            $valid = parent::isValid($data);
    
            foreach ($this->getElements() as $element) {
                if ($element->hasErrors()) {
                    $oldClass = $element->getAttrib('class');
                    if (!empty($oldClass)) {
                        $element->setAttrib('class', $oldClass . ' hasError');
                    } else {
                        $element->setAttrib('class', 'hasError');
                    }
                }
            }
    
            return $valid;
        }
    }
    

    Now, when you call the isValid method, it will automatically add a class of error to any fields that do not pass validation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some rendering logic for custom markup (a'la bbcode, but not bbcode). So
I have a set of custom ASP.NET server controls, most of which derive from
I have created the following custom tooltip with the custom template. <ToolTip x:Class=FireFly.Controls.CustomToolTip xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
I have two custom dropdown lists that have the same markup. I need to
I have a form and I would like to customise the markup for it.
I have a custom control defined in code-behind: Public Class MyControl Inherits Button Private
I have a slideshow that requires custom markup for the pagination, which also includes
I have a custom control that I initially set to collapsed like so <local:SpriteButton
I have a custom base page class: //MobileFunnelPage.cs public abstract class MobileFunnelPage : Page
I have a custom usercontrol with that code: public partial class AudioControl : UserControl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.