addScript('js/app/user/form.js'); $options = array(); $options['formId'] = $this->getId(); $doc->addScript('js/jquery.defaultHint.js', array(), 'defaultHint'); $options['hintOptions'] = [ 'elements' => [ ['selector' => '#_companyId', 'hint' => 'Autocomplete field'] ], 'useWrapper' => true ]; $doc->addInitObject('app.user.form', array($options), 'appUserForm'); return parent::render($view); } /** * @return string */ public function getJobTitleElementMode() { return $this->_jobTitleElementMode; } /** * @param string $mode * @return $this */ public function setJobTitleElementMode($mode) { $this->_jobTitleElementMode = $mode; return $this; } public function setCompanyId($companyId) { $this->_companyId = $companyId; return $this; } public function getCompanyId() { return $this->_companyId; } protected function hasPasswordFields() { return $this->_hasPasswordFields; } /** * @return AdminObj */ protected function _getDataObj() { if (null === $this->_dataObj) { $this->_dataObj = new AdminObj(); } return $this->_dataObj; } /** * "Company" fields for "association" mode * @return $this */ protected function _initCompanyField() { $this->addElement('autocomplete', 'companyId', ['label' => 'Company Name', 'required' => true]); $this->companyId->setDataUrl($this->getAction() . '?action=autocompleteCompany'); if (null !== ($companyId = $this->_getData('companyId'))) { if (null !== ($companyTitle = $this->_getDataObj()->getCompanyTitleById($companyId, 'name'))) { $this->companyId->setTitle($companyTitle); } } return $this; } protected function _initEmailField() { $this->addElement( 'email', 'email', [ 'label' => 'Email Address', 'required' => true, 'attribs' => ['autocomplete' => 'off'] ] ); $unique = new Qs_Validate_Unique(new Qs_Db_Table('User'), 'email', $this->_getData('id')); $unique->setWhere('`bought` = "y"'); $unique->setMessage($this->getConfig('uniqueEmailMessage'), Qs_Validate_Unique::NOT_UNIQUE); $this->getElement('email')->addValidator($unique, true); return $this; } protected function _initPasswordFields() { $this->addElement( 'password', 'password', ['label' => 'Password', 'attribs' => array('autocomplete' => 'off')] ); $this->getElement('password')->addValidator('Callback', true, [[$this, 'validatePassword']]); $this->addElement( 'password', 'confirmPassword', ['label' => 'Confirm Password', 'attribs' => ['autocomplete' => 'off']] ); $this->getElement('confirmPassword')->addValidator('ConfirmPassword', true, ['password']); return $this; } public function validatePassword($value, $context = null) { if (!empty($value) && empty($context['confirmPassword'])) { $this->getElement('confirmPassword')->setRequired()->isValid(''); $this->getElement('confirmPassword')->setErrors(array('Password confirmation does not match')); } return true; } protected function _initPersonalFields() { $this->addElement('text', 'firstName', ['label' => 'First Name', 'required' => true]); $this->addElement('text', 'middleInitial', ['label' => 'Middle Initial']); $this->addElement('text', 'lastName', ['label' => 'Last Name', 'required' => true]); $this->addElement('text', 'nickname', ['label' => 'Nickname', 'required' => true]); $this->_initEmailField(); $this->addElement('phone', 'directPhone', ['label' => 'Direct Phone Number', 'required' => true]); $this->addElement('phone', 'cellPhone', ['label' => 'Cell Phone Number']); $this->addSubForm($this->createAddress(['header' => 'Mailing Address']), 'mailing'); $this->addJobTitleElement(); $this->addElement('text', 'supervisorName', ['label' => 'Name of Supervisor']); if ($this->hasPasswordFields()) { $this->_initPasswordFields(); } return $this; } public function addJobTitleElement() { $options = [ 'label' => 'Job Title', 'multiOptions' => ['' => 'Select One'] + $this->_getJobTitles(), 'mode' => $this->_jobTitleElementMode, ]; if (($companyElement = $this->getElement('companyId'))) { $options['parentElementId'] = $companyElement->getId(); $options['queryUrl'] = Module::getJobTitleApiUrl(); } $this->addElement('SelectOther', 'jobTitle', $options); return $this; } protected function _getJobTitles() { return $this->_getDataObj()->getCompanyJobTitles4Select($this->getCompanyId()); } protected function createAddress($options = []) { $form = new Qs_Form_SubForm(); if (array_key_exists('header', $options)) { $form->setLegend($options['header']); } $this->_addAddressFields($form, Qs_Array::get($options, 'required', false)); return $form; } protected function _addAddressFields(Qs_Form $form, $required = true, $namePrefix = '') { $getName = function ($name) use ($namePrefix) { return ($namePrefix) ? ($namePrefix . ucfirst($name)) : $name; }; $form->addElement('text', $getName('address'), ['label' => 'Address', 'required' => $required]); $form->addElement('text', $getName('address2'), ['label' => 'Address 2']); $form->addElement('text', $getName('city'), ['label' => 'City', 'class' => 'city', 'required' => $required]); $form->addElement( 'select', $getName('state'), [ 'label' => 'State', 'class' => 'state', 'required' => $required, 'multiOptions' => ['' => 'Select One'] + State::getGrouped4Select() ] ); $form->addElement( 'zip', $getName('zip'), [ 'label' => 'Zip', 'class' => 'zip', 'required' => $required ] ); //Remove PostCode validator to allow USA and CAN states $form->getElement($getName('zip'))->removeValidator('PostCode'); return $this; } protected function _getStates4Select() { if (null === $this->_states4Select) { $this->_states4Select = $this->_getDataObj()->getDState4Select(); } return $this->_states4Select; } }