Drupal Custom Module / Form Question: Adding an array of fields - drupal

I'm creating a custom module where I'd like to have the "add another item" functionality for a particular field, but I can't seem to figure out what I need to do in order to accomplish this... I've been going through Drupal forums and their Forms API Reference, but I must not be getting something.... I'm using Drupal 6.20, and in my module, I tried:
$form['options'] = array(
'#title' => t('Options'),
'#type' => 'fieldset',
);
$form['options']['address'] = array(
'#type'=>'textfield',
'#title'=>t('Address'),
'#tree' => 1,
);
Thinking I would get an text input that looked like this:
<input type="text" class="form-text text" value="" size="60" id="edit-address-0-value" name="address[0][value]">
But, I just get an input that looks like this:
<input type="text" class="form-text" value="" size="60" id="edit-address" name="address" maxlength="128">

You need to set #tree on the element above the one you want to duplicate. FAPI will store values in a tree structure from that element on downwards.

To get a name like address[0][value] you will need something like
$form['options']['address'] = array(
'#tree' => TRUE,
);
$form['options']['address'][0] = array(
'#tree' => TRUE,
);
$form['options']['address'][0]['value'] = array(
'#type'=>'textfield',
'#title'=>t('Address'),
);
But you don't need the [value] part unless you are actually trying to achieve multi-valued grouped fields or if your field has a complex (custom) data type implemented with multiple PHP values (ie. latitude/longitude, start/stop dates, etc.).
You will also probably need to store the number of values in something like $form['options']['#nb_values'] or in an hidden field (if you plan to add the additional fields to the form using JavaScript).

Related

Symfony EntityType Extended with Jquery Validate Plugin

I encounter a problem related to the use of the EntityType with the extended and multiple set to true (which generates checkboxes) and then validating each field separately (required or not) using the JqueryValidate plugin.
I'm using the automatic rules set by the class and attributes, so all fields work well but the checkboxes.
This happens because the validator uses the attribute name to set validation rules, but each validation rule is only applied once per name, so only the first checkbox is validated. This combined with the impossibility to change the names for the choices of an EntityType with extended and multiple, makes it impossible to use this configurations together.
CODE
FormType
$builder
->add('checkboxes', EntityType::class, [
'class' => MyClass::class,
'required' => false,
'expanded' => true,
'multiple' => true,
]);
Checkboxes rendered
<input type="checkbox" id="appbundle_myclass_checkboxes_1" name="appbundle_myclass[checkboxes][]" value="1">
<input type="checkbox" id="appbundle_myclass_checkboxes_2" name="appbundle_myclass[checkboxes][]" value="2" required="">
QUESTION
Is there a way to change the name attribute of those checkboxes so I can do multiple validations on them?
Or is there a workaround to make the validator work in these cases?

Symfony DateTime format errors - datatransformation (external datepicker)

I'm working with Symfony 6 and a TempusDomini datepicker.
I have read (amongst many others) the symfony documentation, https://symfony.com/doc/current/reference/forms/types/datetime.html.
Based on the documentation I found I initially implemented the form builder as followed:
->add('TextDateField')
->add('RegistrationDateTime', DateTimeType::class, [
'required' => false,
'widget' => 'single_text',
'format' => 'm/d/Y, h:i a',
'attr' => [
'class' => 'datetimepicker-input',
'data-target' => '#datetimepicker1'
],
'html5' => false,
])
The "TextDateField" is for troubleshooting, see explanation below.
And the actual TWIG file looks like this (for the TextDateField I have a similar datapicker block as the one for RegistrationDateTime):
{{form_start(form)}}
{{form_row(form.username)}}
<div class>
<label for="datetimepicker1Input" class="form-label">Simple picker</label>
<div class="input-group" id="datetimepicker1" data-td-target-input="nearest" data-td-target-toggle="nearest">
<input id="user_creation_form_RegistrationDateTime" name="user_creation_form[RegistrationDateTime]" type="text" class="form-control" data-td-target="#datetimepicker1">
<span class="input-group-text" data-td-target="#datetimepicker1" data-td-toggle="datetimepicker">
<span class="fa-solid fa-calendar"></span>
</span>
</div>
<script type="text/javascript">
const picker = new tempusDominus.TempusDominus(document.getElementById('datetimepicker1'));
</script>
</div>
{{form_row(form.roles)}}
{{form_row(form._token)}}
{{form_row(form.submit)}}
What I notice is that the dates are correctly given from a datepicker to the controller if passed via Text.
Please see this dump where you can see 2 datepicker values as passed to the controller, both for the same dates (6 may 2022)
App\Entity\TUser^ {#361
-id: null
-username: "scdsqd"
-roles: array:2 [
0 => "ROLE_USER"
1 => "ROLE_ADMIN"
]
-TextDateField: "05/06/2022, 12:42 PM"
-RegistrationDateTime: DateTime #1640599500 {#828
date: 2021-12-27 11:05:00.0 Europe/Berlin (+01:00)
}
}
So as you can see the text is passed correctly for the "text" datepicker but using the symfony DateTimeType::class messes it up by giving a seemingly random date.
Using other formats as datetime (for example timestamp) is not possible.
So how can I pass the value correctly to the database?
One possible "solution" is to work with TextType::class but since I want to keep the field RegistrationDateTime in the database as a DateTimeType this is not the best solution either since that would mean I have to transform the data.
Maybe I'm not looking at the right place, but it seems to me as if Symfony is not well adjusted to use external datepickers.
I have found tons of post with people struggling to implement a datepicker...
Is the approach I use correct?
How can I "force" symfony to "understand" the datepicker provided dates?
If the only way is to use TextType in the form-builder, how do I tackle the datatransformations to make sure that via doctrine I save in the datetime format?
Any pointers in the right direction are welcome... I'm getting rather frustrated by it since I've struggled on it an embarrassingly long time now...

