I'm stuck with formhandler on creating multiple forms on one page. What I've done so far is the following Code in my Setup Variables
plugin.Tx_Formhandler.settings {
debug = 0
formValuesPrefix = formhandler
singleErrorTemplate {
totalWrap = <div class="error">|</div>
singleWrap = <span class="message">|</span>
}
preProcessors {
1.class = Tx_Formhandler_PreProcessor_LoadDefaultValues
1 {
config.1 {
name.defaultValue = Name
email.defaultValue = E-Mail
}
}
}
validators {
1.class = Validator_Default
1.config.fieldConf {
name {
errorCheck.1 = required
}
email {
errorCheck.1 = required
errorCheck.2 = email
}
}
}
singleErrorTemplate {
totalWrap = <div class="error">|</div>
singleWrap = <span class="message">|</span>
}
finishers {
# Finisher_Mail sends emails to an admin and/or the user.
1.class = Finisher_Mail
# Finisher_Redirect will redirect the user to another page after the form was submitted successfully.
5.class = Finisher_Redirect
5.config {
redirectPage = {$formhandlerExamples.basic.contact-form.redirectPage}
}
}
}
But now I want to add a second form to my site. If I simply add a second plugin.Tx_Formhandler.settings {} with a new formValuesPrefix it is not working. I am only getting a "Caught exception: Template not found" error.
When I remove the first form and the first settings, my form is working.
I could use a tutorial or something on how to add multiple forms with formhandler on one page.
I've finally figured that out. What you need to do is, work with settings.predef instead of standard settings. For example
plugin.Tx_Formhandler.settings.predef.formhandler {
# This name appears in the dropdown selector in plugin record
name = Newsletter Contact
debug = 0
formValuesPrefix = formhandler
singleErrorTemplate {
totalWrap = <div class="error">|</div>
singleWrap = <span class="message">|</span>
}
validators {
1.class = Validator_Default
1.config.fieldConf {
name {
errorCheck.1 = required
}
email {
errorCheck.1 = required
errorCheck.2 = email
}
}
}
singleErrorTemplate {
totalWrap = <div class="error">|</div>
singleWrap = <span class="message">|</span>
}
finishers {
# Finisher_Mail sends emails to an admin and/or the user.
1.class = Finisher_Mail
# Finisher_Redirect will redirect the user to another page after the form was submitted successfully.
5.class = Finisher_Redirect
5.config {
redirectPage = 8
}
}
}
Now you will find in your module this setting in a dropdown. If you create more of those, you'll have more settings to select. And every Form needs it's own setting.predef.
Related
i need to add a select with defined in admin countries to a prestashop registration form.
Any hint on how to do this?
Simple prestashop 1.7.8.2
Was searching the web, but no strict answers.
You can use additionalCustomerFormFields hook that is placed inside CustomerFormatter class.
Example usage:
https://github.com/PrestaShop/ps_emailsubscription/blob/dev/ps_emailsubscription.php#L1005
Well, i managed to sort it out myself also by:
Adding select with countries that are added to Presta in file /override/classes/form/CustomerFormatter.php
$countries = Country::getCountries((int)$this->language->id, true, false, false);
if (count($countries) > 0) {
$countryField = (new FormField)
->setName('id_country')
->setType('countrySelect')
->setLabel($this->translator->trans('Country', [], 'Shop.Forms.Labels'))
->setRequired(true);
foreach ($countries as $country) {
$countryField->addAvailableValue(
$country['id_country'],
$country['country']
);
}
$format[$countryField->getName()] = $countryField;
}
Adding to file /override/classes/AuthController.php right below:
if ($hookResult && $register_form->submit()) {
this code:
//address saving
$customer = new Customer();
$customer = $customer->getByEmail($register_form->getCustomer()->email);
$address = new Address(
null,
$this->context->language->id
);
$address->id_country = (int) Tools::getCountry();
$address->address1 = Tools::getValue('address1');
$address->postcode = Tools::getValue('postcode');
$address->city = Tools::getValue('city');
$address->phone = Tools::getValue('phone');
$address->firstname = $customer->firstname;
$address->lastname = $customer->lastname;
$address->id_customer = (int) $customer->id;
$address->id_state = 0;
$address->alias = $this->trans('My Address', [], 'Shop.Theme.Checkout');
if($address->save()){
$should_redirect = true;
} else {
$customer->delete();
$this->errors[] = $this->trans('Could not update your information, please check your data.', array(), 'Shop.Notifications.Error');
$this->redirectWithNotifications($this->getCurrentURL());
}
}
The above code is responsible to add new address within provided data. In My case additional is only country, but if You want to add more address data the fields should be added again in CustomerFormatter.php
Credits for several parts of the code:
https://prestapros.com/en/blog/additional-fields-for-registration-form-prestashop-1-7
And:
https://www.prestashop.com/forums/topic/621262-prestashop-17-add-address-in-registration-form/?do=findComment&comment=3380394
Cheers!
My wordpress website capturing url UTM from Google Ads via hidden field of Contact Form 7. But Contact Form 7 capturing keywords with %20 instead of space (Ex. life%20insurance%20policy) and sending to assigned email addresses.
How can I replace %20 with space?
You should make it url decoded value, so if its value saved in hidden input with %20 you’d better to do it via js like this:
var form = document.getElementsByClassName('wpcf7-form')[0];
form.addEventListener('submit', function(evt) {
form.elements['google_adv'].value = decodeURIComponent(form.elements['google_adv'].value);
}, { capture: true });
But if the changing process is in backend side you may try with php codes instead:
add_action( 'wpcf7_before_send_mail', 'some_function_name', 1 );
function some_function_name( $contact_form ) {
$wpcf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$data = array();
$data['posted_data'] = $submission->get_posted_data();
$google = $data['posted_data']['google_adv']; // just enter the field name here
$mail = $wpcf7->prop('mail');
if($google !=''){
$mail['body'] = str_replace($google, urldecode($google), $mail['body']);
$mail['body'] = str_replace('[google_adv]', urldecode($google), $mail['body']);
}
$wpcf7->set_properties(array(
"mail" => $mail
));
return $wpcf7;
}
}
I am adding specific js file to the front end from my wordpress plugin.
For Example :
if($wp->query_vars["pagename"] == 'pagename1'){
include js file 1.
}else if($wp->query_vars["pagename"] == 'pagename1'){
include js file 2.
}
But now i am not able to get the page name in$wp->query_vars.
Thank you
Balaji
Try this:
$post = get_post();
$post_name = $post->post_name;
To add javascript you could do this:
add_action( 'wp_enqueue_scripts', 'addSomescripts' );
function addSomeScripts() {
$post = get_post( );
$post_name = $post->post_name;
if($post_name == 'pagename1') {
wp_enqueue_script('script1', '/pathtoscript1.js');
} else if($post_name == 'pagename2') {
wp_enqueue_script('script2', '/pathtoscript2.js');
}
}
I ll reframe my question based on some research I have done?
I need to store lot of errors separately like $_SESSION['client_error'],$_SESSION['state_error'] etc.
According to zend documentation do I have to store it like this for each error?
$client_error = new Zend_Session_Namespace(''client_error);
$state_error = new Zend_Session_Namespace('state_erro'); and so on?
This is my code in the controller.
I am storing it as
$this->view->state_error_message=$state_error;
After I echo $this->state_error in the view I want to unset it.
Ok here are few more things I tried:
In the controller in policyInfoAction:
session_start();
$error_message = new Zend_Session_Namespace('error_message');
$error_message="TEST";
$this->view->error_message=$error_message;
$this->_redirect('/pdp/client-info/');
In the view in client-info:
session_start();
<?php echo $this->error_message; ?>
This returns nothing.
Ok this is my updated code:
public function clientInfoAction()
{
$errors = new Zend_Session_Namespace('errors');
// get the error arrays
$client_errors = (isset($errors->client_error)) ? $errors->client_error : array();
$state_errors = (isset($errors->state_error)) ? $errors->state_error : array();
unset($errors->client_error, $errors->state_error); // delete from the session
// assign the values to the view
$this->view->client_errors = $client_errors;
$this->view->state_errors = $state_errors;
}
public function policyInfoAction()
{
if (count($arrErrors) > 0)
{
// The error array had something in it. There was an error.
$strError="";
foreach ($arrErrors as $error)
{
$strError="";
$errors->client_error = array();
$errors->state_error = array();
foreach ($arrErrors as $error)
{
$strError .= $error;
// to add errors to each type:
$errors->client_error['client_error'] = $strError;
$errors->client_error[] = $strError;
$this->_redirect('/pdp/client-info/');
}
}
}
When i echo $this->client_errors I get 'Array'
Here is some advice and suggestions that can hopefully get you on the right track.
First, when using Zend_Session and/or Zend_Session_Namespace, you never want to use PHP's session_start() function1. If you start a session with session_start(), and then try to use Zend_Session, it will throw an exception that another session already exists.
Therefore, remove all session_start() calls from your Zend Framework application.
Second, you mentioned you had a lot of messages you need to store, so this may not be the right thing for you, but see the FlashMessenger action helper. This allows you to set a message in a controller, and then access it on the next page request. The messages only live for one page hop, so after the next page load, they are deleted. You can store many messages with the FlashMessenger, but your access to them is not very controlled. You could use multiple flash messengers each in differen namespaces also.
To solve your problem in particular, you could just do something like this:
// in controller that is validating
$errors = new Zend_Session_Namespace('errors');
$errors->client_error = array();
$errors->state_error = array();
// to add errors to each type:
$errors->client_error['some_error'] = 'You had some error, please try again.';
$errors->client_error['other_error'] = 'Other error occurred.';
$errors->client_error[] = 'Other error, not using a named key';
$errors->state_error[] = MY_STATE_PARSING_0;
What is happening here is we are getting a session namespace called errors creating new properties for client_error and state_error that are both arrays. You don't technically have to use multiple Zend_Session_Namespaces.
Then to clear the messages on the next page load, you can do this:
// from controller again, on the next page load
$errors = new Zend_Session_Namespace('errors');
// get the error arrays
$client_errors = (isset($errors->client_error)) ? $errors->client_error : array();
$state_errors = (isset($errors->state_error)) ? $errors->state_error : array();
unset($errors->client_error, $errors->state_error); // delete from the session
// assign the values to the view
$this->view->client_errors = $client_errors;
$this->view->state_errors = $state_errors;
See also the source code for Zend_Controller_Action_Helper_FlashMessenger which can give you some idea on managing data in session namespaces.
I don't know if this will help you or not but here is the code for a controller that just takes an id from a form a gathers data based on that id an assigns that data to the session (to be used throughout the module) and then unsets that data when appropriate. and Never leaves the Index page.
<?php
class Admin_IndexController extends Zend_Controller_Action
{
//zend_session_namespace('location')
protected $_session;
/**
*set the layout from default to admin for this controller
*/
public function preDispatch() {
$this->_helper->layout->setLayout('admin');
}
/**
*initiaize the flashmessenger and assign the _session property
*/
public function init() {
if ($this->_helper->FlashMessenger->hasMessages()) {
$this->view->messages = $this->_helper->FlashMessenger->getMessages();
}
//set the session namespace to property for easier access
$this->_session = new Zend_Session_Namespace('location');
}
/**
*Set the Station and gather data to be set in the session namespace for use
* in the rest of the module
*/
public function indexAction() {
//get form and pass to view
$form = new Admin_Form_Station();
$form->setAction('/admin/index');
$form->setName('setStation');
$this->view->station = $this->_session->stationName;
$this->view->stationComment = $this->_session->stationComment;
$this->view->form = $form;
try {
//get form values from request object
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$data = (object)$form->getValues();
//set session variable 'station'
$this->_session->station = $data->station;
$station = new Application_Model_DbTable_Station();
$currentStation = $station->getStation($this->_session->station);
$this->_session->stationName = $currentStation->station;
$this->_session->stationComment = $currentStation->comment;
//assign array() of stations to session namespace
$stations = $station->fetchAllStation();
$this->_session->stations = $stations;
//assign array() of bidlocations to session namespace
$bidLocation = new Application_Model_DbTable_BidLocation();
$bidLocations = $bidLocation->fetchAllBidLocation($this->_stationId);
$this->_session->bidLocations = $bidLocations;
$this->_redirect($this->getRequest()->getRequestUri());
}
}
} catch (Zend_Exception $e) {
$this->_helper->flashMessenger->addMessage($e->getMessage());
$this->_redirect($this->getRequest()->getRequestUri());
}
}
/**
*Unset Session values and redirect to the index action
*/
public function changestationAction() {
Zend_Session::namespaceGet('location');
Zend_Session::namespaceUnset('location');
$this->getHelper('Redirector')->gotoSimple('index');
}
}
just to be complete i start the session in the bootstrap. On the theory that if I need it great if not no harm.
protected function _initsession() {
//start session
Zend_Session::start();
}
this is all the view is:
<?php if (!$this->station): ?>
<div class="span-5 prepend-2">
<?php echo $this->form ?>
</div>
<div class="span-10 prepend-2 last">
<p style="font-size: 2em">Please select the Station you wish to perform Administration actions on.</p>
</div>
<?php else: ?>
<div class="span-19 last">
<?php echo $this->render('_station.phtml') ?>
</div>
<?php endif; ?>
I am working on a purchase system in my assignment and trying to solve the problem by using a session to store data in the process.
Although I'm experiencing a problem in Mozilla Firefox, which cannot for some reason work with the session I have created. There's most likely no doubt that I must have made some kind of mistake.
The process is as follows:
User fills form -> Clicks submit -> [Validation process] -> User reviews confirm page
Here is the relevant code from the controller:
public function indexAction() {
$this->gatewayForm = new Payment_Form_Gateway;
$save = $this->validate();
$this->view->gatewayForm = $save['form'];
$this->view->alert = $save['alert'];
}
public function validate() {
# get form
$form = $this->gatewayForm;
if ($this->_request->isPost()) {
# get params
$data = $this->_request->getPost();
# check validate form
if ($form->isValid($data)) {
$session = new Zend_Session_Namespace('formData'); // name space creation
$session->data = $data;
$this->_helper->redirector('confirm', 'gateway', 'payment');
} else {
$alert = array('Pay failed');
}
$form->populate($data);
}
return array('form' => $form, 'alert' => empty($alert) ? null : $alert );
}
public function confirmAction() {
$this->_helper->viewRenderer->setNoRender(true); // disable std. view
$session = new Zend_Session_Namespace('formData');
$data = $session->data;
if(isset($data)) {
$this->_helper->viewRenderer->setNoRender(false);
} else {
$this->_helper->redirector('index', 'gateway', 'payment');
}
}
Things go wrong in the confirmAction in Firefox, the session namespace seems to be empty? Although this does not occur in Safari, Chrome, IE etc.
Thanks in advance.
I reinstalled Firefox and removed config and cache files which did the magic. Problems solved!