support

Account creation and guest checkout validation hooks

Started by yonghan, November 04, 2015, 11:28:39 AM

Previous topic - Next topic

yonghan

Hi admins, i wonder if it's possible to hook into the storefront controller or model validation function without the need to modify the default code? If it's not possible to do so in the moment, please consider to add it in the next release so it's possible to mark certain account registration field as required or not and other tweaks. Thanks a lot.

abolabo

to create and check posted data you should to create our own model with data checks and call it from your hook
for ex.
public function onControllerPagesAccountCreate_InitData(){

$that =& $this->baseObject;

     
if($that->request->is_POST()){
$that->loadModel('sale/your_model');
                        //check your attributes (fields) first
$errors = $that->model_sale_your_model->validateAttributesForCreate(
$that->request->post['attributes']
);

                       
if(!empty($errors)){
$that->loadModel('account/customer');
$base_errors = $that->model_account_customer->validateRegistrationData($that->request->post);

//redirect for case when base form data is valid but custom attribute value does not
if(!$base_errors){
header('Location: ' . $that->html->getSecureURL('account/create'));
exit();
}
}
}
}
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan


yonghan

Hi abolabo, i can't get the errors shown. And when it redirects, the existing data in the fields are gone, here are my codes:


public function onControllerPagesAccountCreate_InitData() {
          $that=$this->baseObject;
          $that->loadModel('telephone_option/customer');
          $that->loadModel('account/customer');
          $request_data = $that->request->post;
          if ($that->request->is_POST()) {
              //check your attributes (fields) first
              $errors = $that->model_telephone_option_customer->validateRegister($that->request->post['telephone'],$that->request->post['fax'],$that->request->post['company']);
              if(!empty($errors)){
                  $base_errors = $that->model_account_customer->validateRegistrationData($request_data);
                  //redirect for case when base form data is valid but custom attribute value does not
                  if(!$base_errors){
                      $this->errors=$errors;
                      $this->baseObject->log->write(var_export($errors,true));
                      header('Location: ' . $that->html->getURL('account/create'));
                      exit();
                  }
              }
          }
}
 



