2
    $this->addElement('text', 'projected-start', array(
        'required'   => false,
        'validators'  => array (
            array('date', false, array('MM/dd/yyyy'))
        ),
        'label'      => 'Projected Start:',
        'class'      => 'form-date'
    ));

I'm extending Zend_Form to create a new custom form. I tried to validate a date using the code above but it simply is not working and nothing is displaying when I enter an invalid input. Any help on this one?

EDIT:

class Application_Form_CreateProject extends Zend_Form
{
    public function init()
    { ... }
}

Thats the start of extending the form.

$form = new Application_Form_CreateProject();
        $request = $this->getRequest();
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                                echo "true";
            }
        }
        $this->view->form = $form;

That's the controller

echo $this->form->setAction($this->url());

That's the view

Charles
  • 50,943
  • 13
  • 104
  • 142
user434366
  • 539
  • 7
  • 15

2 Answers2

2

What do you get when you try this:

$f = new Zend_Form();
$f->addElement('text', 'projected-start', array(
    'required'   => false,
    'validators'  => array (
       array('date', false, array('MM/dd/yyyy'))
    ),
    'label'      => 'Projected Start:',
    'class'      => 'form-date'
));

$data = array(
    'projected-start' => '13/03/2011'
);
var_dump( $f->isValid( $data ) );
var_dump( $f->getErrors() );
die;
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
  • `bool(false) array(1) { ["projected-start"]=> array(1) { [0]=> string(15) "dateFalseFormat" } } ` – user434366 Mar 15 '11 at 22:52
  • Well, then that works. So there must be something wrong with either the rest of your form, or with the `POST` data. Try `var_dump`ing your `POST` data and see if you get the expected result that should invalidate your form. – Decent Dabbler Mar 15 '11 at 22:56
  • PS.: please expand your question with more code. Preferably code that shows how exactly you extend `Zend_Form` and build the elements. Also a minimal example of how you intitiate the form and how you pass in the data. – Decent Dabbler Mar 15 '11 at 23:00
  • I put invalid input for the date but it says it passed validation. When I put valid inputs the same happens. Ok. Will do. – user434366 Mar 15 '11 at 23:06
  • What is the *exact* date string you put in? – Decent Dabbler Mar 15 '11 at 23:09
  • Hey, that's strange. With my example from above, it also passes. Then there must be something 'wrong' with the validator, or rather not behaving as expected. – Decent Dabbler Mar 15 '11 at 23:19
  • 1
    BUGS BUGS BUGS BUGS BUGS BUGS – user434366 Mar 15 '11 at 23:22
  • I think i'll just add another validation with regex to make sure its in the right format – user434366 Mar 15 '11 at 23:23
  • Actually, 13/03/2011 comes out as valid – user434366 Mar 15 '11 at 23:27
  • Be sure to read the docs also, if you haven't already. Might be some expected behaviour we are both not aware of. – Decent Dabbler Mar 15 '11 at 23:28
  • I took a peek in `Zend_Validate_Date::isValid()`, it seems it uses `Zend_Date::isDate()` for validation when a format is given. And `Zend_Date` happily accepts just about all your inputs with your given format. So `Zend_Date` is behaving very unintuitive. – Decent Dabbler Mar 15 '11 at 23:35
  • 1
    Ok, well I figured out one bug. Apparently you can't use dashes in the name of the input element. Now when you input 13/13/13 it says invalid. But, 123123 still returns valid. Thanks for the help, I'm going to regex for the rest of the loopholes. – user434366 Mar 15 '11 at 23:37
1

You could try this

$this->addElement('text', 'projected-start', array(
    'required'   => false,
    'validators'  => array (
        new Zend_Validate_Date(array('format' => 'MM/dd/yyyy'))
    ),
    'label'      => 'Projected Start:',
    'class'      => 'form-date'
));
azat
  • 3,545
  • 1
  • 28
  • 30