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));
Related
How can i give different title for different radio button using CakePHP Form Helper
$radio_options = array('unknown'=>'Unknown','negative'=>'Negative','positive'=>'Positive');
$titles = array('0'=>'Unknown','1'=>'Negative','2'=>'Positive');
I am trying to create radio buttons like this
echo $this->Form->input('radio_buttons', array(
'options' => $radio_options,
'legend' =>false,
'label' => true,
'div'=>false,
'class'=>'radio inline',
'type' => 'radio',
'separator'=>'<br>',
'title'=>$titles,));
But its not working..Form Helper creating same title for all of the radio buttons.
Look at what the FormHelper generates, then generate it manually or with a php foreach loop or something.
There are cases like this, when it's just easier (or the only way) to not use a helper.
i wish to display default time as 9:00 am in ctp time dropdown. Following is my ctp code:
<?php
echo $this->Form->input('Rideoffer.DepartureTime', array(
'type' => 'time',
'interval' => 5
));
?>
how do i do that?
Use the 'selected' option
<?php
echo $this->Form->input('Rideoffer.DepartureTime', array(
'type' => 'time',
'interval' => 5,
'selected' => '09:00:00',
));
?>
The best way to set default data is using the controller (and only if not posted):
if (!$this->request->is('post')) {
$this->request->data['Rideoffer']['DepartureTime'] = '09:00:00';
}
see http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#common-options
This works in all form elements.
"selected", "value", "checked" and other hardcoded attributes for the form directly usually break the form after an unsuccessful post (if the form contains validation errors): it loses all the entered data and reset them to the value it was before which is usually quite annoying for the frontend user.
see http://www.dereuromark.de/2010/06/23/working-with-forms/
I'm attempting to customize the error output on the CakePHP 2.0 form helper. Currently, the form renders error messages below the input and applies an 'error' class to the input's label.
I have found that I can either disable error reporting altogether for an input, or output the error class and message.
I would like the error class to be applied to the label of the offending inputs WITHOUT any message below.
How do you turn off the error message outputting for a form, BUT still apply error classes to offending labels?
FormHelper::input() has a format option. It is a
format template for element order. Any element that is not in the array, will not be in the output.
Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
You can pass the default format, leaving out the 'error':
echo $this->Form->input(
'some_field',
array('format' => array('before', 'label', 'between', 'input', 'after'))
);
That should produce the input markup without the error message.
If you want to apply this to multiple inputs in your form, you should keep it DRY:
$format = array('before', 'label', 'between', 'input', 'after');
echo $this->Form->input(
'some_field',
array('format' => $format)
);
echo $this->Form->input(
'some_other_field',
array('format' => $format)
);
It is also possible to set the default format for all inputs of a form by passing the format to FormHelper::create() as inputDefaults:
$this->Form->create(
'MyModel',
array(
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'after')
)
)
);
You'll have to do some of this manually. First turn off the validations, and label generation on the Form Helper.
echo $this->Form->input('myfield', array('error' => false, 'label' => false));
Then to add the class to the create the label and add the error class if validations have failed. To find out which validations failed check the invalidFields array like so:
$error = null;
if (isset($this->invalidFields['Model']['myfield'])) {
$error = 'error';
}
echo $this->Form->label('myfield', 'My Field', array('class' => $error));
You can always make use of Form->error('field_name') which should return nothing if there is no errors.
$error = $this->Form->error('field_name');
echo $this->Html->input('field_name', array(
'class' => !empty($error) ? 'error' : null,
'error' => false
));
You now have the $error with the usual markup for errors that could be displayed in another location.
There is no way to get around without checks, the Form->input() method is a convinience method that does all these things like errors, divs, labels automatically which can be done through Form->label(), Form->checkbox(), Form->select() etc which is the basic elements only.
One of the options that can be passed to Form->create() is inputDefaults which you can use to set defaults for all the other form elements. This will not help much as you are doing field by field. ('error' => false would help a bit)
The other thing you can do is make your own form helper, extending the core FormHelper and customise the input method to do this all automatically. You can use aliasing to load your custom helper into $this->Form to be used as normal. See the bottom of this section.
You can also overload the input method in AppHelper but that is not a good place for it.
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! =)
I have two tables: "restaurants" and "cuisines" which are related to each other by a HABTM table
The table cuisines has certain fixed entries - 54 number
A restaurant can have any number of cuisines. On baking the application this came with a multiple select. Since i wanted check boxes i used array( 'type' => 'select', 'multiple' => 'checkbox') to convert it into checkboxes.
Now i want to style the way this checkboxes are displayed into columns of 4 as seen on the screenshot below.
img2.pict. com/82/bc/a4/1453459/0/200908111511.png
echo $form->input('Cuisine', array('type' => 'select', 'multiple' => 'checkbox'));
The above code produces many div's around each element as follows
http://img2.pict.com/1a/a3/0a/1453457/0/200908121509.png
I have tried the following:
echo $form->input('Cuisine', array( 'type' => 'select', 'multiple' => 'checkbox', 'div' => false, 'label' => false));
but this code only removes the outside divs and label. I am not able to control the internal
<div class="checkbox">
<label for="CuisineCuisine2">Andhra</label>
that appear around the single checkboxes.
How can I use the FormHelper to remove or give classes to the internal divs, so I can do some custom styling?
Or is there any other way to populate this HABTM table to get the effect i want?
You could get around this by doing $form->select() instead, and apply a style or class attribute to get it to look how you want.
It seems to make sense to not use the $form->input() function if you are going to remove the div and label anyway.
You can stylize the DIV elements with CSS.
<style>
div.input div.checkbox {
float: left;
width: 50%;
}
</style>
You can remove or give classes to the internal divs like this
$this->Form->input("hello_test",array('type'=>'checkbox','div'=>'class_name'));
By default cake uses : type class e.g - type is checkbox then class="checkbox"