I have a set of custom fields as part of the user that need to be validated by an API before being saved. For this I am using the user_profile_update_errors hook. The errors are correctly returned to the admin, but the fields are saved regardless of the errors. According to the description of this hook the fields should only be saved if the error array is empty, but this does not seem to be the case.
I have created a very simple script to test this. It contains the following code:
if (!function_exists("testEditUser")) {
function testEditUser(WP_error $errors, bool $update, stdClass $user) {
$errors->add("test_error", "A test error occured");
}
}
add_action('user_profile_update_errors', 'testEditUser', 20, 3);
This script seems to work fine as when I update a user an error is shown after the page loads again
But when I look at the fields that I "tried to change" they actually changed. Not only on the frontend, but also in the database. When I reload the page I am shown this:
I am unsure if this is related.
I am using Woocommerce and Jet Engine for some of the custom fields. Could these plugins be the source of the problem? Should I use a different hook? Any form of help would be appreciated!
This question is specifically for Square e-commerce api.
Hopefully this question can reach an Square's dev team.
I am building the embedded payment form for my website and I am not sure how to handle the credit card related errors.
specifically speaking.
when credit card charge end point thrown an exception. It returns something like this
Array ( [0] => stdClass Object ( [category] => PAYMENT_METHOD_ERROR [code] => VERIFY_AVS_FAILURE [detail] => Postal code check failed. ) )
What I want to do is to look at the error and determine if the error is a result of something outside of customers' control.
For example if the error is an API error, then I just want to show an generic message to tell user to try again later. whereas if the error was because user typed their postal code incorrectly. I want to tell user specifically to correct their postal codes.
so I found this link
https://docs.connect.squareup.com/api/connect/v2/?q=error#type-errorcode
it shows me a list of error category. it also shows a list of specific error codes.
But So my question is,
which category or which list of codes are a result of end user error?
right now I have a switch case statement, if any of the following shows up in the [code] field I will show related error to user and anything else will just show generic error.
Is this the right way of doing it?
CARD_EXPIRED
INVALID_EXPIRATION
INVALID_EXPIRATION_YEAR
INVALID_EXPIRATION_DATE
UNSUPPORTED_CARD_BRAND
INVALID_CARD
CARD_DECLINED
VERIFY_CVV_FAILURE
VERIFY_AVS_FAILURE
CARD_DECLINED_CALL_ISSUER
I show the user whatever error string or strings are in the response. There may be more than one error. Here's my PHP code:
try {
$result = $transaction_api->charge($access_token, $location_id, $request_body);
} catch (\SquareConnect\ApiException $e) {
$response = $e->getResponseBody();
$error_string = "";
foreach($response->errors as &$error) {
$error_string .= $error->detail . "<br>";
}
// Return to the credit card form, passing $error_string as a $_POST value
// so it can be displayed. I did that by putting a <form> here containing
// $error_string and anything else that needs to be passed back as
// "hidden" POST data followed by javascript to submit the form.
exit(); // since the credit card was rejected.
}
// if you get here, there were no errors; go ahead and record the approved payment.
Its up to you to decide which errors to show to your users. In general developers probably just pass the error message through, or show a generic error for all errors.
How you have set it up seems fine, though when I think about user error I just think about mistyping something, not having an expired credit card.
I am trying to add google identity toolkit in php. Signin option is working correctly but when i am clicking on problem in sign in link it is showing capthca after submitting captcha it is not navigating to any url.
email.php
<?php
include "identity-toolkit-php-client-master/src/GitkitClient.php";
$gitkitClient=new Gitkit_Client();
$oob_response = $gitkitClient->getOobResults($_POST);
$oob_link = $oob_response['oobLink'];
echo json_encode($oob_response);
?>
email.php is the oobactionurl file. when i am using this code I am getting this error .image
You need to create a php file to retrieve and send the reset link to the user. Make sure the oobActionUrl widget option points to this file. Within the file, you'll get the generated link and additional information by calling $gitkitClient->getOobResults($_POST). It should also work if you exclude $_POST, as the function will check the post contents if no arguments are passed. Then, you can get the link itself like this:
$oob_response = $gitkitClient->getOobResults($_POST);
$oob_link = $oob_response['oobLink'];
From there, you can use your email function of choice to send it to the user. The returned array should contain the following.
'email' => email of the user,
'oldEmail' => old email (for ChangeEmail only),
'newEmail' => new email (for ChangeEmail only),
'oobLink' => url for user click to finish the operation,
'action' => 'RESET_PASSWORD', or 'CHANGE_EMAIL',
'response_body' => http response to be sent back to Gitkit widget
Let me know if you have any further questions.
I'm working on a form with Form tools http://www.formtools.org
My task is to create php multi-page form, using their API. And it should also save data after every step, not only after the last one.
But I already have a problem while initializing the form into the system.
I have all pages built, and when I'm doing test submission it works good. I successfully see my 'Thank you page'.
Following is the code from my 1st page
<?php
require_once("/app-administration/global/api/api.php");
$fields = ft_api_init_form_page("", "test");
$params = array(
"submit_button" => "submit",
"next_page" => "lc_2.php",
"form_data" => $_POST
);
ft_api_process_form($params);
?>
Then, due to the tutorial, I go to the admin panned, create new form there. And then, I need to initialize it.
So, I'm changing this line:
$fields = ft_api_init_form_page(12, "initialize");
But then, when I try to submit the form I get 100 error on the last page:
http://docs.formtools.org/api/index.php?page=error_codes#100
It says there is something with ID, but 12 is correct one because I have it from admin panel, and I double-checked it there.
Anybody have any ideas why I might have this error? Any help will be appreciated.
Thanks
How to log our own error messages(for ex: error due to invalid user date entry) which is generated in php program to drupal error log.
You can use the watchdog function :
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
Quoting the manual, the parameters are :
$type The category to which this message belongs.
$message The message to store in the log.
$variables Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.
$severity The severity of the message, as per RFC 3164
$link A link to associate with the message.
And the error levels can be found on the page of watchdog_severity_levels. For an error, you'll most probably use WATCHDOG_ERROR, or maybe even something more "critical", depending on the kind of error.
Drupal 8+
// Logs a notice
\Drupal::logger('my_module')->notice($message);
// Logs an error
\Drupal::logger('my_module')->error($message);
See more examples at How to Log Messages in Drupal 8.
1) Indeed, watchdog is a standard way to record own PHP errors.
2) Alternatively, if you need to immediately see error messages while debugging your Drupal pages, you may want to see them logged/printed right at the related page - in FireBug console.
Sometimes is this very convenient when you can see page-related just-in-time logs.
This requires - Devel module, Firebug extension to FireFox and possibly Firephp.
You can use the dfb() function to write log messages directly to the general Firebug console.
dfb($input, $label = NULL)
If you want to keep your Drupal-related log messages out of the normal Firebug console, you can write messages to the Drupal for Firebug log with the firep() function:
firep($item, $optional_title)
Watchdog is the way to go for a production system no doubt but during debugging I find the drupal_set_message function useful.
It outputs the message to the screen where the 'Operation Successful'-type messages are normally displayed (so make sure you remove them before making the site Live).
http://api.drupal.org/api/function/drupal_set_message/6
In drupal 7 we can log message by following method:
drupal watchdog function we can use to log message in database , make sure we have enabled optional core module for Database Logging at /admin/build/modules.
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
$type:
The category to which this message belongs , Example: PHP,cron.., we can filter message by type.
$message :
The message to store in the log,Example: 'The following module is missing from the file system: security_review'
$variables :
Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.
to make message translated , do not pass dynamic value pass variables in the message should be added by using placeholder strings.
Example:
watchdog('cg_volunteer', 'cg in form_alter %formly', array('%formly' => $form['#id']), WATCHDOG_NOTICE, $link = NULL);
$severity
The severity of the message,logs can be filter by severity as per RFC 3164. Possible values are WATCHDOG_ERROR, WATCHDOG_WARNING, etc.
For more example see https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/watchdog/7.x
$link:
A link to associate with the message.
Example
// for logs notices
watchdog('my_module', $message, array());
// for Loging Error
watchdog('my_module', $message, array(), WATCHDOG_ERROR);
In drupal 8 we used following method:
// For Logs a notice.
\Drupal::logger('my_module')->notice($message);
// For Logs an error.
\Drupal::logger('my_module')->error($message);
// For Alert, action must be taken immediately.
\Drupal::logger('my_module')->alert($message);
// For Critical message.
\Drupal::logger('my_module')->critical($message);
// For Debug-level messages.
\Drupal::logger('my_module')->debug($message);
//For Emergency, system is unusable.
\Drupal::logger('my_module')->emergency($message);
//For Warning
\Drupal::logger('my_module')->warning($message);
//For Informational messages.
\Drupal::logger('my_module')->info($message);
Also for translate we should not use t() function.
\Drupal::logger('my_module')->alert('Message from #module: #message.', [
'#module' => $module,
'#message' => $message,
]);
this will be translated on run time.
Example :
\Drupal::logger('content_entity_example')->notice('#type: deleted %title.',
array(
'#type' => $this->entity->bundle(),
'%title' => $this->entity->label(),
));
Both watchdog for D7 & \Drupal::logger for D8 will write log in watchdog table (in your database), and with HUGE data logged, you can imagine performance impact.
You can use error_log php function to do it (see PHP manual).
error_log("Your message", 3, "/path/to/your/log/file.log");
You need to have permission to write in your log file (/path/to/your/log/file.log)
drupal-8drupalphplog
// Get logger factory.
$logger = \Drupal::service('logger.factory');
// Log a message with dynamic variables.
$nodeType = 'Article';
$userName = 'Admin';
$logger->get($moduleName)->notice('A new "#nodeType" created by %userName.', [
'#nodeType' => $nodeType,
'%userName' => $userName,
]);
Source