So, I am working with the Repeater beta add-on for Gravity Forms (https://docs.gravityforms.com/repeater-fields/), and I need to make it required--or more specifically, the first entry within it's field. I have tried various methods to no avail and am not sure why.
I tried setting "required" to true for the fields inside the repeater
I tried setting the "required" to true for the repeater field itself
I tried having the repeater field AND the fields within it required
In the admin, every time I click the "Required" checkbox for the repeater field, the checkbox becomes unchecked after saving the form
Using $(window).load I tried adding a required=required attribute to all fields in the repeater but that prevents the only fields in the form from displaying any "required" messages to the user, and only the first empty field in the repeater is actually checked. The rest, even if empty, are not, and do no display a "required" message.
Using $(document).ready() but the results were similar to above but the rest of the form fields started validating again
Browsing the Gravity Forms forum but could not find the answer I was looking for, and I don't know how long it will take for anyone to respond to my question and this can't wait days or weeks.
This is all the code I setup for the repeater in functions.php per the field's documentation example:
// Adjust your form ID
add_filter( 'gform_form_post_get_meta_5', 'add_my_field' );
function add_my_field( $form ) {
// Create a Single Line text field for the product member's name
$purchasedFrom = GF_Fields::create( array(
'type' => 'text',
'id' => 1002, // The Field ID must be unique on the form
'formId' => $form['id'],
'required' => true,
'label' => 'Purchased From',
'pageNumber' => 1, // Ensure this is correct
) );
$itemtype = GF_Fields::create( array(
'type' => 'text',
'id' => 1007, // The Field ID must be unique on the form
'formId' => $form['id'],
'required' => true,
'label' => 'Item Type',
'pageNumber' => 1, // Ensure this is correct
) );
// Create an email field for the product s
$quantity = GF_Fields::create( array(
'type' => 'text',
'id' => 1001, // The Field ID must be unique on the form
'formId' => $form['id'],
'required' => true,
'label' => 'Quantity',
'pageNumber' => 1, // Ensure this is correct
) );
$purchasedDate = GF_Fields::create( array(
'type' => 'text',
'id' => 1003, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => 'Date of Purchase',
'required' => true,
'pageNumber' => 1, // Ensure this is correct
) );
$serviceDate = GF_Fields::create( array(
'type' => 'text',
'id' => 1004, // The Field ID must be unique on the form
'required' => true,
'formId' => $form['id'],
'label' => 'Date Out of Service',
'pageNumber' => 1, // Ensure this is correct
) );
$size = GF_Fields::create( array(
'type' => 'text',
'required' => true,
'id' => 1009, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' =>'Size',
'pageNumber' => 1, // Ensure this is correct
) );
$upc = GF_Fields::create( array(
'type' => 'text',
'required' => true,
'id' => 1010, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => 'UPC/Part # (From Receipt)',
'pageNumber' => 1, // Ensure this is correct
) );
$damage = GF_Fields::create( array(
'type' => 'text',
'required' => true,
'id' => 1005, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => 'Description of Damage',
'pageNumber' => 1, // Ensure this is correct
) );
// Create a repeater for the product and add the name and email fields as the fields to display inside the repeater.
$product = GF_Fields::create( array(
'type' => 'repeater',
'required' => true,
'id' => 1000, // The Field ID must be unique on the form
'formId' => $form['id'],
'label' => 'Add Products',
'addButtonText' => 'Add Another Product',
'removeButtonText'=> 'Remove Product',
'pageNumber' => 1, // Ensure this is correct
'fields' => array( $purchasedFrom,$itemtype, $quantity, $purchasedDate,$serviceDate, $size, $upc, $damage), // Add the fields here.
) );
$form['fields'][] = $product;
return $form;
}
// Remove the field before the form is saved. Adjust your form ID
add_filter( 'gform_form_update_meta_5', 'remove_my_field', 10, 3 );
function remove_my_field( $form_meta, $form_id, $meta_name ) {
if ( $meta_name == 'display_meta' ) {
// Remove the Repeater field: ID 1000
$form_meta['fields'] = wp_list_filter( $form_meta['fields'], array( 'id' => 1000 ), 'NOT' );
}
return $form_meta;
}
I do need this field to be required somehow, specifically the first entry so that the user knows to enter at least one product. What am I missing?
Hi you simply need to flag them as:
'isRequired' => true,
rather than
'required' => true,
After the change, be sure to remove the repeater from the form and update. The repeater should appear again, click update and check the results.
Related
I am trying to add some extra fields in theme my login Plugin Wordpress.
I am done with the documentation of the theme.
The extra fields aren't been saved on database. I have added the extra fields in the db as well but still the data are not been saved in db
Any Suggestion?
You need to use custom type not text and first_name
function add_tml_registration_form_fields() {
tml_add_form_field( 'register', 'custom_1', array(
'type' => 'custom',
'label' => 'First Name 1 ',
'value' => tml_get_request_value( 'first_name_test', 'post' ),
'id' => 'first_name_test',
'priority' => 15,
) );
tml_add_form_field( 'register', 'custom_2', array(
'type' => 'custom',
'label' => 'Last Name 1',
'value' => tml_get_request_value( 'last_name_test', 'post' ),
'id' => 'last_name_test',
'priority' => 15,
) );
}
add_action( 'init', 'add_tml_registration_form_fields' );
Read this
https://docs.thememylogin.com/article/104-function-tmladdformfield
My code is as below in redux
$fields = array(
'id' => 'opt-select',
'type' => 'select',
'title' => __('Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
// Must provide key => value pairs for select options
'options' => array(
'1' => 'Opt 1',
'2' => 'Opt 2',
'3' => 'Opt 3'
),
'default' => '2',
);
Now, I would like to 3rd option will be shown if header variation 2 is set. Header variation is another field which have two option such as Header variation 1 and Header variation 2.
How to write code to achieve this functionality?
You can use Redux Framework's required argument for any given field.
From the Docs:
Fields may be linked/required/folded according to a/multiple parent
value(s). This is accomplished by appending a required argument,
similar to the following, on any given field:
'required' => array('opt-header-variations','equals','2')
The first value of the array is the field ID in which to link the field to.
The second value is the operation to perform.
The third value is the value to compare against.
The required argument supports a few operators so you can do some logic.
A real example:
Bellow we're setting two fields, both are select field types, but it would work the same for any field type. We want to hide the select field with "opt-select" id and only show it if the select field "opt-header-variations" value equals 2, in this case the same as the "Header variation 2" option.
In order to do that, we need to use the required argument on the select field we want to conditionally hide as such:
'required' => array('opt-header-variations','equals','2'),
$fields = array(
array (
'id' => 'opt-header-variations',
'type' => 'select',
'title' => __('Header Styles', 'redux-framework-demo'),
'subtitle' => __('Header Variations', 'redux-framework-demo'),
'options' => array(
'1' => 'Header variation 1',
'2' => 'Header variation 2'
),
'default' => 1,
),
array (
'id' => 'opt-select',
'type' => 'select',
'title' => __('Select Option', 'redux-framework-demo'),
'subtitle' => __('No validation can be done on this field type', 'redux-framework-demo'),
'desc' => __('This is the description field, again good for additional info.', 'redux-framework-demo'),
// Must provide key => value pairs for select options
'options' => array(
'1' => 'Opt 1',
'2' => 'Opt 2',
'3' => 'Opt 3'
),
'default' => '2',
'required' => array('opt-header-variations','equals','2')
),
);
The required argument can also be used with multiple “parent” required values. If all of these conditions are not met, this field will not be visible and the output CSS will not be used. An example is as
follows:
'required' => array(
array('layout','equals','1'),
array('parent','!=','Testing')
)
I'm adding extra fields to the checkout page in WooCommerce,
I've added basic text fields ok, but I want a dropdown or select box with a few options,
Here is what I've done so far but I've made an error somewhere
$fields['billing']['billing_meat'] = array(
'label' => __('Food options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false
'type' => 'select',
'options' => array( // array of key => value pairs for select options
__('I eat meat', 'woocommerce') => __('I eat mate', 'woocommerce'),
__('meat is gross', 'woocommerce') => __('meat is gross', 'woocommerce'),
Maybe I'm not defining 'type' field correctly?
thanks loads
If that is your exact code, then the problem is that you are missing a comma after 'clear' => false.
I've tested this and it works:
$fields['billing']['billing_meat'] = array(
'label' => __('Food options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => array(
'eat-meat' => __('I eat maet', 'woocommerce' ),
'not-meat' => __('Meat is gross', 'woocommerce' )
)
);
Note that I also did not use __() on the options array keys. It's better to not translate those.
this is working. here's my code
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'dropdown' );
// Our hooked in function - $fields is passed via the filter!
function dropdown( $fields ) {
$fields['billing']['dropdown'] = array(
'label' => __('dropdown', 'woocommerce'),
'placeholder' => _x('dropdown', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'type' => 'select',
'options' => array(
'option 1' => __('option 1', 'woocommerce' ),
'option 2' => __('option 2', 'woocommerce' )
)//end of options
);
return $fields;
}
this works. but it seems that the value is not being saved under _billing_dropdown. I used "admin columns" plugin to add a field in the order table (admin side). under my custom field "dropdown", no value shows up. where can i retrieve the value? same goes with my other custom field "purpose" that is under "_billing_purpose" and is type=>'textarea'. thanks!
here's a screenshot
https://scontent-b-hkg.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/10696182_985415141473105_5302358697439449940_n.jpg?oh=74cb5ebc6b1ad6dd0c29e51293b61fdf&oe=5502613C
$instance = array(
'field_name' => $field_name,
'entity_type' => $entity,
'bundle' => $bundle,
'field types' => 'list_boolean',
'widget' => array(
'type' => 'options_onoff',
'settings' => array('display_label' => 1)
),
'default_value' => array(array('value' => 1)),
);
this is not taken, and i have to save it twice in the admin contenttype - field/edit,
until it takes it ...
i now exported the finished field with the features module,
and took the generated code - suddenly it works, with default_value
i guess i was missing the property module on the field, also field types is inexistant ..
In your field definition, you have to set the allowed_values in the settings array in order for the default_value in the instance to get picked up.
so like this assuming you are doing this in a module
$fields[] = array(
'field_name' => '$field_name',
'type' => 'list_boolean',
'settings' => array(
'allowed_values' => drupal_map_assoc(range(0, 1)),
),
);
Instead of using 'default_value', I got it to work by using 'default_value_function' and creating a function that returns array(array('value' => 1)).
I'm trying to use Drupal 7's entities and field API to correctly build a new module. What I have been unable to understand from the documentation is the correct way to use the new API to create a 'content type' (not a node type) with a number of set fields, such as Body.
I'm trying to set up the entity using hook_entity_info, then I believe I need to add the body field using field_create_instance, but I can't seem to get it to work.
In mycontenttype.module:
/**
* Implements hook_entity_info().
*/
function mycontenttype_entity_info() {
$return = array(
'mycontenttype' => array(
'label' => t('My Content Type'),
'controller class' => 'MyContentTypeEntityController',
'base table' => 'content_type',
'uri callback' => 'content_type_uri',
'entity keys' => array(
'id' => 'cid',
'label' => 'title',
),
'bundles' => array(
'mycontenttype' => array(
'label' => 'My Content Type',
'admin' => array(
'path' => 'admin/contenttype',
'access arguments' => array('administer contenttype'),
),
),
),
'fieldable' => true,
),
);
return $return;
}
/**
* Implements hook_field_extra_fields().
*/
function mycontenttype_field_extra_fields() {
$return['mycontenttype']['mycontenttype'] = array(
'form' => array(
'body' => array(
'label' => 'Body',
'description' => t('Body content'),
'weight' => 0,
),
),
);
return $return;
}
Then does this go in the .install file?
function mycontenttype_install() {
$field = array(
'field_name' => 'body',
'type' => 'text_with_summary',
'entity_types' => array('survey'),
'translatable' => TRUE,
);
field_create_field($field);
$instance = array(
'entity_type' => 'mycontenttype',
'field_name' => 'body',
'bundle' => 'mycontenttype',
'label' => 'Body',
'widget_type' => 'text_textarea_with_summary',
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
field_create_instance($instance);
}
I think your problem is that if node module is installed, there is already a field named 'body'. You should either re-name your field to something like 'mycontenttype_body' (comment.module uses comment_body), or re-use the 'body' field and skip the adding the field part and skip to adding the instance of it. The former is recommended over the latter.
Every field has an array property, entity_types, which limits the entities to which the field can be attached.
The best Drupal solution I can find, hook_field_create_field, can alter fields as they are created, but that's no good for the body field which is created on installation.
So my solution is just to edit the database directly in my hook_install
$data_col = db_query("SELECT data from field_config where field_name = 'body'")->fetchAssoc();
$data = unserialize($data_col['data']);
$data['entity_types'][] = 'MY_ENTITY_TYPE';
db_update('field_config')
->fields(array('data' => array('data' => serialize($data))))
->condition('field_name', 'body')
->execute();
just started down the same path here is a video from fago
Here's a nice repo to start: Lawmakers entity