addElement('text', 'title', array('label' => 'Title', 'required' => true)); $this->addElement('textarea', 'metaKeyword', array('label' => 'Meta Keywords')); $this->addElement('textarea', 'metaDescription', array('label' => 'Meta Description')); $this->addElement( 'htmlEditor', 'description', array( 'label' => 'Purpose', 'required' => true, 'hasMsWordNote' => true, ) ); $this->addElement( 'grid', 'members', array( 'label' => 'Members', 'columns' => $this->_getMemberColumns(), 'itemName' => 'Member', 'addLinkPrefix' => '+ ', 'required' => true, 'scriptOptions' => array('updateCallback' => 'App_Committee_Admin_Form.initMemberAutocomplete'), ) ); /** @var \Qs_Form_Decorator_Grid $gridDecorator */ $gridDecorator = $this->getElement('members')->getDecorator('grid'); $gridDecorator->setText('delete', 'Remove from Committee'); $this->addFormRule(array($this, 'validateForm')); return parent::_initElements(); } protected function _getMemberColumns() { $roles = \App\Committee\Admin\Obj::getInstance()->getUserCommitteeRole4Select(); return array( 'name' => array('type' => 'text', 'title' => 'Name'), 'roleId' => array('type' => 'select', 'title' => 'Leadership Role', 'multiOptions' => $roles), 'options' => array('type' => 'options', 'title' => 'Options', 'attribs' => array('colspan' => 2)), 'userId' => array('type' => 'hidden', ), ); } protected function _addResources() { /** @var $doc \Qs_Doc */ $doc = \Zend_Registry::get('doc'); $doc->addScript('js/jquery-ui.js'); $doc->addScript('js/app/Committee/admin/form.js'); $doc->addInitObject( 'app.committee.admin.Form', array(array( 'addRowId' => 'members-add', 'userUrl' => \App_User_Admin_View::getUrl4UserJson(), )), 'App_Committee_Admin_Form' ); return parent::_addResources(); } public function validateForm($data) { $errors = array(); $members = empty($data['members']) ? array() : $data['members']; $membersErrorMsg = 'This field is required and can\'t be empty'; if (!$members) { $errors['members'] = $membersErrorMsg; return $errors; } foreach ($data['members'] as $member) { if (empty($member['userId'])) { $errors['members'] = $membersErrorMsg; return $errors; } } $this->_validateMembers($members, $errors); return empty($errors) ? true : $errors; } protected function _validateMembers($members, &$errors) { if (empty($members)) { return $this; } $membersId = array_filter(\Qs_Array::fetchCol($members, 'userId')); if (empty($membersId)) { return $this; } if (count($membersId) != count(array_unique($membersId))) { $errors['members'][] = 'There are some duplicates. Please, remove unnecessary rows.'; } return $this; } }