public function onControllerPagesAccountCreate_UpdateData() {
      $that=$this->baseObject;
      if ($this->baseObject_method='main') {
          $phone = $that->config->get('phone_option_mandatory_status');
          $fax = $that->config->get('fax_option_mandatory_status');
          $company = $that->config->get('company_option_mandatory_status');
          if($that->config->get('prevent_email_as_login')){
              $this->data['noemaillogin'] = true;
          }

          $form = new AForm();
          $form->setForm(array( 'form_name' => 'AccountFrm' ));
          $this->data['form'][ 'form_open' ] = $form->getFieldHtml( array( 'type' => 'form',
              'name' => 'AccountFrm',
              'action' => $that->html->getSecureURL('account/create')));

          if($that->config->get('prevent_email_as_login')){ // require login name
              $this->data['form'][ 'loginname' ] = $form->getFieldHtml( array(
                  'type' => 'input',
                  'name' => 'loginname',
                  'value' => $that->request->post['loginname'],
                  'required' => true ));
          }
          $this->data['form'][ 'firstname' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'firstname',
              'value' => $that->request->post['firstname'],
              'required' => true ));
          $this->data['form'][ 'lastname' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'lastname',
              'value' => $that->request->post['lastname'],
              'required' => true ));
          $this->data['form'][ 'email' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'email',
              'value' => $that->request->get_or_post('email'),
              'required' => true ));
          $phoneStatus=($phone=='1' ? true : false);
          $this->data['form'][ 'telephone' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'telephone',
              'value' => $that->request->post['telephone'],
              'required' => $phoneStatus
          ));
          $faxStatus=($fax=='1' ? true : false);
          $this->data['form'][ 'fax' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'fax',
              'value' => $that->request->post['fax'],
              'required' => $faxStatus ));
          $companyStatus=($company=='1' ? true : false);
          $this->data['form'][ 'company' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'company',
              'value' => $that->request->post['company'],
              'required' => $companyStatus ));
          $this->data['form'][ 'address_1' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'address_1',
              'value' => $that->request->post['address_1'],
              'required' => true ));
          $this->data['form'][ 'address_2' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'address_2',
              'value' => $that->request->post['address_2'],
              'required' => false ));
          $this->data['form'][ 'city' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'city',
              'value' => $that->request->post['city'],
              'required' => true ));
          $this->data['form'][ 'postcode' ] = $form->getFieldHtml( array(
              'type' => 'input',
              'name' => 'postcode',
              'value' => $that->request->post['postcode'],
              'required' => true ));
          $that->loadModel('localisation/country');
          $countries = $that->model_localisation_country->getCountries();
          $options = array("FALSE" => $that->language->get('text_select') );
          foreach($countries as $item){
              $options[ $item['country_id'] ] = $item['name'];
          }
          $this->data['form'][ 'country_id' ] = $form->getFieldHtml( array(
              'type' => 'selectbox',
              'name' => 'country_id',
              'options'=>$options,
              'value' => ( isset($that->request->post['country_id']) ? $that->request->post['country_id'] : $that->config->get('config_country_id')),
              'required' => true ));
          $that->view->assign('zone_id', $that->request->post['zone_id'], 'FALSE' );
          $this->data['form'][ 'zone_id' ] = $form->getFieldHtml( array(
              'type' => 'selectbox',
              'name' => 'zone_id',
              'required' => true ));

          $this->data['form'][ 'password' ] = $form->getFieldHtml( array(
              'type' => 'password',
              'name' => 'password',
              'value' => $that->request->post['password'],
              'required' => true ));
          $this->data['form'][ 'confirm' ] = $form->getFieldHtml( array(
              'type' => 'password',
              'name' => 'confirm',
              'value' => $that->request->post['confirm'],
              'required' => true ));

          $newsletter = '';
          $this->data['form'][ 'newsletter' ] = $form->getFieldHtml( array(
              'type' => 'radio',
              'name' => 'newsletter',
              'value' => (!is_null($that->request->get_or_post('newsletter')) ? $that->request->get_or_post('newsletter') : -1),
              'options' => array(
                  '1' => $that->language->get('text_yes'),
                  '0' => $that->language->get('text_no'),
              ) ));

          $agree = isset($that->request->post['agree']) ? $that->request->post['agree'] : FALSE;
          $this->data['form'][ 'agree' ] = $form->getFieldHtml( array(
              'type' => 'checkbox',
              'name' => 'agree',
              'value' => 1,
              'checked' => $agree ));

          $this->data['form'][ 'continue' ] = $form->getFieldHtml( array(
              'type' => 'submit',
              'name' => $that->language->get('button_continue') ));


          $this->data['error_warning'] = $this->errors['warning'];
          $this->data['error_loginname'] = $this->errors['loginname'];
          $this->data['error_firstname'] = $this->errors['firstname'];
          $this->data['error_lastname'] = $this->errors['lastname'];
          $this->data['error_email'] = $this->errors['email'];
          $this->data['error_telephone'] = $this->errors['telephone'];
          $this->data['error_fax'] = $this->errors['fax'];
          $this->data['error_company'] = $this->errors['company'];
          $this->data['error_password'] = $this->errors['password'];
          $this->data['error_confirm'] = $this->errors['confirm'];
          $this->data['error_address_1'] = $this->errors['address_1'];
          $this->data['error_city'] = $this->errors['city'];
          $this->data['error_postcode'] = $this->errors['postcode'];
          $this->data['error_country'] = $this->errors['country'];
          $this->data['error_zone'] = $this->errors['zone'];

          $this->data['action'] = $that->html->getSecureURL('account/create') ;
          $this->data['newsletter'] = $that->request->post['newsletter'];

          if ($that->config->get('config_account_id')) {

              $that->loadModel('catalog/content');
              $content_info = $that->model_catalog_content->getContent($that->config->get('config_account_id'));

              if ($content_info) {
                  $text_agree = $that->language->get('text_agree');
                  $this->data['text_agree_href'] = $that->html->getURL('r/content/content/loadInfo', '&content_id=' . $that->config->get('config_account_id'));
                  $this->data['text_agree_href_text'] = $content_info['title'];
              } else {
                  $text_agree = '';
              }
          } else {
              $text_agree = '';
          }
          $this->data['text_agree'] = $text_agree;

          $text_account_already = sprintf($that->language->get('text_account_already'), $that->html->getSecureURL('account/login') );
          $this->data['text_account_already'] = $text_account_already;

          $that->view->batchAssign($this->data);
          $that->processTemplate('');
      }
  }


I wonder if there are errors on my codes. Thanks

abolabo

when you do redirect it's a GET method. Account/create controller shows field values after POST.
i suggest to use session to move your custom post-data into account/create core-controller. Just write it before redirect into your own session-variable and restore it from session into $this->request->post array in your hook (on GET requests only).

Also you can edit public property $errors of core-controller from your hook to show errors for each your field.
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan

Hi abolabo, I have followed your suggestion to use session. It works if i turn on the extension, but when i turn off the extension. the additional errors are showed in the account create page. I wonder if AbanteCart team would consider about making the validation part as a function where we can hook it? Thanks a lot.

abolabo

Quote from: handoyo on November 08, 2015, 07:30:58 AM
Hi abolabo, I have followed your suggestion to use session. It works if i turn on the extension, but when i turn off the extension. the additional errors are showed in the account create page. I wonder if AbanteCart team would consider about making the validation part as a function where we can hook it? Thanks a lot.

you should to check your extension status in each hook calls via checking for config
i mean if(!$this->config->get('your_extension_status')){ return null; }
"No one is useless in this world who lightens the burdens of another."
― Charles Dickens

yonghan

Hi abolabo, I have used that and still no joy. Seems like I will find another way to do it. Thanks

Forum Rules Code of conduct
AbanteCart.com 2010 -