Can't avoid field label to be echoed - cakephp

I've been creating an app preparing it already to be internationalized. But when I try to do this:
echo $this->Form->input('end_date', array('label' => __('End Date'), 'dateFormat' => 'DMY', 'minYear' => date('Y'), 'type' => 'text'));\
It echoes two "End Date". I tried disabling label by setting it to null, but didn't work either. :(
How can I avoid this to happen?
Thanks

Instead of NULL, try setting it to false:
'label' => false
You can also set ALL inputs of a form to default to 'label'=>false by using:
'inputDefaults'=> array('label'=>false)
as an option of your form

For what it's worth, the __() function echoes its value by default instead of returning it. That's why you were seeing the label displayed twice. It was being displayed once because that's the value that the field name resolves to automagically. It was being displayed the second time by the __() method. In other words, your label option wasn't really overriding the automatic label.
echo $this->Form->input(
'end_date',
array(
'label' => __('Modified End Date Label', true), # note the "true" argument
'dateFormat' => 'DMY',
'minYear' => date('Y'),
'type' => 'text'
)
);
For more, see the __ documentation.

Related

Label for Select Form Helper

I"m having trouble with CakePHP3's Form Helper class.
When I create a text input field like this:
echo $this->Form->input('fieldname');
When the input field is "required", a label appears in bold. This is fine.
However, when I create a select field like this:
echo $this->Form->select('fieldname', [1,2,3,4,5]);
The select field is created however there is no label at all. I can add it manually using:
echo $this->Form->label('fieldname');
However in cases where the select is a required field, the font is not correct (required labels show up bold with a red * following them). I've narrowed this down to the CSS only applying to nested label tags (so when I create a label tag, it's not inside a div tag like the labels for the text inputs are).
I'd like to avoid manually inserting HTML code to achieve my desired result, any help would be appreciated!
<?= $this->Form->input('fieldName', ['type' => 'select', 'options' => ['0' => 'Option1' , '1' => 'Option2'], 'empty' => __('(choose one)'), 'label' => __('LabelName')]);?>
or
$sizes = ['s' => 'Small', 'm' => 'Medium', 'l' => 'Large'];
<?= $this->Form->input('fieldName', ['type' => 'select', 'options' => $sizes, 'default' => 'm', 'label' => __('LabelName')]);?>

CakePHP: How to use HTML entities in button titles using Form Helper

I'd like to use a checkmark as the text for a button. The entity I want is ✓ but when I put that in as the text for the button using the Form Helper, it always converts the leading ampersand into & so that the text shows but not the entity.
Here's how I'm creating the button:
echo $this->Form->button(
'✓',
array(
'type' => 'submit',
'id' => $checklistItem['ChecklistItem']['id'],
'escape' => 'false'
)
);
and the generated HTML looks like this:
<button type="submit" id="1">&#x2713;</button>
which obviously doesn't render the entity.
I've tried it by setting 'escape' => 'true' but that has not effect at all.
Any ideas?
You do not need to escape it false, it is by default escaped to false.
echo $this->Form->button('✓',
array(
'type' => 'submit',
'id' => $checklistItem['ChecklistItem']['id']
)
);
You need to move your escape to the third element of the button:
echo $this->Form->button(
'✓',
array(
'type' => 'submit',
'id' => $checklistItem['ChecklistItem']['id'],
),
array('escape' => 'false')
);
Also, this has already been answered here.

Cake PHP Checkboxes

I am new to CakePHP now I'm working on checkbox I used the following statement but it
gives check box after the label and it prints the field also.My requirement is it does not
print the field name and label should be displayed after the check box.
please help me ,
Thanks in advance
<?php echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' =>
$options, 'selected' => $selected));?>
First, make sure your value is a boolean or tinyint. Otherwise, you will never get a checkbox.
Then, just build like this :
echo $this->Form->input('Model.field', array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
)
));
My solution is according to v.2.0
<?php
echo $this->Form->input('field_name', array(
'label' => 'Some label',
'selected' => $selected
/*maybe some other options*/
));
?>
if you've specified model name above, while creating the form, you dont need to use name of model . If field is boolean, you'd get the control as checkbox automatically. Alsom you can specify it in options array like
'type'=>'checkbox'
good luck!
To draw a check box you have to first configure your table in DB properly. Set these options on your field in DB:
Field Type = Tinyint
Length/Values = 1
Set Defualt = 0
and finally your view:
echo $this->Form->input('checkbox_field');
100% will work if not then set default value for your field in view:
echo $this->Form->input('checkbox_field', array('type'=>'checkbox'));
CakePHP 3.0
$this->Form->input('id', ['type'=>'select', 'multiple' => 'checkbox', 'options'=>$array]);

CakePHP Form Helper - Show error class, but not error message

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.

Multiple checkboxes in CakePHP - how to set which are checked?

I have multiple checkboxes in CakePHP's Add/Edit view, created with:
echo $this->Form->input('email_warning_chb', array('type'=>'select', 'multiple'=>'checkbox', 'label'=> __('Email notice'), 'class'=>'multiple-chb', 'options'=> array('title...'=>array( '5'=>'5 days', '15'=>'15 days', '30'=>'30 days', '60'=>'60 days');
My question is how to set which one are checked by default (ie. in thi example, 5, 15 and 60)?
Thank you in advance!
As said in other answers, you should set the 'selected' option.
What some people don't mention is that your selected array should only contain the id in each element.
Example:
$selectedWarnings = $this->Warning->find('list', array(
'fields' => array('id')
));
echo $this->Form->input('email_warning_chb', array(
'label' => 'Email Notice',
'type' => 'select',
'multiple' => 'checkbox',
'options' => $warnings,
'selected' => $selectedWarnings
));
this looks like this one
cakephp: How to set checkbox to checked?
where $selected contains the selected values
in your controller you have to put the value like this:
$this->request->data['Model']['email_warning_chb'] = array(5,15,60);
and it will automatically display checkbox as selected.
Please ask if not work for you.

Resources