i have some data that will be added in hidden input fields inside a form.
Now since i'm using symfony2 forms those fields don't get submitted.
i.e. $form->getData() does not get data from those fields.
how do i get data from those dynamically added (hidden) input fields as well?
Fetch post values in context of a form type:
$postData = $request->request->get('form_name');
$name_value = $postData['title'];
Fetch post values not in context of a form type (maybe this is what you need):
$data = $request->request->all();
$name = $data['input_name'];
Related
Let's say I have a Callback validator attached to a field in a collection sub-form that looks like this:
$callback = new Callback([
'callback' => function($obj, $context) {
// Value of field to validate, no problem
$value = $context->getValue();
// Get the form, but gets my parent form
$form = $context->getRoot();
// Try and get other field value I need to validate current field,
// but gets "child "otherfield" does not exist" error.
$otherValue = $form->get('otherfield')->getData();
// continue validation logic...
}
]);
I need the value of another field from the specific form instance for the collection item, but I can't find a way to access the child form instance. How do I do this? I can access the list of forms for the collection by using the parent field that holds the collection, but I need the specific instance that this is validating.
It's Symfony version 3.3, but I can't find anything in a later version, either.
I have an inline entity form complex and I want to get form state values before submit in drupal 8. I have tried using:
function mymodule_inline_entity_form_entity_form_alter(&$entity_form, &$form_state)
{
if ($entity_form['#entity_type'] == 'care_giver')
{
//collect values from the form
$name = $form_state->getUserInput();
}
}
I want to collected values from the inline entity form and save them to another entity programmatically.So when I try to collect data using the above code I get null results
I'm using Ninja Form plugin in my WordPress installation.
My form has 3 input text fields.
I need, after pressing the submit button, to validate one of this input by checking if the entered value exists in a custom table in my database.
If the value doesn't already exists nothing should happen (Ninja Form save the form), if it exists I need to add a Ninja Form error and let the user change the input in order to save the form with a new value.
How can I hook the submit action? How can I get in this hook the input value I need? How can I add a Ninja Form error if the value exists in order to prevent the form save?
You can do this using the ninja_forms_submit_data hook. There you can access the value of a field using its id through the variable $form_data. When adding an error message for the field to $form_data['errors'] the form will not be saved.
Like this (in functions.php):
add_filter('ninja_forms_submit_data', 'custom_ninja_forms_submit_data');
function custom_ninja_forms_submit_data($form_data)
{
$field_id = 2;
$field_value = $form_data['fields'][$field_id]['value'];
$exists = true; // Check your database if $field_value exists
if($exists)
{
$form_data['errors']['fields'][$field_id] = 'Value already exists';
}
return $form_data;
}
This question already has answers here:
Symfony2: Test on ArrayCollection gives "Unreachable field"
(4 answers)
Closed 6 years ago.
I use two ways to test my forms:
By using $form = …->form();
Then setting the values of the $form array (more precisely this is a \Symfony\Component\DomCrawler\Form object):
Full example from the documentation:
$form = $crawler->selectButton('submit')->form();
// set some values
$form['name'] = 'Lucas';
$form['form_name[subject]'] = 'Hey there!';
// submit the form
$crawler = $client->submit($form);
By sending the POST data directly:
The previous code doesn't work with forms which manage collections (relying on fields created by Javascript) because it throws an error if the field doesn't exist. That's why I also use this other way.
Full example from the documentation:
// Directly submit a form (but using the Crawler is easier!)
$client->request('POST', '/submit', array('name' => 'Fabien'));
This solution is the only way I know to test forms which manage collections with fields added by Javascript (see link to documentation above). But this second solution is harder to use because:
it doesn't check which fields exist, this is impractical when I have to submit a form with existing fields and a collection which relies on fields created dynamically with Javascript
it requires to add the form _token manually
My question
Is it possible to use the syntax from the first way to define the existing fields then add new dynamically created fields with the second syntax?
In other words, I would like to have something like this:
$form = $crawler->selectButton('submit')->form();
// set some values for the existing fields
$form['name'] = 'Lucas';
$form['form_name[subject]'] = 'Hey there!';
// submit the form with additional data
$crawler = $client->submit($form, array('name' => 'Fabien'));
But I get this error:
Unreachable field "name"
And $form->get('name')->setData('Fabien'); triggers the same error.
This example is not perfect because the form has no collection, but it's enough to show you my problem.
I'm looking for a way to avoid this validation when I add some fields to the existing form.
This can be done by calling slightly modified code from the submit() method:
// Get the form.
$form = $crawler->filter('button')->form();
// Merge existing values with new values.
$values = array_merge_recursive(
$form->getPhpValues(),
array(
// New values.
'FORM_NAME' => array(
'COLLECTION_NAME' => array(
array(
'FIELD_NAME_1' => 'a',
'FIELD_NAME_2' => '1',
)
)
)
)
);
// Submit the form with the existing and new values.
$crawler = $this->client->request($form->getMethod(), $form->getUri(), $values,
$form->getPhpFiles());
The array with the news values in this example correspond to a form where you have a fields with these names:
<input type="…" name="FORM_NAME[COLLECTION_NAME][A_NUMBER][FIELD_NAME_1]" />
<input type="…" name="FORM_NAME[COLLECTION_NAME][A_NUMBER][FIELD_NAME_2]" />
The number (index) of the fields is irrelevant, PHP will merge the arrays and submit the data, Symfony will transform this data in the corresponding fields.
In a typical Symfony2 form, when a field is invalid, the form is presented again to the user with all fields repopulated and an error on the specific field that has an issue.
In my form, I want to force the user to reenter the values of one field (for security reasons), but keep the rest of the fields populated. Is there any way to unset/clear a fields value from the controller in SF2?
Just set it to null — or whatever an empty value is — on the model object itself.
if ($form->isValid()) {
// ...
} else {
$object->setSomeField(null);
}
After you change the value on the object as shown in the answer below, you need to create the form again and pass the object to the form when you do...
$form = $this->createForm(new YourFormType(), $object);
return $this->render('YourBundle:YourEntityName:yourTemplate.html.twig, (array(
'entity'=>$entity,
'form'=>$form->createView()
));