开发者

Zend: Submit form with uppercase values

I want to submit a zend form but with lowercase values. Whatever user use case but I want to get form value as lowercase. I am creating form like this.

$elements = array();

$element = $this->CreateElement('text', 'LastName');
$element->setLabel('Last Name');
$element->addFilter( 'StringToLower' );
$elements[] = $element;

$element = $this->CreateElement('text', 'FirstName');
$element->setLabel('First Name');
$element->addFilter( 'StringToLower' );
$elements[] = $element;

$this->addElements($elements);

And getting values like this:

$request = $this->getRequest();
$values = $request->getParams();

Above form is submit form values successfully but $element->addFilter( 'StringToLower' ); does not seem to work. It always submit the form with user entered case not lowercase.

How can I force the form to convert all values to lowercase?

开发者_Go百科Thanks


You did not describe how you get the result values, but this is the right way to do that:

$values = $form->getValues()

Also, the more handy way of writing the same code could be:

$this->addElement('text', 'LastName', array(
    'label' => 'Last Name',
    'filters' => array('StringToLower')
));

And one of the possible complete workarounds could be:

if ($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
    $values = $form->getValues();
}


Are you calling $form->isValid() then using $form->getValues()?

The form data wont filter itself unless you have client-side validation/filtering as well.

$form = new MyForm();

if($form->isValid($this->getRequest()->getPost()){
    $filteredValues = $form->getValues();
} else {
    // validation failed
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