addFormRule(array($this, '_validateForm')); return parent::init(); } protected function _initElements() { $this->_addUserElement(); $this->addElement( 'select', 'eventId', array( 'label' => 'Event', 'multiOptions' => array('' => 'Select One') + (array) $this->getDataObj()->getSignupEvents4Select(), 'required' => true ) ); return $this; } protected function _addUserElement() { if (($userId = (int) $this->_getData('userId')) && false !== ($name = \App_User_Obj::getAutocompleteItemTitle($userId)) ) { $this->addElement('hidden', 'userId', array('value' => $userId)); $this->addElement( 'static', 'userName', array( 'label' => 'User Name', 'value' => $name ) ); } return $this; } protected function _validateForm($data) { $errors = array(); if ($eventId = $data['eventId']) { $event = $this->getDataObj()->setPrimaryKey($eventId)->getData(); } else { // Element is required and validates using default validator return true; } if (empty($event)) { $errors['eventId'][] = 'Selected event not found'; return $errors; } if (Obj::TYPE_REGULAR !== $event['type'] && Obj::TYPE_GROUP !== $event['type']) { $errors['eventId'][] = 'Selected event does not support registration'; return $errors; } if ($event['registrationUrl']) { $errors['eventId'][] = 'Selected event has registration outside the site'; return $errors; } if ($event['endDate'] < date('Y-m-d')) { $errors['eventId'][] = 'Registration for outdated events does not supported'; return $errors; } if ($event['registrationLimit'] && $event['registeredCount'] >= $event['registrationLimit']) { $errors['eventId'][] = 'Selected event does not have free registration spots'; return $errors; } return ($errors) ? $errors : true; } public function setDataObj(Obj $dataObj) { $this->_dataObj = $dataObj; return $this; } public function getDataObj() { if (null == $this->_dataObj) { throw new Qs_Exception_EmptyPropertyException($this, '_dataObj'); } return $this->_dataObj; } }