CakePHP 3: FormHelper to load <label> after <input>? - cakephp

Is there any way to load <label> after <input>. By default CakePHP loads <label> before <input> like this:
Php Code:
<?php echo $this->Form->input('email', ['placeholder' => 'Email']); ?>
Generated Code:
<label for="email">Email</label>
<input name="email" placeholder="Enter your email" id="email" type="email">
But what do I when I would like to get it like this:
<input name="email" placeholder="Enter your email" id="email" type="email">
<label for="email">Email</label>
Is there a way to have this besides hacking something?

Templates
You can use the templating functionality, the template responsible for this is the formGroup one, which by default is {{label}}{{input}}.
echo $this->Form->input('email', [
'placeholder' => 'Email',
'templates' => [
'formGroup' => '{{input}}{{label}}'
]
]);
It's also possible to define form group tempaltes per input type, by following the naming convention that is the name of the type, followed by FormGroup. So for the email type that would be emailFormGroup.
echo $this->Form->create(null, [
'templates' => [
'emailFormGroup' => '{{input}}{{label}}',
'textFormGroup' => '{{input}}{{label}}'
// ...
]
]);
That way you can easily apply this option globally to specific types only.
See also Cookbook > Form Helper > Customizing the Templates FormHelper Uses
CSS
And of course you could also use CSS, there's tables
.input.email {
display: table;
width: 100%;
}
.input.email input {
display: table-header-group;
}
.input.email label {
display: table-footer-group;
}
flex-box ordering
.input.email {
display: flex;
flex-flow: column;
}
.input.email input {
order: 1;
}
.input.email label {
order: 2;
}
etc...

You will have to use the label() method of the FormHelper, like this:
<?php
echo $this->Form->input('email', ['placeholder' => 'Email', 'label' => false]);
echo $this->Form->label('email', 'Email');
?>

One other approach is to create a Custom helper and overwrite the existing one using the way #Choma suggested.
So you will have to add in your
//View/Helpers/MyCustomFormHelper.php
App::uses('FormHelper', 'View/Helper');
MyCustomFormHelper extends FormHelper{
public function input($fieldName, $options) {
$defaults = array_merge($options, array('label' => false))
echo parent::input($fieldName, $defaults);
echo parent::label(fieldName);
}
}
//AppController.php
class AppController extends Controller {
public $helpers = array(
'Form' => array(
'className' => 'MyCustomFormHelper'
)
);
}
Or you could use the after option and for doing it more easy again inside your own helper.
//View/Helpers/MyCustomFormHelper.php
App::uses('FormHelper', 'View/Helper');
MyCustomFormHelper extends FormHelper{
public function input($fieldName, $options) {
$after = parent::label(fieldName);
$defaults = array_merge($options, array(
'label' => false,
'after' => $after,
));
return parent::input($fieldName, $defaults);
}
//or do not overwrite the input function and use a custom one
public function inputLabelAfter($fieldName, $options) {
...
...
}
}
I am sorry if the code has errors I haven't tested it but I think you got the picture and gave you more ideas.
Edit: I hadn't seen that this is for CakePHP 3.x. The above example is for 2.x but I believe that it will do.

Using templates is the right way to go - I hope this helps...
$this->Form->templates([
'nestingLabel' => '{{input}}<label{{attrs}}>{{text}}</label>',
'formGroup' => '{{input}}{{label}}',
]);
Moving Checkboxes & Radios Outside of a Label

Related

How to update a input data by jQuery from another input selection in one form?

