Ejemplo n.º 1
0
 protected function createComponentItemForm()
 {
     $form = new MyAppForm();
     $form->enableAjax();
     $form->enableAjaxFileUpload();
     if (!is_null($this->getTranslator())) {
         $form->setTranslator($this->getTranslator());
     }
     $form->getRenderer()->wrappers['label']['requiredsuffix'] = " *";
     $form->addGroup();
     $form->addText('username', 'User Name')->addRule($form::FILLED)->addRule($form::MIN_LENGTH, NULL, 3)->addRule($form::MAX_LENGTH, NULL, 30)->getControlPrototype()->data('nette-check-url', $this->link('checkAvailability!', array('__NAME__', 'user')))->class[] = 'checkAvailability';
     $form->addText('firstname', 'First Name')->addRule($form::FILLED)->addRule($form::MIN_LENGTH, NULL, 2)->addRule($form::MAX_LENGTH, NULL, 70);
     $form->addText('lastname', 'Last Name')->addRule($form::FILLED)->addRule($form::MIN_LENGTH, NULL, 2)->addRule($form::MAX_LENGTH, NULL, 70);
     if ($this->getAction() === 'add') {
         $form->addPassword('password', 'Password')->addRule($form::FILLED)->addRule($form::MIN_LENGTH, NULL, 6);
         $form->addPassword('password2', 'Confirm Password')->addRule($form::FILLED, 'Confirm user password!')->addRule($form::EQUAL, 'No match for passwords!', $form['password']);
     } elseif ($this->getAction() === 'edit') {
         $form->addPassword('password', 'Password')->setOption('description', 'Fill in only if you want to change current password')->addCondition($form::FILLED)->addRule($form::MIN_LENGTH, NULL, 6);
         $form['password']->getControlPrototype()->autocomplete('off');
         $form->addPassword('password2', 'Confirm Password')->setOption('description', 'Fill in only if you want to change current password')->addConditionOn($form['password'], $form::FILLED)->addRule($form::FILLED, 'Confirm your password!')->addRule($form::EQUAL, 'No match for passwords!', $form['password']);
     }
     $form->addText('email', 'E-Mail')->setEmptyValue('@')->addRule($form::FILLED)->addRule($form::EMAIL)->addRule($form::MAX_LENGTH, NULL, 60);
     if ($this->isClientMode) {
         //		    $form->addSelect('roles', 'Role', $this->prepareRoles())
         $form->addSelect('roles', 'Role', BaseModel::prepareSelect($this->prepareRoles()))->skipFirst()->addRule($form::FILLED);
     } else {
         $form->addMultiSelect('roles', 'Role', $this->prepareRoles(), 7)->addRule($form::FILLED)->addCondition(Form::EQUAL, UsersModel::$clientRolesId)->toggle('supervisorBox')->addCondition(Form::EQUAL, UsersModel::UL_CLIENT_ID)->toggle('clientLogoBox');
         // client as supervisor group
         $form->addGroup()->setOption('container', Html::el('div')->id('supervisorBox'));
         $form->addSelect('supervisor_id', 'Client', $this->prepareClients())->skipFirst()->addConditionOn($form['roles'], Form::EQUAL, UsersModel::$clientRolesId)->addRule(Form::FILLED);
         // client logo group
         $form->addGroup()->setOption('container', Html::el('div')->id('clientLogoBox'));
         // when editing logo is optional
         if ($this->getParam('action') == 'edit') {
             $form->addFile('client_logo', "Client's logo")->setOption('description', 'Choose photo only if you want to change current one')->addCondition(Form::FILLED)->addRule(MyAppForm::SUFFIX, 'Logo must be image (jpg, gif, png)', 'jpg, gif, png')->addRule(Form::MAX_FILE_SIZE, 'File attachment exceeds maximum file size ' . Environment::getConfig("upload")->max_file_size . 'B', Environment::getConfig("upload")->max_file_size);
         } else {
             $form->addFile('client_logo', "Client's logo")->addConditionOn($form['roles'], Form::EQUAL, UsersModel::UL_CLIENT_ID)->addRule(Form::FILLED)->addRule(MyAppForm::SUFFIX, 'Logo must be image (jpg, gif, png)', 'jpg, gif, png')->addRule(Form::MAX_FILE_SIZE, 'File attachment exceeds maximum file size ' . Environment::getConfig("upload")->max_file_size . 'B', Environment::getConfig("upload")->max_file_size);
         }
     }
     $form->addGroup();
     $form->addCheckbox('send_email', 'Send notification email');
     $form->addSubmit('save', 'Add User')->getControlPrototype()->class[] = 'ok-button';
     $_this = $this;
     $form->addSubmit('cancel', 'Cancel')->setValidationScope(NULL)->getControlPrototype()->class[] = 'cancel-button';
     $form['cancel']->onClick[] = function () use($_this) {
         $_this->refresh(null, 'add', array(), true);
     };
     $form->onSubmit[] = callback($this, 'itemFormSubmitted');
     //		$form['save']->onClick[] = callback($this, 'itemFormSubmitted');
     return $form;
 }