Chris Weldon wrote:
>
> How do you have the Multicheckbox element setup? How are you going
> about checking that the form is valid? A bit more code samples would
> prove useful...
>
I've done a simple setup, this is, more or less, my setup:
Base class (in which I add common decorator and filters for all forms in
order to obtain consistent look and feel and behaviour for my app)
abstract class Form_Base extends Zend_Form {
/**
* Form title
* @var string
*/
protected $_title;
public function init() {
// set class for this form
$this->setAttrib('class', 'form');
// add forms css
$view = $this->getView();
$view->headLink()->appendStylesheet(
$view->baseUrl() . '/styles/form.css', 'all');
// set the form to be post
$this->setMethod('post');
// set translator
$this->setTranslator(
Zend_Registry::get('Zend_Translate')->getAdapter());
// call abstract method that init form elements
$this->initComponents();
// trim all values provided in form
foreach ($this->getElements() as $elt) {
$elt->addFilter('StringTrim');
}
$this->setDecorators(
array (
'FormElements',
array (
'Description',
array (
'tag' => 'dd',
'escape' => false,
'placement' => Zend_Form_Decorator_Abstract::PREPEND)),
array (
'HtmlTag',
array (
'tag' => 'dl',
'class' => 'zend_form')),
'Form'));
// custom render of the form
$titleTag = new Form_Decorator_Title();
$this->addDecorator($titleTag);
$this->addDecorator(array ('SurroundTag' => 'HtmlTag'),
array ('tag' => 'div', 'class' => 'formdiv'));
}
protected function addSubmitButton($label = 'Send') {
$submit = new Zend_Form_Element_Submit('submitBtn', $label);
$this->addElement($submit);
$this->addDisplayGroup(array ('submitBtn'), 'buttons',
array ('legend' => 'Send Form'));
}
// subclass will create form elements in this method
abstract protected function initComponents();
}
and this is the class in which I use the multicheckbox element:
class Form_Checkout extends Form_Base {
protected function initComponents() {
// title
$this->setTitle('FORM_CHECKOUT_TITLE');
$selected = new Zend_Form_Element_MultiCheckbox('orders',
array (
'separator' => '',
'required' => true,
'class' => 'checkbox'));
$this->addElement($selected);
$this->addSubmitButton('FORM_CHECKOUT_BUTTON');
}
public function addOrder(Request $request) {
$this->orders->addMultiOption($request->idorder,
$request->getDescription());
}
}
In my controller I call addOrder method once for every order that could be
payed, but in a special case, I don't want that more than x orders could be
selected in the form as explained in my first message.
Thank you.
--
View this message in context:
http://www.nabble.com/Writing-a-validator-for-limit-the-number-of-selectable-elements-tp21511756p21523746.html
Sent from the Zend Framework mailing list archive at Nabble.com.