Custom Validation of Array Value in Quform Wordpress Plugin - php

I bought Quform wordpress plugin from codecanyon. The plugins is great - it allows you create custom validator for your forms as shown here http://support.themecatcher.net/quform-wordpress/guides/customization/creating-a-custom-validator.
The example above however only deals with one validating against a single value. What if I have 500 of them?
This is code that I have.
function custom_validator($valid, $value, $element, $rcnumber)
{
$rcnumber = array('103184','104351','104359','103912','104389','104400','100505','102180','103530','104455','79162','74233','26451','75140','289752','101785','103141','26646','103178','100567','75159','103744','103244','78557','103330','102602'<500 numbers....>,);
if ($value != $rcnumber) {
$element->addError('Invalid RC Number');
$valid = false;
}
return $valid;
}
add_filter('iphorm_element_valid_iphorm_14_11', 'custom_validator', 10, 3);
The problem with this code is that no matter what value I enter in rcnumber field I keep getting 'Invalid RC Number' error even though the number IS among 500 listed in the rcnubmer array. What am I doing wrong?
Thank you!

Not sure of what you want to get here, but it looks like you want to check if a value exists in an array.
Try this:
if (!in_array($value, $rcnumber))
Instead of:
if ($value != $rcnumber)
in_array function will look for a value in an array, your current code checks if $value is different that the given array.

Related

How to register multiple taxonomies for Woocommerce custom attributes?

I've been using the Woocommerce documentation to get custom attributes in my wordpress navigation menu:
https://woocommerce.com/document/using-custom-attributes-in-menus/
I used the following code:
add_filter('woocommerce_attribute_show_in_nav_menus', 'wc_reg_for_menus', 1, 2);
function wc_reg_for_menus( $register, $name = '' ) {
if ( $name == 'pa_druivensoort') $register = true;
return $register;
}
This works, but how should I modify the code to register multiple of these custom taxonomies?
Let's see if I can help you out.
What I think you can do is use the PHP OR operator that is basically two pipes "||". The OR operator works by combining the conditions and then PHP executes the if-block if at least one of the conditions are true. If both of the conditions are false, then PHP does not execute the if-block statements.
Ofcourse if you have alot of attributes there might be a more effective way to do this but I hope this helps!
Also remeber to make sure that the attributes you want in the navigation menu are archived. You do this by going to Products -> Attributes then hover your mouse over the attribute and click on Edit. On the edit screen make sure the "Enable archive" check box is checked.
Here is an example of how the function should look like:
add_filter('woocommerce_attribute_show_in_nav_menus', 'wc_reg_for_menus', 1, 2);
function wc_reg_for_menus( $register, $name = '' ) {
if ( $name == 'pa_druivensoort' || 'pa_secondattribute' || 'pa_thirdattribute') $register = true;
return $register;
}

Gravity Forms custom validation filter

