Multiple forms inside a foreach - php

I have a foreach that show many forms with the same action ending with diferente id's.
But, the tag <form> just appears in the first form. All others, the fields appears, but don't the <form>
I tried to put the id for the form different in the loop. But doesn't work.
The code:
<?php echo $this->Form->create(null, array(
'url' => array('controller' => 'menus', 'action' => 'aprovar', $procuracao['Attorney']['id']), 'id' => $procuracao['Attorney']['id']
)); ?>
<div class="control-group">
<label class="control-label">Alçada:</label>
<div class="controls">
<?php echo $this->Form->input ('alcada', array('type' => 'select', 'label' => FALSE, 'options' => array(
'Até 10.000' => 'Até 10.000',
'Até 50.000' => 'Até 50.000',
'Acima de 100.000' => 'Acima de 100.000',
'Acima de 500.000' => 'Até 500.000',),
'empty' => 'Selecione')); ?>
</div>
</div>
<div class="control-group">
<label class="control-label">Validade:</label>
<div class="controls">
<?php echo $this->Form->input('validade', array('label' => FALSE, 'type' => 'text')); ?>
</div>
</div>
<?php echo $this->Form->submit('Ok', array('class' =>'btn btn-success pull-left', 'div' => false)); ?>
</div>
The field "Alçada" and "Validade" appears correctly. But the tag <form> just appears in the first element.

You are not ending the form.
echo $this->Form->create(null, array(
'id' => 'your-form-'.$i, //that $i is the index of the foreach, for example
'url' => array('controller' => 'menus', 'action' => 'aprovar', $procuracao['Attorney']['id']), 'id' => $procuracao['Attorney']['id']
));
//all inputs and other stuff
echo $this->Form->end(array('label'=>'Ok', 'class' =>'btn btn-success pull-left', 'div' => false));
all that inside the foreach you're using.
Here is the reference of that function in the docs. But basically, it does this
Closes an HTML form, cleans up values set by FormHelper::create(), and
writes hidden input fields where appropriate

Related

CakePHP 4 FormProtector instance has not been created error

