How to populate a Gravity Form checkbox with a query string - gravity-forms-plugin

I can populate a gravity form text field that has a parameter name of 'textfield' with a query string like
https://www.example.com/formpage/?textfield=value
But I would also like to be able to populate a checkbox, i.e check a box. However, checkboxes are not mentioned in the Gravity Forms documentation "Dynamically Populating a Field".
My checkbox is called 'flags' and has choices with names of 'box1' and 'box2', so I tried
https://www.example.com/formpage/?textfield=value&flags.box1=true
https://www.example.com/formpage/?textfield=value&flags.box1=1
https://www.example.com/formpage/?textfield=value&flags[box1]=true
https://www.example.com/formpage/?textfield=value&flags[box1]=1
https://www.example.com/formpage/?textfield=value&flags=box1
But I cannot get a box to check. Does anyone know if there is a way to do this?

Went and looked at the code.
To set a single checkbox (or value in a list) use
https://www.example.com/formpage/?textfield=value&flags=box1
To set multiple values the code is set up to use syntax like:
https://www.example.com/formpage/?textfield=value&flags=box1|box2
https://www.example.com/formpage/?textfield=value&flags=box1%C7box2
However, in the gravity form source there is a test for a list, that also needs to test for a checkbox. In forms_model.php, function get_parameter_value I changed the statement
if ( ! empty( $value ) && RGFormsModel::get_input_type( $field ) == 'list' )
to
if ( ! empty( $value ) && (RGFormsModel::get_input_type( $field ) == 'list' ||
RGFormsModel::get_input_type( $field ) == 'checkbox' ))
Then it all works!

Your syntax was just a bit off. It's not that well documented. You need to use commas.
https://www.example.com/formpage/?textfield=value&flags=box1,box2

Related

Magento 2 - Hide/Show custom EAV attribute after if condition

I created a new EAV attribute using this code :
'information',
[
'type' => 'int',
'default' => null,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => false
]
this attribute is by default hidden, but I want to visible it after a condition, is there any method to do that?
The visibility of the attribute is set per global or store view, so this should not be possible. However, when you display your attribute, for example on your product view page, you can build in some logic to decide wether it should be displayed or not.
Solution 1
Here's an example I used in a similar way in phtml on product view:
<?php
$product = $block->getProduct();
$attribute = $product->getResource()->getAttribute('attribute_code')->getFrontend()->getLabel($product);
if('>>your_condition_here<<'){
echo $_attribute;
}
?>
You could use this anywhere you want, especially outside of the details tab.
Solution 2
Another way would be to extend the default detail tab and build in your condition. Copy vendor/magento/module-catalog/Block/Product/View/Attributes.php to your module or theme and extend the first if condition (line 83)* in getAddtionalData() to your needs.
Change
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr))
To
if (($attribute->getIsVisibleOnFront() || ($attribute->getAttributeCode() == 'information' && '>>>your condition here <<<')) && !in_array($attribute->getAttributeCode(), $excludeAttr))
*I should add that this works for M2 2.2.x. I don't know about 2.3

Perl Mechanize Click A Radio Button

I am trying to click a radio button by using Perl with Mechanize module . I tried
$mech->find_all_inputs ( name => "name" , value => "1" )
, but could not get any result. The html code of the radio button is like that;
<input name="name" value="1" type="radio">
And there are other radio buttons with the same name but different values.So how can I click the radio button using Mechanize module?
Thank you in advance.
The user agent cannot magically distinguish the right one. Find them all, and then go through them and pick the one(s) you need. You can learn values by the method possible_values
# Finding the field name and values for the radio element
foreach ( $ua->find_all_inputs(type => 'radio') ) {
$radio_name = $_->name;
say "$radio_name values: " . join('|', $_->possible_values)
}
If it's just one
my ($radio_name, $input_err) =
map $_->name, $ua->find_all_inputs(type => 'radio');
warn "More radio inputs than expected: $!" if defined($input_err);
Once you have the right button you can fill the form in which it is. For example
$ua->submit_form( fields => { $radio_name => 'AND' });
The submit_form "lets you select a form from the previously fetched page, fill in its fields, and submit it." [from docs, WWW::Mechanize]. It takes parameters as hashes, where you can set names and values, for example. This method is a higher-level wrapper, instead of which you can use more specific ones, for example
$ua->field( $name, $value );
To set a field among duplicate ones, pick the number of the one you want. For example, to set the second one from the list
$ua->set_fields( $name => [ 'field_value', 2 ] )
Also note that WWW::Mechanize inherits heavily. When you search for the right call it may be a good idea to look through methods in the packages that it inherits from.

