Prestashop Add extra fields to registration form - php

I am building a prestashop site
The corporate buyers cannot register for an account to view the products unless they can enter a valid EIN number or a valid Duns & Brandstreet number in registration.
How to make it possible?
Also any other e-commerce software that can help me solve this by switching to it?

All you really need to do is add the field to the template and then override the AuthController with additional code to handle you new field. e.g.
<?php
class AuthController extends AuthControllerCore
{
public function preProcess()
{
// Additional pre-processing for the new form field
if (Tools::isSubmit('submitAccount'))
{
if (!MyDBNumberValidationClass::verifyvalidnumber(Tools::getValue('db_number_field', 0)))
$this->errors[] = Tools::displayError('The Dun and Bradstreet number you entered is invalid.');
}
parent::preProcess();
}
}
I'm assuming that you're doing complex verification of the number which is why the static call to MyDBNumberValidationClass::verifyvalidnumber() is in the above, but it could equally by any simple test or an additional function in your AuthController override class definition above that validates. If you add two fields (i.e. the EIN number too) then just alter the logic to handle generating an error only if both are invalid; success of the form validation is based on $this->errors being empty.
This is only part of a general solution since it only handles validating the extra fields. If you need to actually do something with the data entered then the best way is to write a little handler module that installs itself on hookCreateAccount e.g.
public function hookCreateAccount($params)
{
// Get the field data entered on the form
$DB_number = $params['_POST']['db_number_field'];
// Your custom processing...
}
Note that there's no way to back out of the account creation from here so you will want to add code to email the store owner should there be a problem.

Related

Monolog add additional data at the end when something is logged

Coming from Yii2 at the end of each request when something is logged, Yii2 adds additional data to your log, for example the $_POST data so you know what paramaters caused the issue.
Is there a way to add these information in Monolog too?
I don't want to use a Processor as it includes all those parameters to each and every record. I would just like to add an additional string in case something is logged at the end of the log after all messages are included/send (for example via BufferHandler when sending Mails via SwiftMailerHandler)
I found a way..
I can extend the default HtmlFormatter or LineFormatter and include my custom information that way
namespace modules\myspa\monolog;
class HtmlFormatter extends \Monolog\Formatter\HtmlFormatter
{
public function formatBatch(array $records): string
{
$formatBatch = parent::formatBatch($records);
$formatBatch .= '<br><br>Foobar.. add some custom info';
return $formatBatch;
}
}
Then I can add this one as a formatter
$swiftMailHandler = new SwiftMailerHandler($swiftMailer, $message, \Monolog\Logger::ERROR);
// add my formatter
$swiftMailHandler->setFormatter(new HtmlFormatter());
$psrLogger->pushHandler(new BufferHandler($swiftMailHandler));

Laravel 4 custom validation rule

I am little confused as to whether a custom validation rules needs to return true or false to fire.
I am validating an email address to make that it does already belong to another model via a relationship.
Validator::extend('email_exists', function($attribute, $value, $parameter){
$user = User::where('email', '=', $value)->with('clients')->first();
//Does the user exits, and are they already a member of this client?
//We know this by looking at the client id, and comparing them the current ID.
if($user != NULL) {
if(in_array(Input::get('client_id'), $user->clients->lists('id'))) {
return false;
} else {
return true;
}
} else {
return true;
}
});
What I am trying to above is, find if a user exists with the entered email, if it does exist, then check if the email is related to any clients, and if any of those clients id match the client id in the POST, if the email exists and is related to client id in the POST I want to return an error, other wise I happy for the POST to processed.
At the moment, I think I am allowing anything through. Am I using the custom rule correctly, and what should return if I want to throw an error?
I think you're approaching this the wrong way. Instead of the stress of extending the Validator class for a singular use case, you should consider simply adding a message to the message bag with your input element as the key. e.g.
$validator->messages()->add('client_id', 'This email is already associated with a client');
Tip: I usually add a rules array to my models and inject a Validator class so I can easily validate the model without having to create an instance of the Validator or writing a rules array all the time.

Payment module return URI

