I have a custom content type called events which has a few fields defined in it.
The field name is field_store_name. I can get all the options from these check boxes using this code:
$form['field_store']['und']['#options']
This is how I get the option(s) that are selected/checked. Is this the correct way of doing this?
$form_state['build_info']['args']['0']->field_store['und']
Thanks
When user submits form your custom submitter could be called.
To add custom form submitter to any form you should use:
/* Implements hook_form_alter(). */
function moduleName_form_alter($form, $form_state) {
// ...
$form['#submit'][] = 'moduleName_submitterName';
// ...
}
So in custom submitter you will have all submitted values under $form_state['values']:
function moduleName_submitterName($form, $form_state) {
dpm($form_state['values']);
}
This index will apper in $form_state array only when you submit form and will contain submitted values. $form array will still contain default values shown at form before you've changed them and submitted form.
Read more:
An example of form submitter: https://www.drupal.org/node/717740.
hook_form_alter(): https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7
Value you need should be $form_state['values']['field_store]['und'][0].
Related
I need to modify a field in the SUBMIT form event, but when I do any validation rules on the field are lost.
This is all that's happening in the form type (the title field isn't actually being changed I'm just using it as an example):
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("title");
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$form->add("title");
});
}
Any validation rules for 'title' are now lost, either annotation rules defined with the entity or using a separate validator class.
Can I do anything to keep the validation or is it intended that validation rules don't get run for fields which are modified in the SUBMIT event?
If you can handle the FormEvents::POST_SUBMIT event instead of FormEvents::SUBMIT you will keep the validation. You will need to make sure that the listener is on the child form that you want to edit, otherwise you will have an issue with not being able to add a field to a submitted form.
In this instance you're not actually modifying a field you're adding a new one with $form->add('title') which will replace the existing 'title' field within the form (which is why the validation constraints are disappearing). You might want to look into validation groups for the type of functionality you're aiming for unless you want to elaborate on what you're doing within the submit event?
When a form doesn't validate, I need to access the submitted data inside a Form Class in order I can set some options in a custom field.
I have tried with
$data = $builder->getForm()->getData();
$data = $builder->getData();
but $data has the empty object. So... what is the correct form to access the submitted data by the user after validation error in the form class?
Thanks
The problem is you're trying to access submitted data when it has not be handled yet. Basically, when you are in a builder (buildForm for the abstract types), you are building your form structure. It has nothing to do with form submission/binding. This is why you get the initial data when you call $builder->getData() because it only know the initial data at this state.
Knowing that the form component allows you to access the submitted data via events. You can attach a listener to your builder and rely on one of the *_submit event. The FormEvent class will given you the submitted data with $event->getData().
See this doc for more information: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html
Look into $options variable (var_dump it)
As I remeber you are looking for
$options['data']
Using Form Events.
For those who wonder how Form Events are used.
Here is an example where you can modify the form after the user has tapped the submit button.
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
// ...
/* Listener to order to set a price if it does not exist yet */
$builder->get('price')->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
// modify it as you wish
$event->setData($data);
});
The FormEvents::PRE_SUBMIT event is dispatched at the beginning of the
Form::submit() method.
If needed, here is an example where you can modify the form price before you display it.
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
// ...
/* Listener to order to set a price if it does not exist yet */
$builder->get('price')->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
if (null === $data) { $data = '0.00'; }
$event->setData($data);
});
The FormEvents::PRE_SET_DATA event is dispatched at the beginning of
the Form::setData() method.
in some content type form I added a checkbox. When this checkbox is checked I want to remove some of the submitted data.
To do so I created a custom module (my_module.module):
function my_module_form_alter(&$form, &$form_state) {
// ...
$form['#submit'][] = 'my_module_form_alter_submit';
}
function my_module_form_alter_submit($form_id, $form_values) {
drupal_set_message(t('Submit Function Executed!'));
}
How can I tell this module to refer only to the form of a certain containt type? And how can I remove data when it is submitted?
Assuming you are altering a node edit form, you can either conditionally add the submit callback such as (in your hook_form_alter):
if(isset($form['#node']) && $form['type']['#value'] == 'page') {
$form['#submit'][] = 'my_module_form_alter_submit';
}
or, you could check the $form argument in the submit callback in a similar fashion.
You are missing the third argument to the hook_form_alter which should be $form_id, and your submit callback should take the arguments such as:
function my_module_form_alter_submit($form, &$form_state) { ... }
See also:
http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_form_alter/6
http://api.drupal.org/api/drupal/developer%21topics%21forms_api.html/6
To remove data after the submit, in your form_alter function, just use unset() on the form field. unset($form['my-field']);
hey i got a contnet type that i made and i want to twweak it some , when i do form alter its all good but once i do submit its ignore my changes that i added a field for example, how can i do that changes and tell him to insert also that field? do i need to put all the fields again in hook_submit?
$function YOURMODULE_form_alter(&$form, &$form_state) {
//...
$form['#submit'][] = 'YOURMODULE_submitfunction';
//...
}
function YOURMODULE_submitfunction($form, &$form_state) {
// Save your own changes here to DB or something other
}
I have a content type and I wish to edit the submit function. I thought the way you would do this would be as follows:
function moduleName_contentType_node_form_submit($form, &$form_state){
drupal_set_message(t('Test'));
}
I cleared the cached but the message is not being displayed on the screen. Am I doing this correctly or do I need to use form_alter? If so how would I do that?
In this case too you can use form alter, and add
$form['#submit'][] = 'your_sumbmit_callback';
or if you want to completely change the submit and do your own thing:
$form['#submit'] = array('your_submit_callback');
And obviously the callback function needs to be defined;
function your_submit_callback( $form, &$form_state) {
drupal_set_message('hello');
}