1

hello i have a form where the user can click on a button and dinamically add new elements(with Jquery)

<input name="sconto[]" type="text"><br>
<input name="sconto[]" type="text"><br>
<input name="sconto[]" type="text"><br>
...

I have a custom validator for float numbers in format with comma and dot separation like 20.50 and 20,50 The problem is i can't seem to find how to make zend apply it it to each element of the array.

So how should i declare this element and how to apply the validator? xD

this is my validator

protected $_messageTemplates = array(
self::NON_E_NUMERO  => 'non sembra essere un numero'
);

public function isValid($value, $context = null)
{
    $pos_virgola = strpos($value, ",");
    if ($pos_virgola !== false)
        $value = str_replace(",", ".", $value);

    if (!is_numeric($value))
    {
        $this->_error(self::NON_E_NUMERO, $value);
        return false;
    }
    else
        return true;
}

}

the form i don't know how to do it, i use this but obviously it doesn't work

$sconto = $this->createElement('text','sconto')->setLabel('sconto');
//->setValidators(array(new Gestionale_Validator_Float()));
$this->addElement($sconto);
...
$sconto->setDecorators(array(//no ViewHelper
            'Errors',
            'Description',
            array(array('data' => 'HtmlTag'), array('tag' => 'td', /*'class' => 'valore_campo', */'id'=>'sconto')),
            array('TdLabel', array('placement' => 'prepend', 'class' => 'nome_campo'))
    ));
max4ever
  • 11,909
  • 13
  • 77
  • 115
  • Can you provide code for the form and your validator. – Marcin Apr 05 '11 at 12:46
  • 1
    I think that in ZF you cannot easily make forms with multiple fields named 'sconto[]'. However you could do 'sconto[0]', 'sconto[1]', etc. Have a look for example [here](http://stackoverflow.com/questions/405897/zend-form-array-based-elements) or [here](http://stackoverflow.com/questions/4922304/array-input-like-name-person-in-zend-form/4923019#4923019). – Marcin Apr 05 '11 at 15:09
  • i could do sconto[0] sconto[1] ... with javascript, the problem is i don't know how many the user will enter – max4ever Apr 05 '11 at 15:20
  • Yeah, these things are why I prefer handling my forms and my $_POST data myself. I just hope that Zend_Form will live up to the rest of Zend Framework in version 2. – markus Apr 06 '11 at 05:52

1 Answers1

1

If Marcin comment is not what you want to do, then this is another way to create multi text element.

  1. Create a custom decorator 'My_Form_Decorator_MultiText'. You will need to register your custom decorator class. Read Zend Framework doc for details http://framework.zend.com/manual/en/zend.form.decorators.html

    class My_Form_Decorator_MultiText extends Zend_Form_Decorator_Abstract {

    public function render($content) {
    
        $element = $this->getElement();
        if (!$element instanceof Zend_Form_Element_Text) {
            return $content;
        }
    
        $view = $element->getView();
        if (!$view instanceof Zend_View_Interface) {
            return $content;
        }
    
        $values = $element->getValue();
        $name = $element->getFullyQualifiedName();
        $html = '';
    
        if (is_array($values)) {
            foreach ($values as $value) {
                $html .= $view->formText($name, $value);
            }
        } else {
            $html = $view->formText($name, $values);
        }
    
        switch ($this->getPlacement()) {
            case self::PREPEND:
                return $html . $this->getSeparator() . $content;
            case self::APPEND:
            default:
                return $content . $this->getSeparator() . $html;
        }
    }
    

    }

  2. Now your validation class will validate each element value

    class My_Validate_Test extends Zend_Validate_Abstract { const NON_E_NUMERO = 'numero'; protected $_messageTemplates = array( self::NON_E_NUMERO => 'non sembra essere un numero' );

    public function isValid($value, $context = null) {
        if (!is_numeric($value)) {
            $this->_error(self::NON_E_NUMERO, $value);
            return false;
        }
        else
            return true;
    }
    

    }

  3. This is how you can use the new decorator

$element = new Zend_Form_Element_Text('sconto', array( 'validators' => array( new My_Validate_Test(), ), 'decorators' => array( 'MultiText', // new decorator 'Label', 'Errors', 'Description', array('HtmlTag', array('tag' => 'dl',)) ), 'label' => 'sconto', 'isArray' => true // must be true )); $this->addElement($element);

Hope this helps

satrun77
  • 3,202
  • 17
  • 20
  • i don't understand why this is suppose to work with an arbitrary length array, it seeems to me that the validator is passed only one value ? – max4ever May 23 '11 at 08:11
  • Your are not clear of what you want. Creating an array elements or adding new elements with JavaScript in Zend_Form is easy. You have different answers posted by other member and I posted another option. All of the answers will achieve the same result array elements. Good Luck! – satrun77 May 24 '11 at 00:33