how to add a sign like dollar to the end of input value of Gravity Forms entry after submition - gravity-forms-plugin

I need an input type for percentage input which will add the % sign at the end of the user input and also an input type for currency without the text of currency(CAD, USD, ...). I just need a $ sign
and I also use a PDF maker for gravity forms that I need the Correct Values to be printed in both pdf and email with sign
I tried gform_after_create_post_39 and gform_pre_submission_39 which 39 is the id of the form
This is the code :
add_action( 'gform_after_submission_39', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
if(isset($_POST['input_16']) && !empty($_POST['input_16'])) {
$_POST['input_16'] = $_POST['input_16'] . ' %';
}
if(isset($_POST['input_17']) && !empty($_POST['input_17'])) {
$_POST['input_17'] = $_POST['input_17'] . ' $';
}
}
I need to add % sign at the end of input_16 value in email and PDF
and also I need to add $ sign to the end of input_17's value in the email and PDF
i tried to echo $_POST['input_16'] or $_POST['input_17'] the output is correct but it is not effected in email and PDF

The problem is solved. here is the Answer :
I added a simple Text input to the form and then by using this code
I can change the specific input value or update it by adding the sign at the end of an entry before notification send
'gform_pre_submission_39' -> 39 is the ID of the form that I want to change its input value
'input_130' -> is the simple text input with id 130 that I need to add % to the end
This hook will perform before Notification and after Form Validation!
So the changes will apply on PDF and email!
in the beginning, the problem was from the input which was number format so when I added % or $ sign to the end of the entry after the form submission it didn't accept because it was number format.
sorry for my English, I hope I could help someone who has such a problem in the future
add_action( 'gform_pre_submission_39', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
// INPUT 130 - Basic Coverage
if(isset($_POST['input_130']) && !empty($_POST['input_130'])) {
if(strpos($_POST['input_130'], '%') == false) {
$_POST['input_130'] = $_POST['input_130'] . '%';
} else {
//Do Nothing
}
}
if(isset($_POST['input_131']) && !empty($_POST['input_131'])) {
if (strpos($_POST['input_130'], '$') == false) {
$_POST['input_131'] = $_POST['input_131'] . '$';
} else {
//Do Nothing
}
}
}

Related

How do you access Gravity Form fields pre-submission?

I'm trying to access the Gravity Forms fields pre-submission to check for gibberish entries. I've tried pretty much everything on StackOverflow and no luck.
The form is id #1. Some of the 1000 things I've tried include:
GFFormsModel::get_form_meta(1);
GFFormsModel::get_leads(1);
$_POST['input_1']
and reading this:
https://docs.gravityforms.com/gform_pre_submission/
How do I do this?
If you want to fail silently for spam entries, I would recommend using the gform_validation filter and, if you determine that the submission is spam, dynamically enable the honey pot. Gravity Forms will then fail the submission silently. The confirmation will be displayed but the entry will not be created.
Here's how my GP Blacklist plugin handles this:
$honeypot_field_id = GFFormDisplay::get_max_field_id( $form ) + 1;
$_POST[ "input_{$honeypot_field_id}" ] = true;
$form['enableHoneypot'] = true;
$result['is_valid'] = true;
EDIT
To actually retrieve the value, the simplest way is to just fetch it from the $_POST (FYI, there are more thorough techniques). Here's a more robust example.
add_filter( 'gform_validation', function( $result ) {
// Get the value of field ID 1
$value = rgpost( 'input_1' );
if( $value == 'gibberish' ) {
// activate honeypot
}
return $result;
} );

Find out whether function returns true or false

