开发者

Validate an input field for a date through Zend_Form with Zend_Validate_Date

开发者 https://www.devze.com 2023-02-17 05:13 出处:网络
$this->addElement(\'text\', \'projected-start\', array( \'required\'=> false, \'validators\'=> array (
    $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 fo开发者_运维知识库rm. 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


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;


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'
));
0

精彩评论

暂无评论...
验证码 换一张
取 消