Example #1
0
 /**
  * Validate the form.
  */
 private function validateForm()
 {
     // is the form submitted
     if ($this->frm->isSubmitted()) {
         // get fields
         $txtEmail = $this->frm->getField('email');
         $txtPassword = $this->frm->getField('password');
         $chkRemember = $this->frm->getField('remember');
         // required fields
         $txtEmail->isFilled(FL::getError('EmailIsRequired'));
         $txtPassword->isFilled(FL::getError('PasswordIsRequired'));
         // both fields filled in
         if ($txtEmail->isFilled() && $txtPassword->isFilled()) {
             // valid email?
             if ($txtEmail->isEmail(FL::getError('EmailIsInvalid'))) {
                 // get the status for the given login
                 $loginStatus = FrontendProfilesAuthentication::getLoginStatus($txtEmail->getValue(), $txtPassword->getValue());
                 // valid login?
                 if ($loginStatus !== FrontendProfilesAuthentication::LOGIN_ACTIVE) {
                     // get the error string to use
                     $errorString = sprintf(FL::getError('Profiles' . \SpoonFilter::toCamelCase($loginStatus) . 'Login'), FrontendNavigation::getURLForBlock('Profiles', 'ResendActivation'));
                     // add the error to stack
                     $this->frm->addError($errorString);
                     // add the error to the template variables
                     $this->tpl->assign('loginError', $errorString);
                 }
             }
         }
         // valid login
         if ($this->frm->isCorrect()) {
             // get profile id
             $profileId = FrontendProfilesModel::getIdByEmail($txtEmail->getValue());
             // login
             FrontendProfilesAuthentication::login($profileId, $chkRemember->getChecked());
             // update salt and password for Dieter's security features
             FrontendProfilesAuthentication::updatePassword($profileId, $txtPassword->getValue());
             // trigger event
             FrontendModel::triggerEvent('Profiles', 'after_logged_in', array('id' => $profileId));
             // query string
             $queryString = urldecode(\SpoonFilter::getGetValue('queryString', null, SITE_URL));
             // redirect
             $this->redirect($queryString);
         }
     }
 }
 /**
  * Validate the form
  *
  * @return void
  */
 private function validateForm()
 {
     // is the form submitted
     if ($this->frm->isSubmitted()) {
         // validate required fields
         $email = $this->frm->getField('email');
         // validate required fields
         if ($email->isEmail(FL::err('EmailIsInvalid'))) {
             if (FrontendModel::get('mailmotor.member')->isSubscribed($email->getValue())) {
                 $email->addError(FL::err('AlreadySubscribed'));
             }
             // we need to add this because the line below.
             // $this->frm->getErrors() only checks if form errors are set, not if an element in the form has errors.
         } else {
             $this->frm->addError(FL::err('AlreadySubscribed'));
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // build
             $mergeVars = array();
             try {
                 // subscribe the user to our default group
                 FrontendModel::get('mailmotor.member')->subscribe($email->getValue(), null, $mergeVars);
                 // trigger event
                 FrontendModel::triggerEvent('MailMotor', 'after_subscribe', array('email' => $email->getValue()));
                 // redirect
                 $this->redirect(FrontendNavigation::getURLForBlock('MailMotor', 'Subscribe') . '?sent=true#mailMotorSubscribeForm');
             } catch (Exception $e) {
                 // when debugging we need to see the exceptions
                 if (\SPOON_DEBUG) {
                     throw $e;
                 }
                 // show error
                 $this->tpl->assign('mailMotorSubscribeHasError', true);
             }
             // show errors
         } else {
             $this->tpl->assign('mailMotorSubscribeHasFormError', true);
         }
     }
 }