I am struggling to make form validation work and it looks like the main problem is placeOrder function (other function which sit inside this functions work just fine). The function behaves in a strange way (gives alert only if the first input field (message) does not confirm to the test conditions, while if I leave the others blank it does not show alert). I was trying to find the reason for that by putting each part of the main if statement (which are divided by AND operators) in this test:
if(validateLenghtData(5, 32, form["message"], form["message_help"])) {
console.log("validation passed");
} else {
console.log("validation failed");
}
This gave me nothing because it only returns false ("validation failed") to console, while it outputs nothing if I enter valid text to the input. Where can be the bug and how can I test each part of big if statement ( like validatedZipCode(form["zipcode"], form["zipcode_help"])) in order to find out which part gives me false.
function placeOrder(form) {
// first check whether all fields are filled with valid data
// form["message"] and other simular arguments are part of form object and are passed from submit form (look it)
if (validateLenghtData(5, 32, form["message"], form["message_help"]) &&
validatedZipCode(form["zipcode"], form["zipcode_help"]) &&
validateNonEmptyField(form["date"], form["date_help"]) &&
validateNonEmptyField(form["name"], form["name_help"]) &&
validateNonEmptyField(form["phone"], form["phone_help"]) &&
validateNonEmptyField(form["email"], form["email_help"])) {
// everything is ok, submit the order to the server
form.submit();
} else {
alert ("You need to feel all fields correctly");
}
if(validateLenghtData(5, 32, form["message"], form["message_help"])) {
console.log("validation passed");
} else {
console.log("validation failed");
}
}

GravityForms - calculate two fields into a percentage

Hello stack exchange community,
Is it possible to use the internal gravity forms calculate fields function to calculate it into a percentage? I can't find where in the code this calculation happens so i can rewrite how the field outputs. Can anyone point me in the correct direction (as in what hook can i use to rewrite how a form field outputs)?
You'll want to use the "gform_calculation_format_result" JS filter. Copy, paste, and configure the following code in an HTML field on your form.
<script type="text/javascript">
gform.addFilter( 'gform_calculation_format_result', function(formattedResult, result, formulaField, formId, calcObj ) {
// UPDATE: "3" to ID of your Calculation field
if ( formulaField.field_id == 3 ) {
formattedResult = gformFormatNumber( result, ! gformIsNumber( formulaField.rounding ) ? -1 : formulaField.rounding, '.', ',' ) + '%';
}
return formattedResult;
});
Here's an export of a working form if you'd like to import and see this in action (form export).

form validation with preg_match

I have created a form which gets user data about the city he leaves. The form stores data in a table called profile. I am using a function which inserts data into profile table. If there are already data, the function just updates the table. If the user does not insert data into city field and press submit, the table continues to keep the previous data, because nothing submitted.
In the form I would like to make a validation so that the user will inserts only a-z or space characters.
I have used the following code for validation:
if( empty($_POST) === false ){
if( empty($errors) === true ){
if( preg_match('/^[a-zA-Z ]+$/', $_POST['city']) ===0 ){
$errors[]='<font color="#963">City must only contains a-z and spaces</font>';
}
}
}
It works! But the problem now is that if the user leave the form empty and press submit the above code enforces him to place data( says: City must only contains a-z and spaces). How can I use the above code to validate the form only if the user enters data. And if not then to continue without any problem.
You can do this without even generating an error. Replace
if( preg_match('/^[a-zA-Z ]+$/', $_POST['city']) ===0 ){
With
$_POST['city'] = preg_replace("&[^a-zA-Z ]&","",$_POST['city']);
This will replace all non-lowercase letters and non-spaces with nothing before it's inserted into the database.
EDIT:
If you'd still like to generate an error, use
if( preg_match("&[^a-zA-Z ]&",$_POST['city']) ){
$errors[]='<font color="#963">City must only contains a-z and spaces</font>';
}
before the preg_replace.

How to include field values in the vtypes message text for vtype validation in forms?

I'm working on form validation and I have used Vtypes to check number ranges.
It's all working fine, except I need to include my own allowed values ( field.minValField and field.maxValField) in the 'numberrangeText'. Is there any way to do that ?
Thanks in advance
Ext.apply(Ext.form.VTypes, {
numberrange : function(val, field) {
if(val < field.minValField || val > field.maxValField){
console.log(field);
return false;
}
},
numberrangeText: 'Value Range Should Be: '
});
Arfeen
There is no way to use templates or XTemplates in numberrangeText. Because they(extjs) just take this string without changes as I've found out from file src/widgets/form/TextField.js from the line errors.push(this.vtypeText || vt[this.vtype +'Text']);.
But as you can see you can use field.vtypeText instead.
For example you can write something like this:
field.vtypeText = 'Value Range Should Be: ' + field.minValField + '-' + field.maxValField;
in your numberrange function.
You can see what I'm talking about in this example