I have added a custom validation handler to a node creation form that has an embedded field_collection. In the validation handler I call
<?php form_set_error("field_location", 'Please enter the primary location name.'); ?>
where field_location_name
is the field name of the field_collection. This does indeed work – it stops the form from submitting, displays the error, and highlights the entire field_collection container and all contained fields. The problem is that I only want to require a couple of items in the field_collection, so I've tried calling something like:
<?php form_set_error("field_location']['und'][0]['field_location_name", 'Please enter the primary location name.'); ?>
and while the form prevents submission, this specific text field is not highlighted. Is there a way to call form_set_error on a specific field inside of a field_collection?
Comments
Comment #1
agileadamI'm using something similar and for me, it works.
field_monetary_distribution is the field collection.
field_distribution_percentage is the field within the collection
Comment #2
charlie-s CreditAttribution: charlie-s commentedagileadam - you're right, that does work. Here's what I've found that's causing the problem for me. In addition to the form_set_error, I'm also marking the fields as required in the hook_form_alter() like so:
$form['field_collection_name'][LANGUAGE_NONE][0]['field_name'][LANGUAGE_NONE][0]['value']['#required'] = TRUE;
This gives the field the red asterisk and "required" class and such, but it also seems to be making my form_set_error not work as expected. Since this is likely something to do with the form API or just my misunderstanding of the #required attribute, I'm marking as "works as designed" and will hopefully find the solution elsewhere.
Comment #3
charlie-s CreditAttribution: charlie-s commentedActually, I'd like to reopen. How can one set a field_collection item to be required and then subsequently handle validation via a custom message without having duplicate messages printed about a field? Example:
Having to add the '#required' and 'error' class back into the form element is silly enough, but I also now get 2 error messages:
Is there a way to combat this? What is the best practice for working with field collection fields programmatically in relation to form validation?
Comment #4
liyanfei CreditAttribution: liyanfei commentedfunction mymodule_article_form_validate($form, &$form_state) {
if (!is_numeric($price) ) {
form_set_error('field_ticket][' . LANGUAGE_NONE . '][0][field_price', t('Please enter number under the tickets '));
}
}
It works for me.
Thanks
Comment #4.0
liyanfei CreditAttribution: liyanfei commentedHad wrong element name.
Comment #5
raushan CreditAttribution: raushan commentedits also working for drupal8 and drupal7, please see example for drupal8 for title and alt field for image or normal title field.
Comment #6
NoRandom CreditAttribution: NoRandom as a volunteer commentedI'm struggling to validate one of my node fields, so let's see if you can find what's my mistake.
My current configuration is:
Drupal 7
Node with custom fields group (using module "Field Group 7.x-1.5")
That group contains a text field.
Personalized module to validate node editing (WIP).
The group machine name is group_meta_data and the field name is field_mdata_date (unique and no multi-langual site).
So in my module I have:
What should I exactly put in $name? because so far I get the error message in the notification area of Drupal when editting the node but I don't se the field or the group highlighted in red. So, I don't have visual indication of which field is wrong.
Regards and thanks in advance.