Example #3
0
 /**
  * Validate the form.
  */
 private function validateForm()
 {
     // submitted
     if ($this->frm->isSubmitted()) {
         // does the key exists?
         if (\SpoonSession::exists('formbuilder_' . $this->item['id'])) {
             // calculate difference
             $diff = time() - (int) \SpoonSession::get('formbuilder_' . $this->item['id']);
             // calculate difference, it it isn't 10 seconds the we tell the user to slow down
             if ($diff < 10 && $diff != 0) {
                 $this->frm->addError(FL::err('FormTimeout'));
             }
         }
         // validate fields
         foreach ($this->item['fields'] as $field) {
             // field name
             $fieldName = 'field' . $field['id'];
             // skip
             if ($field['type'] == 'submit' || $field['type'] == 'paragraph' || $field['type'] == 'heading') {
                 continue;
             }
             // loop other validations
             foreach ($field['validations'] as $rule => $settings) {
                 // already has an error so skip
                 if ($this->frm->getField($fieldName)->getErrors() !== null) {
                     continue;
                 }
                 // required
                 if ($rule == 'required') {
                     $this->frm->getField($fieldName)->isFilled($settings['error_message']);
                 } elseif ($rule == 'email') {
                     // only check this if the field is filled, if the field is required it will be validated before
                     if ($this->frm->getField($fieldName)->isFilled()) {
                         $this->frm->getField($fieldName)->isEmail($settings['error_message']);
                     }
                 } elseif ($rule == 'numeric') {
                     // only check this if the field is filled, if the field is required it will be validated before
                     if ($this->frm->getField($fieldName)->isFilled()) {
                         $this->frm->getField($fieldName)->isNumeric($settings['error_message']);
                     }
                 } elseif ($rule == 'time') {
                     $regexTime = '/^(([0-1][0-9]|2[0-3]|[0-9])|([0-1][0-9]|2[0-3]|[0-9])(:|h)[0-5]?[0-9]?)$/';
                     if (!\SpoonFilter::isValidAgainstRegexp($regexTime, $this->frm->getField($fieldName)->getValue())) {
                         $this->frm->getField($fieldName)->setError($settings['error_message']);
                     }
                 }
             }
         }
         // valid form
         if ($this->frm->isCorrect()) {
             // item
             $data['form_id'] = $this->item['id'];
             $data['session_id'] = \SpoonSession::getSessionId();
             $data['sent_on'] = FrontendModel::getUTCDate();
             $data['data'] = serialize(array('server' => $_SERVER));
             // insert data
             $dataId = FrontendFormBuilderModel::insertData($data);
             // init fields array
             $fields = array();
             // loop all fields
             foreach ($this->item['fields'] as $field) {
                 // skip
                 if ($field['type'] == 'submit' || $field['type'] == 'paragraph' || $field['type'] == 'heading') {
                     continue;
                 }
                 // field data
                 $fieldData['data_id'] = $dataId;
                 $fieldData['label'] = $field['settings']['label'];
                 $fieldData['value'] = $this->frm->getField('field' . $field['id'])->getValue();
                 if ($field['type'] == 'radiobutton') {
                     $values = array();
                     foreach ($field['settings']['values'] as $value) {
                         $values[$value['value']] = $value['label'];
                     }
                     $fieldData['value'] = $values[$fieldData['value']];
                 }
                 // clean up
                 if (is_array($fieldData['value']) && empty($fieldData['value'])) {
                     $fieldData['value'] = null;
                 }
                 // serialize
                 if ($fieldData['value'] !== null) {
                     $fieldData['value'] = serialize($fieldData['value']);
                 }
                 // save fields data
                 $fields[$field['id']] = $fieldData;
                 // insert
                 FrontendFormBuilderModel::insertDataField($fieldData);
             }
             $this->get('event_dispatcher')->dispatch(FormBuilderEvents::FORM_SUBMITTED, new FormBuilderSubmittedEvent($this->item, $fields, $dataId));
             // trigger event
             FrontendModel::triggerEvent('FormBuilder', 'after_submission', array('form_id' => $this->item['id'], 'data_id' => $dataId, 'data' => $data, 'fields' => $fields, 'visitorId' => FrontendModel::getVisitorId()));
             // store timestamp in session so we can block excessive usage
             \SpoonSession::set('formbuilder_' . $this->item['id'], time());
             // redirect
             $redirect = SITE_URL . $this->URL->getQueryString();
             $redirect .= stripos($redirect, '?') === false ? '?' : '&';
             $redirect .= 'identifier=' . $this->item['identifier'];
             $redirect .= '#' . $this->formName;
             throw new RedirectException('Redirect', new RedirectResponse($redirect));
         } else {
             // not correct, show errors
             // global form errors set
             if ($this->frm->getErrors() != '') {
                 $this->tpl->assign('formBuilderError', $this->frm->getErrors());
             } else {
                 // general error
                 $this->tpl->assign('formBuilderError', FL::err('FormError'));
             }
         }
     }
 }