I am trying to transfer a value automatically from selection input area to another input area when the selection input has changed. But I cannot. Please help me. The code is here:
<?php
echo $this->Form->input('treatment',
array('options' => $types, 'id' => 'treatment_foo'));
echo $this->Form->input('fee', array('id' => 'fee_foo'');
echo $this->Js->get('#treatment_foo')->event('change',
$this->Js->request(
array(
'update' => '#fee_foo',
'dataExpression' => true,
'data' => '10'))
);
?>
Just use some JQuery
$(document).ready(function() {
$('#treatment_foo').change(function() {
$('#fee_foo').val($('#treatment_foo').val());
});
});

Cake PHP 2 custom Form->Label

I'm using the Form helper to generate a label:
$this->Form->label('Contact.name', 'Name');
Which generates the following:
<label for="ContactName">Name</label>
Is it possible to generate the following using the helper:
<label for="ContactName"><span class="mandatory">*</span> Name</label>
Whilst I can manually write the html for the above it becomes a little more difficult when I am using the input method where a label is automatically generated.
For example:
$this->Form->input('Contact.forename',array('div' =>false,
'label' => array(
text'=> 'First Name',class =>'myclass'),
'class' => 'input','size' => '25' ,'tabindex' => '1'));
Is this possible in cake or do I have to manually inject the html using javascript when the page loads? Which I would think is rather ugly.
If you are using model validation for the mandatory fields then cakephp automatically applies '*' on the Label else you can use the helper as follows-
echo $this->Form->label('name', '<span class="mandatory">*</span> Name');
If you don't want the labels to generate automatically you can use "label => false" while using the helper.
echo $this->Form->input('Contact.forename',array('label' =>false));
Not sure CakePHP supports that (and it would get a bit messy anyway). The simplest solution I can think of is to assign a "mandatory" class to the label via the form helper:
$this->Form->label('User.name', 'Your username', array('class'=>'mandatory'));
Which produces something like:
<label class="mandatory" for="ContactName">Name</label>
Then the rest is done purely in CSS:
label.mandatory:after {
content: ' *';
color: red;
display: inline;
}
Avoids having any additional HTML.
I know this is old, but maybe someone with Cakephp 3 is having the same problem. This is what fixed it for me, without any inlinecode.
<?php
echo $this->Form->input(
'renovate_old',
[
'type' => 'checkbox',
'label' => ['text' => __('Alte Wohnung'), 'class' => 'moCheckLabel']
]); ?>
So you can name your label and use the Databasefield to write.
you can do it simple by
echo $this->Form->input('whatever', array('between'=>'<label for="ContactName"><span class="mandatory">*</span> Name</label>','label'=>false));

Cakephp Form Radio Button Label Class

I want to use Radio Button using the Form Helper. A radio button has a Radio element and a Label. I have by default Display: block for Label element. I want to assign a class the the label of the Radio button so that i can assign it a inline-block.
$attributes = array('legend' => false, 'label' => array('class' => 'radioBtn'));
echo $this->Form->radio('gender', $options, $attributes);
How can i assign a class to the label of the option
By looking at the Form->radio() method code, nothing seems related to attributes belonging to the labels.
But to modify the display of these labels, you could use a surrounding div
echo '<div class="inline_labels">';
echo $this->Form->radio('gender', $options, $attributes);
echo '</div>';
and use CSS like this:
.inline_labels label
{
display: inline-block;
}
How about using FormHelper::label(string $fieldName, string $text, array $options)
You could define the label class in the options array, so (for example):
echo $options = array( /* relevant stuff goes here */ );
echo $attributes = array( /* relevant stuff goes here */ );
echo $this->Form->radio('gender', $options, $attributes);
echo $this->Form->label('gender', 'Text of your label', array('label'=>'radioBtn'))
Source CakePHP Cookbook on FormHelper
Work for me:
// Add your own label with CSS class
$opts = array('1' => "<label class='myCSS'>My first option</label>", "2" => "<label class='myCSS'>My second option</label>");
// Put label param to false
echo $this->Form->input('my-input', array('type' => 'radio', 'label' => false, 'options' => $opts, 'legend' => false));
Enjoy

Give ID to submit button

I have a form and i have ended it using,
echo $this->Form->end('Login');
However, i can't seem to assign it an id or class to style it and be able to reference it with jquery. Also,I want to style it with a picture and the picture has the text already in so i need the buttons value to be empty but i can't manage that either...
Any Help?
Many Thanks, Chris
***Note - this is the html it generates;
<div class="submit"><input type="submit" value="Login"/></div>
Ideally i would like to remove the div aswell but its not a necessity.
here's the solution:
$options = array
(
'label' => 'Update',
'value' => 'Update!',
'id' => 'blabla',
'div' => array(
'class' => 'glass-pill',
)
);
echo $this->Form->end($options);
Cheers! =)

Update Input with select in CakePHP and FullCalendar

Hey all,
I'm making a booking calendar for a rental house and I'm trying to make a selectable calendar update two form fields after selection. I want users to be able to drag across available days and populate the start and end date of my form. This is what I have so far:
<script type='text/javascript'>
$(document).ready(function() {
$('#calendar').fullCalendar({
events: "bookings/feed",
selectable: true,
unselectAuto: false,
select: function(startDate, endDate, allDay, jsEvent, view) {
**I just don't know what to put here to edit the inputs bolded below**
}
});
});
</script>
<div style="width: 500px;"><div id="calendar"></div></div>
<div id="eventdata"></div>
<div id="form">
<?php echo $form->create('Booking'); ?>
**<?php echo $form->input('start', array( 'label' => 'Check In:', 'type' =>'text')); ?>
<?php echo $form->input('end', array( 'label' => 'Check Out:', 'type' =>'text')); ?>**
<?php echo $form->input('firstName'); ?>
<?php echo $form->input('lastName'); ?>
</div>
Cheers!
Solved! The following passes the Start and End dates to the form.
select: function(startDate, endDate) {
$( '#BookingStart' ).val(startDate)
$( '#BookingEnd' ).val(endDate)
}
The code in your question didn't paste correctly, but in general to change the input value in jQuery you can do $( '#id_name' ).val( 'new value' ). CakePHP assigns form field ids with the pattern ModelField so the ids you need should be "BookingStart" and "BookingEnd" (you can verify by looking at the HTML source). You can also give the fields any id you want with 'id' => 'foo' option.

Resources