开发者

Zend_Form disable populate before isValid()

Im not able to disable populating values in Zend_Form .

I have my own form class. With hidden token element where I would like to dynamicaly setup random value every time the form is called (or reposted). I thought that setValue will make the job.

class MY_Form_Test extends Zend_Form {
    public function init() {
    ...

    $this->addElement('hidden', 'token');
    $this->getElement('token')->setValue(uniqid('',true));
    ... 
}

BUT: When I have simple controller like this. Zend automate populating old hidden values except to generate new one.

$form = new JC_Form_Test();
if($form->isValid($_POST)){
   // Action ... 
}
else{
   // Error
}

SOLUTION: The only solution I found is to call setValue in Controler AND AFTER isValid method. eg. in Error block.

QUESTION: Is there any way to setup element va开发者_如何学运维lues directly in form class OR disable populate values in form class or before isValid() is called?


I think it's the best way to do it.

I work much with Zend Framework and have my own library for overwrite some Zend classes.

It's not bad to change something, but don't do it directly within Zend Framework


SECOND SOLUTION: Second solution I found is to overload isValid() method in Form class. Like this. Then I dont need to put setValue() into every Controller.

class MY_Form_Test extends Zend_Form {

    ...

    public function isValid($data){
    // Propagate values
    $valid = parent::isValid($data);

    $this->getElement('token')->setValue(uniqid('',true));

    return $valid;
}

Are there any other solution eg. some element option to do this job more simple?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