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开发者_JAVA技巧', 'class' => 'nome_campo'))
));
If Marcin comment is not what you want to do, then this is another way to create multi text element.
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; } }
}
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; }
}
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
精彩评论