I am currently in the process of updating a (rather big) application from CakePHP 3 to 4.
I have this template:
<?= $this->Form->create(
$dmpLayer,
[
'url' => [
'controller' => 'DmpLayers',
'action' => 'edit',
],
]
); ?>
<div class="row">
<div class="col-4">
<?= $this->element('DataLayers/layer-table'); ?>
</div>
<div id="form-div" class="col-6">
<div class="layer-form">
<?= $this->element('DataLayers/form') ?>
</div>
</div>
<div class="col-2">
<div class="layer-form">
<h2>Form Actions</h2>
<?= $this->Form->submit('Create/Update Layer', ['class' => 'btn btn-success']); ?>
</div>
</div>
</div>
<?= $this->Form->end(); ?>
<?= $this->Html->script('data-layers'); ?>
which includes the DataLayers/form element:
<div class="row">
<div class="col-12">
<h4>Artist Layer</h4>
<?php
echo $this->Html->tag('fieldset', $this->element(
'actions/add',
[
'url' => [
'prefix' => 'Admin',
'plugin' => false,
'controller' => 'SegmentCores',
'action' => 'add',
],
]
)
. $this->Form->control('artist_layer.segment_cores[]', [
'multiple',
'options' => $segmentCores,
'label' => 'Segment Core',
'value' => $selectedValues['segment_cores'],
])
. $this->Form->control('artist_layer.segment_potentials[]', [
'multiple',
'options' => $segmentPotentials,
'label' => 'Segment Potential',
'value' => $selectedValues['segment_potentials'],
])
. $this->Form->control('artist_layer.layer_tags[]', [
'multiple',
'options' => $layerTags,
'label' => 'Artist Tag',
'value' => $selectedValues['artist_tags'],
])
. $this->Form->control('artist_layer.genres[]', [
'empty' => 'No genre set',
'options' => $genres,
'label' => 'Genre',
'value' => $selectedValues['genres'],
]);
?>
</div>
</div>
<?php
$this->Form->unlockField('artist_layer.genres');
$this->Form->unlockField('artist_layer.segment_cores');
$this->Form->unlockField('artist_layer.segment_potentials');
$this->Form->unlockField('artist_layer.layer_tags');
?>
In the initialize function of the AppController I have this:
$this->loadComponent('Security');
When I visit the page, it doesn't render and I immediately get this error:
FormProtector instance has not been created. Ensure you have loaded the FormProtectionComponent in your controller and called FormHelper::create() before calling FormHelper::unlockField()
This is the only form in my application for which this error happens. Every other form is working fine, and I am calling the Form->unlockField() function in many of them.
I am obviously calling Form->create() in my code, so is this because I am including an element to add fields to the form that is defined in the "main" template? Or is there some other explanation?
I have already attempted adding
$this->loadComponent('FormProtection');
to my AppController, but this causes a whole lot more problems in many other places in the app, and it doesn't solve the problem anyway (the page renders, but I get an error when submitting the form to save the data).

Show hidden select field on change using script in cakephp

I wrote this script to show hidden label and whole select field on change in other select field CakePHP,
script function is showing label but not select field.
Here is the htmlhelper:
<div class="form-group">
<label for="job_category" class="col-sm-5 control-label">Tutor City</label>
<div class="col-sm-7">
<?php echo $this->Form->input('city_id',
array('class' => 'user_login form-control',
'placeholder' =>'TutorCity',
'label' => false,
'div' => false,
'id' => 'city'
));
?>
</div>
</div>
<div class="form-group">
<label for="job_category" class="col-sm-5 control-label" id="area_label" hidden>Tutor Access Areas</label>
<div class="col-sm-7">
<?php echo $this->Form->input('area_id',
array('class' => 'user_login form-control',
'placeholder' => 'TutorAreas',
'id' => 'area',
'div' => false,
'label' => false,
'multiple' => true,
'type' => 'hidden'
));
?>
</div>
</div>
Script:
<script type="text/javascript">
$("#city").change(function () {
$("#area_label").show();
$("#area").show();
})
</script>
Any help is appreciated. Thanks.
The second input is hidden input <input type="hidden"> so to generate select field remove type => hidden
<?php echo $this->Form->input('area_id',
array('class' => 'user_login form-control',
'placeholder' => 'TutorAreas',
'id' => 'area',
'div' => false,
'label' => false,
'multiple' => true,
'type' => 'hidden' // remove this
));
?>
You need to remove the type hidden first. You can try using type text with display none. You can change the input field display block on change.

Impresspages Widget disappears after drag and drop

I've seen a strange behavior for a custom plugin. It's skin generates some html and it works fine. As soon as i drop the widget onto the page it is only visible after a page refresh. Same is true when i try to drag the widget to another position.
Has anybody experienced the same issue? I am not sure whether its a bug or i might miss something in my code.
[edit] There is an issue with ipContent()->getCurrentPage()->getId() which is NULL after performing any drag and drop action [edit]
This is my skin file:
<div>
<div class="form-group">
<div class="checkbox">
<label>
<input id="auth-email-check-nutzung" type="checkbox"><?php echo ipSlot('text', array(
'id' => 'nutzungsbedingungen_' . ipContent()->getCurrentPage()->getId(),
'default' => 'Ich stimme den Nutzungebedingungen zu.',
'attributes' => array(
'id' => 'auth-email-check-nutzung-text'
)
)); ?>
</label>
</div>
<div class="checkbox">
<label>
<input id="auth-email-check-daten" type="checkbox"><?php echo ipSlot('text', array(
'id' => 'datenschutz' . ipContent()->getCurrentPage()->getId(),
'default' => 'Ich stimme der Datenschutzerklärung zu.',
'attributes' => array(
'id' => 'auth-email-check-daten-text'
)
)); ?>
</label>
</div>
</div>
<form class="form" role="form">
<div class="form-group">
<label for="inputEmail1" class="control-label">Email</label>
<input type="email" class="form-control" id="inputEmail1" placeholder="Email">
</div>
<div class="form-group">
<?php if ((ipIsManagementState())) :?>
<?php
// Use a simulated Button to edit text and prevent any default button event
// for user friendly inline editing
echo ipSlot('text', array(
'id' => 'button' . ipContent()->getCurrentPage()->getId(),
'default' => 'senden',
'tag' => "div",
'class' => 'btn btn-danger auth-button btn-block',
'attributes' => array(
'data-target' => 'bs-button',
'id' => 'auth-email-button-div'
)
)); ?>
<?php endif; ?>
<?php if (!(ipIsManagementState())) :?>
<?php
// Display real Button only on the live page with the contents of ipSlot :
// 'id' => 'button' . ipContent()->getCurrentPage()->getId()
echo ipSlot('text',array(
'id' => 'button' . ipContent()->getCurrentPage()->getId(),
'tag' => "button",
'class' => 'btn btn-danger btn-block',
'attributes' => array(
'data-target' => 'bs-button',
'id' => 'auth-email-button',
'disabled' => 'disabled'
)
)); ?>
<?php endif; ?>
</div>
</form>
</div>
Thanks in advance.
When you reload the page, "currentPage" really exists. That's why the code works. But when you drag a new widget or move the widget, it's content is loaded using AJAX. AJAX request has no real page. So getting current page id in widget's skin is not possible at some points.

Cakephp change the Form->input output

I am trying to achieve the output where I have a wrapper div which contains a label and an inner div, and within the inner div I have the form input.
My output should look like this:
<div class="form-group">
<label>Name:</label>
<div class="form-input">
<input type="text" />
</div>
</div>
Here is my current form object in php:
echo $this->Form->input('name', array(
'class' => 'form-input',
'div' => 'form-group',
'label' => array('class' => 'control-label')));
But this adds the class form-input to the actual input itself.
How would I be able to achieve this while still keeping true to the CakePHP way of doing things?
TIA!
Use input options 'before', 'after', 'between' http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options I haven't verified it, but I think it should look something like this:
echo $this->Form->input('name', array(
'between' => '<div class="form-input">',
'after' => '</div>',
'div' => 'form-group',
'label' => array('class' => 'control-label')));
What about this:
echo $this->Form->input('name', array(
'div' => 'form-group',
'before' => '<div class="form-input">',
'after' => '</div>',
'label' => array('class'=>'control-label')
));
I think this works.

Using CakePHP FormHelper with Bootstrap Forms

CakePHP's FormHelper is how you generate forms when making CakePHP applications. As one might assume, this includes generating input elements, like so:
$this->Form->input('abc');
Which will produce HTML something like this:
<div class="input text">
<label for="ModelAbc">Abc</label>
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
Now, sadly, Bootstrap wants something like the following:
<div class="control-group">
<label for="ModelAbc" class="control-label">Abc</label>
<div class="controls">
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
</div>
How do I make CakePHP produce this output?
Inspired by lericson's answer, this is my final solution for CakePHP 2.x:
<?php echo $this->Form->create('ModelName', array(
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'div' => array('class' => 'control-group'),
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
)));?>
<fieldset>
<?php echo $this->Form->input('Fieldname', array(
'label' => array('class' => 'control-label'), // the preset in Form->create() doesn't work for me
)); ?>
</fieldset>
<?php echo $this->Form->end();?>
Which produces:
<form...>
<fieldset>
<div class="control-group required error">
<label for="Fieldname" class="control-label">Fieldname</label>
<div class="controls">
<input name="data[Fieldname]" class="form-error" maxlength="255" type="text" value="" id="Fieldname"/>
<span class="help-inline">Error message</span>
</div>
</div>
</fieldset>
</form>
I basically added the 'format' and 'error' keys, and added the control-label class to the label element.
Here's a solution for Bootstrap 3
<?php echo $this->Form->create('User', array(
'class' => 'form-horizontal',
'role' => 'form',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'div' => array('class' => 'form-group'),
'class' => array('form-control'),
'label' => array('class' => 'col-lg-2 control-label'),
'between' => '<div class="col-lg-3">',
'after' => '</div>',
'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
))); ?>
<fieldset>
<legend><?php echo __('Username and password'); ?></legend>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
In case a field needs its own label:
<?php echo $this->Form->input('username', array('label' => array('text' => 'Your username', 'class' => 'col-lg-2 control-label'))); ?>
Here's one way:
<?php echo $this->Form->create(null, array(
'inputDefaults' => array(
'div' => array('class' => 'control-group'),
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>',
'class' => '')
)); ?>
Your answer is correct, but for the benefit of other users there's some other tweaks you can do to take advantage of the error/help text:
Add form-horizontal to class in the Form->create() for more compact forms (labels on the left of the input, rather than on top)
Here's how to put help text underneath a field (has to be done for each field), not forgetting to close the </div>.
echo $this->Form->input('field_name', array(
'after'=>'<span class="help-block">This text appears
underneath the input.</span></div>'));
and to correctly display errors:
// cake 2.0
echo $this->Form->input('abc', array(
'error' => array('attributes' => array('class' => 'controls help-block'))
));
Outputs:
<div class="control-group required error">
<label for="ModelAbc" class="control-label">Abc</label>
<div class="controls">
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
<!-- error message -->
<div class="controls help-block">This is the error validation message.</div>
<!-- error message -->
</div>
It's extra mark-up and not as neat as bootstrap but it's a quick fix. The alternative is to do each error message individually.
and it lines up nicely. I haven't discovered an easy way to make use of inline messages yet however.
Applying the same principle as above to the form->end function as follows:
<?php echo $this->Form->end(array(
'label' => __('Submit'),
'class' => 'btn',
'div' => array(
'class' => 'control-group',
),
'before' => '<div class="controls">',
'after' => '</div>'
));?>
To produce:
<div class="control-group">
<div class="controls">
<input class="btn" type="submit" value="Submit">
</div>
</div>
small add for another comments:
if you whant add class and change label base text, you can write next
<?php echo $this->Form->input('Fieldname', array(
'label' => array('class' => 'control-label','text'=>'HERE YOU LABEL TEXT')
)); ?>
I had the same problem using slywalker / cakephp-plugin-boost_cake, I open a ticket and he had it fix in a few hours, he updated to 1,03 and told me to use it like this
<?php echo $this->Form->input('email', array(
'label' => array(
'text' => __('Email:'),
),
'beforeInput' => '<div class="input-append">',
'afterInput' => '<span class="add-on"><i class="icon-envelope"></i></span></div>'
)); ?>
I hope it helps some one else too
To get it working with a horizontal form in bootstrap with bootswatch I had to use:
echo $this->Form->create(
'User',
array(
'action' => 'add',
'admin' => 'false',
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array( 'before', 'label', 'between',
'input', 'error', 'after' ),
'class' => 'form-control',
'div' => array( 'class' => 'form-group' ),
'label' => array( 'class' => 'col-lg-2 control-label' ),
'between' => '<div class="col-lg-10">',
'after' => '</div>',
'error' => array( 'attributes' => array( 'wrap' => 'span',
'class' => 'text-danger' ) ),
)
)
);
Then you can just use it as normal:
echo $this->Form->input( 'User.username' );
Luc Franken posted this link in his comment: http://github.com/slywalker/cakephp-plugin-boost_cake
It took me a while to notice it, so for those who are still looking for the simplest solution:
Simply add the CakePHP Bootstrap plugin from GitHub and let the helper do the job for you!

Categories