I am creating a payment module for our payment gateway...
So far I have 1) set up the back-end and 2) used the hookPayment() method to display a hidden form at part 5 of the checkout process (http://prestashop.dev/order). This will then redirect off to my gateway with all the required information. Good job.
The next part is the part I am struggling with. I don't understand what the return URIs are for payment-confirmation - just to give the customer some information on the status of the order (and maybe also update the back-office?).
For now, I have just a very simple method;
public function hookPaymentReturn()
{
// if (!$this->active) {
// return null;
// }
return $this->display(__FILE__, 'views/templates/front/confirmation.tpl');
}
in my main module file. I just want to get to this on the browser... I'll start worrying about POST'ed values after this. But for now I just don't know the URI. What will it be?? Do I need to register the route somehow?
Most payment modules confirmation.tpl get hooked into the order-confirmation.tpl of your theme which in turn gets called by the OrderConfirmationController.php.
This can be accessed by:
[YOUR_BASE_URL]/index.php?controller=order-confirmation&[some_stuff].
The [some_stuff] part is important though since the controller performs a series of validations and will redirect you somewhere else if these are not met

set custom validation messages for different fields in Codeigniter

I am working on already built codeigniter application. I am working on enhancements. One of the enhancement is to change the validation messages. So I checked the validation messages are drive through CI_Form_validation library in codeigniter. I want manage to set the custom messages using the "set_message".
$this->form_validation->set_message('required', 'Please enter %s.');
This is triggering for all fields where the values is empty. Which is good. But I have few select fields and radio buttons the application. For those message should change from "Please enter %s" to "Please select %s". I tried callback methods mentioned in the " How can I setup custom error messages for each form field in Codeigniter? "
And also I have tried the method mentioned in the following links
1) " https://github.com/EllisLab/CodeIgniter/wiki/Custom-Validation-Errors-per-Field ".
2) " http://www.witheringtree.com/2011/09/custom-codeigniter-validation-methods/ ".
Is there any way to set the different custom messages for different fields? If so please give me the suggestion. There is already a file called MY_Form_validation in the application (which is mentioned in the second link) with some custom functions. Custom validations are triggering in that file except the custom function written by me. (I know you think may be I have written an faulty code! But there is only simple echo statement in that function. Just for testing only I have put an echo statement).
You wouldn't be able to create a generic function to do this as codeigniter has no way of knowing how the information was posted as it just receives it as an array.
What you could do is create MY_Form_validation.php in application/libraries
class MY_Form_validation extends CI_Form_validation
{
public function required_select($val)
{
if ($this->required($val) === FALSE) {
$this->set_message('required_select', 'Please select %s');
return FALSE;
}
return TRUE;
}
}
then when creating the rules for form validation
$this->form_validation->set_rules('dropdown', 'Dropdown', 'required_select');
Obviously, replace dropdown with the name of the element.
Hope this helps!

Handling contact form submissions Codeigniter

I've inherited a website built using Codeigniter (v2.1.4). The client has asked for a change, and I'm not sure of the best way to achieve it.
I have the following method in the Main controller that powers a new vans page.
public function new_vans($slug = null){
$this->load->view('inc/header_view');
if($slug === NULL){
//If no slug is provided, show all new vans
$this->load->view('new_vans_view');
}else{
//If there is a slug, just show the selected van, or redirect if nothing returned
$data['new_van'] = $this->Database->getSingle('new_vans', array('slug' => $slug));
if(!empty($data['new_van'])){
$this->load->view('new_van_details_view',$data);
}else{
redirect('/new-vans');
}
}
$this->load->view('inc/footer_view');
}
The client has asked for a contact form to be added to a couple of pages including this one, and my question is, should I create a new method that just handles the contact form submissions? If so, how would I handle sending validation errors back to the page? The contact forms will all have the same fields, so I would guess creating a new method is the way to go?
Partial Views(forms)
Partial views are good for forms, they can be re-used
like your client has requested.
Returning views as data
There is a third optional parameter lets you change the behavior
of the function so that it returns data as a
string rather than sending it to your browser.
This can be useful if you want to process the data in some way.
If you set the parameter to true (boolean) it will return data.
The default behavior is false, which sends it to your browser.
Remember to assign it to a variable if you want the data returned:
$string = $this->load->view('myfile', '', true);
Master layouts
To create a Master layout so you can wrap your views
create a new file inside your views directory
views/master/layout.php
<body>
<?php $this->load->view($view); ?>
</body>
Controller
class someController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->template = 'master/layout';
}
public function index()
{
return $this->load->view($this->template, array(
'view' => 'somecontrollerview',
'contact_form' => $this->load->view('partials/forms/contact', array(), true)
));
}
}
somecontrollerview
Echo out the contact form(string)
<?php echo $contact_form; ?>
Contact Controller
Create a new Controller to handle your form validation
The client has asked for a contact form to be added to a couple of pages including this one, and my question is, should I create a new method that just handles the contact form submissions?
create a new controller and new methods
If so, how would I handle sending validation errors back to the page?
look through the codeigniter documentation for form validation. basically if they have an error you are going to show them a view with the form again. it does not matter which page they came "from".
The contact forms will all have the same fields, so I would guess creating a new method is the way to go?
you need to validate the form fields, hopefully capture the contact info to a database, send an email confirmation to the customer, and send an email to the sales person unless its being done directly from the database, and then show a view with a thank you.
each one of those steps is a separate method.
optionally you can show the email address on the thank you page saying 'we have sent you a copy to the email address: something#gmail.com -- that way if the customer messed up the email address they can go back and correct it.

Categories