Symfony 2 unchecked checkbox missing in POST data

Unchecked checkbox is not present in GET or POST data.
How to handle it?
Is there need to do it manually or there is another reason why symfony $form->getData() does not handle it automatically?
Symfony by default parse your checkboxes to an array so if you have a checked one you will have it in your form->getData() else you will not have it, so in your controller if you don't get your checkbox in form data that's mean that the checkbox is unchecked
As explained above
I had the same issue, to be honest I dont understand why there is no option value for unchecked checkboxtype like 'unchecked_value' => false
I had to manually go over submitted fields, check if field has not been submitted then I know it is missing in form submit and that means it equal to false.
this is in my class to be run before persisting...it will basically loop over properties and set its values to false
public function setUncheckedReplacementFields(array $data)
{
foreach($this as $property => $value){
if(str_contains('Replacement', $property) !== false){
if(!in_array($property, $data)){
$method = sprintf('set%s', ucfirst($property));
if(method_exists(this, $method)){
$this->$method(false);
}
}
}
}
}
before I am persisting form I run this
$object->setUncheckedReplacementFields($request->request->get($form->getName()));
so if field is not part of form I know it has been unchecked and I loop over object to find those checkboxes and setting them to false in my case unchecked.

laravel 4 form helpers - including additional attributes

Within Laravel 4 I can create a select box in a form with this helper:
Form::select('name', $data)
where $data represents an array of the select options.
However I want to now add an extra attribute of 'Required' to help with form validation on the client side.
I can do this if I use normal php and markup but I'd like to use the laravel helper if I can.
I've tried adding:
Form::select('name', $data, array('required'=>'required'))
This doesn't appear to add anything to the final mark up. I've tried the following but this doesn't work either:
Form::select('name', $data, array('required'))
Is there an easy way of doing this or accept that I should revert to another method without a helper
If you check the FormBuilder class the select field is declared as
public function select($name, $list = array(), $selected = null, $options = array())
So what you are passing is the third parameter which should be the default selected option from the list.
In order to achieve what you need you have to pass the array of $options as the fourth parameter (if you don't have default selection just leave the third parameter null)
Form::select('name', $data, null, array('required'=>'required'))

Display Zend_Form form errors in ViewScript

I'm trying to display all form errors before the form using a ViewScript. Here is the code that I'm currently trying to use within my ViewScript:
<div class="errors">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</div>
This call gives me an error message:
Warning: htmlspecialchars() expects parameter 1 to be string, array given
I've seen this same code suggested other places but its not working for me. If I print out $this->element->getMessages() I do see the error messages as the following:
Array ( [myField] => Array ( [isEmpty] => Value is required and can't be empty ) )
Any ideas?
The getMessages() returns an array of form element names as keys which each contain an array of errors for that element. So basically instead of handing the formErrors view helper:
Array ( [isEmpty] => Value is required and can't be empty )
You are handing it:
Array ( [myField] => Array ( [isEmpty] => Value is required and can't be empty ) )
You would want to do something like this instead:
$arrMessages = $this->myForm->getMessages();
foreach($arrMessages as $field => $arrErrors) {
echo sprintf(
'<ul><li>%s</li>%s</ul>',
$this->myForm->getElement($field)->getLabel(),
$this->formErrors($arrErrors)
);
}
As Mark points out in his answer, the getMessages() returns an array of form element names as keys which each contain an array of errors for that element; and his solution is:
$arrMessages = $this->myForm->getMessages();
foreach($arrMessages as $field => $arrErrors) {
echo sprintf(
'<ul><li>%s</li>%s</ul>',
$this->myForm->getElement($field)->getLabel(),
$this->formErrors($arrErrors)
);
}
This works, as long as getMessages() results in a two-dimensional array. However, if the form is based on relational data sets generated by Doctrine (or some other plugin), the error message associated with a field might also be an array and the above code will crash because it treats $arrErrors as a string when it turns out to be an array.
To capture the error messages if there's a second data set we could introduce a foreach statement nested inside the first foreach statement, but that won't work when getMessages() results in a two-dimensional array; nor does it work if the data sets are more than two-deep.
In a relational data scenario where we don't know how deep the error message comes from, a scalable solution is
$arrMessages = $this->myForm->getMessages();
print_r ($arrMessages);