I'm trying to use CakePHP's form helper to generate some input elements.
The HTML I am trying to generate is:
<div class="formRow">
<label>LabelText:</label>
<div class="formRight">
<input name="data[User][email_address]" type="text" value="">
</div>
<div class="clear"></div>
</div>
Ive had a look through the Cake documentation (Using 2.1) and I can't find enough information on how to do this.
It looks like I need to use the format option on the input method, but can't figure out how to get it right. Especially concerned about the div surrounding the input field with a class name on it..
E.g. Ive tried something like this:
echo $this->Form->input('email_address', array(
"input" => array('attributes' => array('wrap' => 'div','class' => 'formRight'))));
But this doesnt change any of the markup and just throws this error:
Notice (8): Array to string conversion [CORE\Cake\View\Helper.php, line 459]
So my question is how can I get this form helper to create that markup?
Any help much appreciated
You're over-thinking it. (No worries, we all do). Just remember, CakePHP is all about making things easier for you (among other things) - if you're struggling with trying to force Cake to do something for you, just remember, you can fall back to the basics - it's just PHP/HTML after-all.
<div class="formRow">
<label>LabelText:</label>
<div class="formRight">
<?php echo $this->Form->input('email_address', array(
'div'=>false, 'label'=>false)); ?>
</div>
<div class="clear"></div>
</div>
You should use the Form helper for your forms when possible, but you don't have to use all of it's presets like surrounding divs & labels. In the case above, just tell it you don't want the div, and wrap it with a div yourself.
If you don't want <div>s or <label>s around any inputs, you can also set the form's inputDefaults:
$this->Form->create('Whatever', array(
'inputDefaults' => array('label'=>false, 'div'=>false)
));
If you have a lot of fields, you can use Jquery.
php:
echo $this->Form->input('email_address', array('class' => 'formRow'));
Jquery:
$(".formRow").each(function() {
$(this).wrapInner( "<div class='formRight'></div>");
$(this).find("label").prependTo(this);
$(this).append('<div class="clear"></div>');
});
Related
This may seem obvious to some of you, but I really am struggling to find a straight answer. I've generally googled, as well as read both the CakePHP manual and the API for an answer to the following question:
When creating an input, the following code creates the following outputs:
// in the view
echo $this->Form->input('notes');
// resultant html
<div class="input textarea">
<label for="notes">Notes</label>
<textarea id="notes" rows="5" name="notes"></textarea>
</div>
Note: this is consistent across most input types; and because it's consistent it's great for formatting.
However, with a checkbox:
//In the view
echo $this->Form->input('ticket_required',
['type' => 'checkbox']
);
// resultant HTML
<div class="input checkbox">
<input type="hidden" value="0" name="ticket_required">
<label for="ticket-required">
<input id="ticket-required" type="checkbox" value="1" name="ticket_required">
Ticket Required</label>
</div>
[Note: I understand the need/desire for the hidden field]
Now.. surely it can't be an uncommon requirement to simply want the same format approach as every other standard input?
My question - how do I make CakePHP create a checkbox element as follows:
// desired HTML
<div class="input checkbox">
<input type="hidden" value="0" name="ticket_required">
<label for="ticket-required">
Ticket Required</label>
<input id="ticket-required" type="checkbox" value="1" name="ticket_required">
</div>
To be clear: the order of visible elements is the same as other generated elements (label before input, and all encased in the wrapping div).
Please note.. i have tried the 'nestedInput' => false option. This actually gets rid of the checkbox input entirely from the div.
I can't understand why this isn't done that way... but even if it was, I can't fathom why this isn't an obvious question for the documentation.
Oh well.. hopefully someone can help me here.
Thanks in advance.
Rick
I would have thought the nestedInput would work but even if it did, you don't want to add that to every input you create throughout the website.
CakePHP 3 uses string templates to build form controls. You can modify them to suit your needs.
By default the checkbox is using the nestingLabel template so if you want to stop all inputs from being nested you can change the template.
// src/View/AppView.php
$this->loadView('Form', [
templates => [
'nestingLabel' => '<label{{attrs}}>{{text}}</label>{{hidden}}{{input}}'
],
// [More helper default config overrides][2]..
])
For more control over your helpers you can create your own that extends one of the core helpers.
CakePHP Code
<?php echo $this->Form->create('KPI');?>
HTML Output
<form accept-charset="utf-8" method="post" id="..." action="...">
<div style="display:none;">
<input type="hidden" value="POST" name="_method">
</div>
I want to delete auto generated div which display in html output. How can delete that div which generated by cakephp form create?
As already mentioned in the comments, you should not remove that markup, besides that would only be possible by completely overwriting FormHelper::create(), see
https://github.com/.../cakephp/blob/2.5.6/lib/Cake/View/Helper/FormHelper.php#L426-L432
https://github.com/.../cakephp/blob/2.5.6/lib/Cake/View/Helper/FormHelper.php#L462-L464
Also note that there might be an additional hidden block at the end of the form, see FormHelper::secure().
The only simpler way to remove the wrapper, would be to remove the hidden wrappers altogether, that would for example be possible by using a custom config for HtmlHelper where the hiddenblock tag is modified so that it doesn't contain the wrapper, however that's not a good idea - don't do it!
The problem here is that you can't just remove this specific wrapping div element, the hidden input and the div go hand in hand. And the input ensures that CakePHP is able to figure the proper request method (POST, PUT, DELETE).
So instead simply make your jQuery selector more specific, do not just select div elements, instead make sure that your elements have a proper class set, and then select them by that class.
<?php echo $this->Form->create('Kpi', array(
'inputDefaults'=>array('div'=>'false', 'label'=>false)));
?>
I’m having an issue saving HABTM data in a controller.
I have an Article model, which is associated to an Event model, as articles may be about specific events in my application. I’ve read the CakePHP cookbook entry on saving HABTM data so understand how it works on the controller level, but having difficulties understanding how it should look on the view side of things.
What I have in my form are inputs for the article data (title, excerpt, content etc). I then want checkboxes for each event in my database then I can then check to associate an article with the corresponding event(s).
Normally, I would have a checkbox like this:
<input type="checkbox" name="event[]" value="1" />
Where 1 is the ID of a venue, and do a checkbox like this for every event in my database, but this doesn’t seem to work in CakePHP.
What should I put for my field name when creating checkboxes with $this->Form->input()? What’s the “CakePHP” way?
Assuming you've set the events in the controller as described in the Cook Book:
$this->set('events', $this->Event->find('list'));
then your input should look like this, basically like the example in the Cook Book, with the addition of the multiple option with an value of checkbox:
$this->Form->input('Event', array('multiple' => 'checkbox'));
This should give you a correctly formatted list of checkboxes with the events displayField as labels.
Your next problem might be validation, so also have a look at CakePHP HABTM data not saving to database
I suppose that you save your data from the ArticlesController::add().
So your view, you should have something like the following
add.ctp
<?php
echo $this->Form->create('Article');
echo $this->Form->input('Article.title', array('type' => 'text'));
echo $this->Form->input('Article.excerpt', array('type' => 'textarea'));
echo $this->Form->input('Article.title', array('type' => 'textarea'));
/** Generate multiple checkboxes, assumung that your array of events is $events **/
foreach($events as $index => $evt) {
echo $this->Form->input('Event'.$index.'.id', array('type' => 'checkbox', 'value' => $evt['Event']['id']));
}
echo $this->Form->submit('save');
?>
The foreach loop will create a set of checkboxes which looks like this
<input type="checkbox" name="data[Event][0][id] value="1">
<input type="checkbox" name="data[Event][1][id] value="2">
<input type="checkbox" name="data[Event][2][id] value="3">
...
The respected data sent to the controller can be accessed via the $this->request->data['Event'] array. If you have setup the relations correclty then the data saving
is been done automatically for you.
I hope this helps.
I came from a symfony background. In symfony to get any form element attributes that symfony generates I would do something like form.username.vars.id , form.username.vars.full_name to get id, name attribute for that field.
I was wondering how I would do this in cakephp2. For example in cakephp:
echo $this->Form->input('username');
would generate:
<input type="text" required="required" id="UserUsername" value="admin" maxlength="50" name="data[User][username]">
I only want to get the id, name generated by cakephp so that I could use in javascript . How can I do this ? Are there any helpers to do so?
The convention is consistent for naming these attributes.
Name will always be data[ModelName][field]
ID will always be ModelNameField
You can also choose the ID when you echo out the element:
echo $this->Form->input('username', array('id' => 'username'));
You can also change the name (you guess it, 'name' => 'name'), however that could definitely mess with form processing in CakePHP.
I am implementing an auto complete box using the Ajax.autocompleter method of the scriptaculous.js framework.
This is the auto complete box and the div where the auto suggested entries are populated.
<?php echo $form->create('Share', array('url' => '/forms/share')); ?>
<label for="shareWith">Share Form with</label>
<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<div id="autocomplete_choices" class="autocomplete"></div>
<input type="hidden" id="sharedUserId" name="sharedUserId"/>
<?php echo $form->end('Share');?>
This is the JQuery function to get the auto-suggested list and to get the id of the selected entry which is stored in the hidden field of the form.
new Ajax.Autocompleter("autocomplete", "autocomplete_choices",
"http://localhost/FormBuilder/forms/autoComplete",
{
tokens: ',',
afterUpdateElement : getSelectedId
}
);
function getSelectedId(text, li) {
$("#sharedUserId").val(li.id);
}
Suppose if I select multiple entries,how to send those values?
Can I have an array as a hidden field, so that I can have an array of the selected elements and save that array as a hidden field?
Simply create a new hidden input field for every selected ID, and make sure that for each you have name="sharedUserId[]". This doesn't follow the CakePHP form element naming convention, but it will make sure that the POSTed value of sharedUserId is an array.
serialize with json and parse it in the server back. PHP 5.2 can parse json natively.
Not related to your question though..
http://docs.jquery.com/Plugins/Autocomplete
jQuery Auto complete you