I have a function that processes sales via a third-party service, processes the result and returns an array with the Status "Success" or "Invalid." This sales call is made using the gform_after_submission hook applied to the specific form.
What I need to do is store the "Success" or "Invalid" result in the array as a variable that I can later pass to a function to validate or invalidate the credit card field, using gform_validation hook.
I'm declaring the variable in a function, like so:
function foo {
...code to sell product through API...
$status = $checkoutShoppingCartRequest['Result']['Status'];
}
When I print the variable $status within the function, it is showing either Success or Invalid like it should.
Here is other function where I need to use this variable, passed to gform_validation, which fails every time regardless of Success or Invalid result:
function MBvalidate( $validation_result ) {
$form = $validation_result['form'];
if ( $status !== "Success") {
$validation_result['is_valid'] = false;
foreach( $form['fields'] as &$field ) {
if ( $field->id == '34' ) {
$field->failed_validation = true;
$field->validation_message = 'Your credit card could not be processed.';
break;
}
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
add_filter( 'gform_validation_47', 'MBvalidate' );
I have tried passing the variable a number of different ways, via globals and sessions, etc.
I am new to GF development so I am sure I'm missing something. I'd appreciate any direction.
The gform_after_submission action hook runs after gform_validation.
Anyway, assuming you can find a hook that runs earlier, what I would do is store a unique variable for each submitted form using the Transients API's set_transient() and get_transient() functions. For example you can create a hidden field in every form which you populate with a random ID. Use this random ID as a key to store and retrieve the Success/Invalid result.
$status here is a local variable which has never been defined before you try to use it in if-condition. So, it's always null.
Maybe you missed
$status = $validation_result['Result']['Status'];
or something like this before checking the condition.

Wordpress - How can I hide the_meta output if it is empty?

I've created a function and hook to insert some custom field information on some of my posts.
<?php the_meta(); ?>
I have added some CSS formatting (box with background) for display. Problem: if I have a post without any custom fields defined it displays an empty box. How can I prevent it from outputting the empty if there is nothing to display? All I can find is information on specific field types and and can't extrapolate from it. I'm definitely not a php boss.
You can use get_post_custom() to get the custom fields as an array, and then do your output only if there are any custom fields in the array.
This should do the job, though it's not very elegant:
$has_custom = false;
foreach(get_post_custom_keys() as $k => $v) {
$t = trim($v);
if('_' != $t{0}) {
$has_custom = true;
break;
}
}
if($has_custom) {
the_meta();
}

PHP else statement not working as it should.. is_numeric and isset

In the following If.. else statement, I am trying to see if first, there IS a 4th breadcrumb (URI array 4), and then to see if it is a number..
but both always return true, number, letters, or empty..
can anyone tell me why?
// checks the third breadcrumb contained within the breadcrumbs class to see
// if it is an article. Then uses a recursive function to search the first
// and second breadcrumbs to see if they exist within their respective menu
// arrays. finally, if that validates, then the fourth breadcrumb is checked
// to see if it exists (not empty) AND if it is a number.
else
if (($this->breadcrumbs->getCrumb(3)=='article' &&
$array_helper->recValueReturn($this->breadcrumbs->getCrumb(2),
$this->getNavTwo()) &&
$array_helper->recValueReturn($this->breadcrumbs->getCrumb(1),
$this->getNavOne())) || ($this->breadcrumbs->getCrumb(4)) &&
is_numeric($this->breadcrumbs->getCrumb(4))) {
...
the following always validates to False:
else
if (($this->breadcrumbs->getCrumb(3)=='article'&&
$array_helper->recValueReturn($this->breadcrumbs->getCrumb(2),
$this->getNavTwo()) &&
$array_helper->recValueReturn($this->breadcrumbs->getCrumb(1),
$this->getNavOne())) &&
(array_key_exists($this->breadcrumbs->getCrumb(4),
$this->breadcrumbs->getCrumbs())&&
is_numeric($this->breadcrumbs->getCrumb(4)))) {
...
For future reference, and for anyone else reading this..
The problem was solved in the BreadCrumbs class itself.
Previous, breadcrumbs had a private array with the exploded URI values. And I retreived a single url index using:
public function getCrumb($x) { return $this->breadcrumbs[$x] }
But if i modify this getter directly to include an isset:
public function getCrumb($x) {
if (isset($this->breadcrumbs[$x])) {
return $this->breadcrumbs[$x];
} else {
return null;
}
}
and then use the first else..if in the OP.. problem solved. It works.

Drupal - Using a CCK value as an if statement in views argument validation code

I am wanting to display a view differently depending on CCK field values that exist on the current node being viewed. I have found many solutions relating to user id and node reference fields, but they don't seem to work in this use case..
I have a view that contains 3 attachment displays, the first returns a single randomly sorted result; the second returns a slideshow of results; and the third, a list of all results.
To select which of these attachment displays to show, the content has two fields - field_content_ad_slideshow and field_content_ad_showall. Both of these are set as yes|Yes and no|No. In other words, it is a checkbox, where selected is 'yes' and unselected is 'no'.
I want attachment 1 to display if both values = no.
attachment 2 to display if field_content_ad_slideshow = yes
and attachment 3 to display if field_content_ad_slideshow = no and field_content_ad_showall = yes
(therefore if both values are yes, attachment 2 will display)
Due to reasons I wont go into, I cannot use the views filters to limit each display and so am trying to do it in the argument. I have tried setting the Default argument type to 'Node ID from URL' and the validate code to -
if (arg(0) == 'node' && is_numeric(arg(1))) {$node = node_load(arg(1));}
if ($node->field_content_ad_slideshow[0]['value'] == 'no' && $node->field_content_ad_showall[0]['value'] == 'no') {return TRUE;}
else {return FALSE;}
This doesn't work.
I have also tried changing the validator code to basic validation and the Default argument type to
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node=node_load(arg(1));
$slideshow = $node->field_content_ad_slideshow[0]['value'];
$showall = $node->field_content_ad_showall[0]['value'];
$display = null;
if($slideshow=='no' && $showall=='no') {
$display == 'true';
}
if($display) {
$args[0] = (arg(1));
} else {
$args[0] == '';
}
return $args;
Neither of these work. Any help much appreciated!!
Thanks
Rob
You have it as $display == 'true' So the $display var always validates as false.
Also I thought args were returned as strings and multiple args were managed using the '+' signs in views options. But this was in drupal 6.
Good Luck!

Categories