1

In my model

$form = new \Zend_Form();
$form->addElement('text', 'name', array(
        'validators'    => array(
            array('NotEmpty', true),
            array("stringLength", true, array(1,40))
        ),
        'required'      => true,
        'label'         => "Name",
    ));
return $form;

In my controller, called above model function

if($form->isValid($_POST)) {
  ....
} else {
$form = Product::getForm();
print_r($form->getErrors());
print_r($form->getErrorMessages());
print_r($form->getMessages());
}

I am using Zend Framework.

Here in name field in form, string with more than 40 chars needs to display error messages.

I tried with get error with getErrors() and getErrorMessages(). But none of these function give me error. It returns an empty array on printing the these functions.

Please help me to solve this problem...

1
  • No problem, just worth mentioning when it's low. Commented Aug 7, 2012 at 11:07

2 Answers 2

3

Why make you this call:

$form = Product::getForm();

So you overwrite the old $form variable with its error messages. Try it without these line.

Sign up to request clarification or add additional context in comments.

2 Comments

Code provided for model in question is function getForm()
@Justin: Thargor is right. Your controller sequence starts with a $form instance, but after you call $form = Product::getForm(), you are blasting the old form and creating a new one.
2

The error messages will not be there until you call isValid() on the form, like so

$form = Product::getForm();

if(!$form->isValid($_POST))
{
    print_r($form->getErrors());
    print_r($form->getErrorMessages());
    print_r($form->getMessages());        
}

3 Comments

Already the code is in that format... I have edited the question.
Ah, well the original question wasn't in that format. Does your Product::getForm() override isValid()? Or any of the other functions? If you leave the field empty, does it fire the NotEmpty error?
Code provided for model is function getForm(). I don't think it will override the $form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.