HTML Form vs FormHelper

When building prototypes, I’m using html to build my forms. My little knowledge of CakePHP and html is just enough to get things done the way I want it to behave.

Bit by bit I’m trying make more use of the FormHelper, but in the case of the select-element, I’m getting stuck. Reading the cookbook over and over, just can’t figure out how to convert from htm-code to the FormHelper way of building the wanted form.

Took the code from the prototype to my testbed (fresh baked) to do things step-by-step. Maybe by writing things down I can find out what’s lacking.

Here’s the html-code which behaves like I want to:

<div>
<form>
<label for="amount">Amounts:</label>	
<select id="amount" name="amount">
<?php foreach ($individual->amounts as $amount) : ?>	
<option value="<?= $amount->quantity ?>"> <?= $amount->amount ?> (<?= $amount->quantity ?> g)</option>
<?php endforeach; ?>
</select>
<input type="submit" value="Submit">
</form>
</div>

<?php $quantity = $this->request->getQuery('amount'); ?>

The page and form look like:

After trying a lot of things with the related records, I restarted with just a list of options, but the
$quantity doesn’t get a value:

<div>

<?= $this->Form->create(); ?>
<?= $this->Form->select('amount', [1, 2, 3, 4, 5]); ?>
<?= $this->Form->button('Submit'); ?>
<?= $this->Form->end(); ?>

</div>

<?php $quantity = $this->request->getQuery('amount'); ?>

Looking to the generated html-code, the name stays empty, I thought that has to have value in order to work?

<select name=""><option value="0">1</option><option value="1">2</option><option value="2">3</option><option value="3">4</option>

So there’s something wrong with referring to ‘amount’, because of being a related record?

<?= $this->Form->select('amount', [1, 2, 3, 4, 5]); ?>

CakePHP uses default templates for each form element which can be seen here:

If you want to adjust those see Form - 4.x

But your problem is rather strange because I can’t see anything wrong with your code.

The only thing missing from your code is the label above the select

<div>

<?= $this->Form->create(); ?>
<?= $this->Form->label('amount', __('Amounts:')); ?>
<?= $this->Form->select('amount', [1, 2, 3, 4, 5]); ?>
<?= $this->Form->button('Submit'); ?>
<?= $this->Form->end(); ?>

</div>

but this doesn’t prevent the value of the name attribute in the select to not being present at all.

Otherwise you will have to look further into cakephp/FormHelper.php at 4.x · cakephp/cakephp · GitHub and debug your given values in your instance

Searched for a select-form in the baked templates, which I could find in the edit.php, to change the group of an individual.

The ‘name’ doesn’t have a value in the html-source:

<label for="group-id">Group</label><select name="" id="group-id"><option value="1" selected="selected">Group_A</option><option value="2">Group_B</option></select>

When using the page to change the group of the individual, there’s the message group is changed, but actually it isn’t.

Assuming the baked code is ok, the FormHelper has an issue (using cakephp 4.3.3)?

Baked templates only use Helpers and do not output form elements themselves so it has nothing to do with baking your code.

Are you sure you don’t have a custom FormHelper in src/View/Helper/FormHelper.php which extends the core FormHelper and therefore change something with the name attribute?

1 Like

No, wasn’t sure, but now I’m. Made a change and didn’t undo it correctly :blush:

Now ‘name’ has correct value, thanks.