In cakephp 3 I got error Unexpected field in POST data

In cakephp 3 I got error Unexpected field in POST data.
Actually that field is not in my table, but I want to use in controller.
The Security Component in CakePHP is not forgiving. If you want to allow a field thru that should not go thru the Security Component hashing process, you need to use the unlockedField method that comes with the FormHelper class as such:
$this->Form->unlockField('field');
If this does not work, you will need to provide us with the pertinent code
I was getting the similar error in cakephp 3.4 I was using the simple html form and input fields. I was passing the input fields data in array. like below:-
<form action="" method="post">
<input name="data[1][category_1]" id="category_1">
</form>
Then i do some R&D and found that we need to use the cakephp form helper to create the form and its fields like below :-
In case of pass form data in array
<?= $this->Form->create($user, ['url' => ['controller' => 'Users', 'action' => 'saveOrder']]); ?>
<?= $this->Form->input("Data.1.category_1"); ?>
<?= $this->Form->end() ?>
In case of simple input fields you can do the code like below
<?= $this->Form->create($user, ['url' => ['controller' => 'Users', 'action' => 'saveOrder']]); ?>
<?= $this->Form->input("category"); ?>
<?= $this->Form->end() ?>
This work form me and resolve the error Unexpected field in POST data in cakephp 3.4

Get checkbox value of own made form type in Symfony

I have added the following form typo in Symfony to my form. As it is not a member of the entity I need to handle it manually in the controller. How can i get the checkbox value in my controller?
->add('showUrl', 'checkbox', array(
'mapped' => false,
))
This is the generated html in the form:
<input type="checkbox" id="cuslocation_accountreg_showUrl" name="cuslocation[accountreg][showUrl]" required="required" value="1">
$form->getData()['showUrl']
or
$form->get('showUrl')->getData()
or
$form['showUrl']->getData()

symfony2 comparing hidden field with its hash with form validation

I would like to know how to compare fields in symfony2 form with custom validation.
In particular I want to compare a simple hidden field with its hash.
<input type="hidden" name="smoke" value="1" />
<input type="hidden" name="smoke_hash" value="kahsjkdasjkdh3iuy84932798" />
Something like "repeated Field" but validated with my own logic.
But more something like this:
use Symfony\Component\Validator\Constraints\HashMatchString;
$builder
->add('smoke', 'hidden', array(
'data' => 1,
)
)
->add('smoke_hash', 'hidden', array(
'constraints' => array(
new HashMatchString('smoke')
),
)
)
;
Form Goodness in Symfony 2.1
I’ve already see the solution of Steven Brown (http://www.yewchube.com/2011/08/symfony-2-field-comparison-validator/) but is one year ago with multiple touches on core files...
SOLVED
I’ve created a gist: Gist
Just add validation method to your entity http://symfony.com/doc/current/book/validation.html#getters

Resources