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/
Related
I have a form with a dropdown that is multiple => true
echo $this - > Form - > input('test_id', [
'options' => $tests,
'required' => true,
'empty' => 'Select Tests',
'multiple' => true
]);
But when i submit the form only one value is saved in database.
I have searched for solution and found this:
Multiple select in input Cakephp
It suggests using SaveMany in my Controller, but i can't figure it out how to use it to get my desired output.
I'm very new to this framework so any help would be appreciated.
sorry i m also new but as other answers and question i don't see 'selected' => $selected in your form
CakePHP 2.0 Select form Mulitple selected
and How to set selection in mulitiselect list box in cakephp
I'm editing a dropdown box. i need to use empty and selected in same dropdown.
here is my code
$this->Form->input('per', array('id'=>'per','class'=>'inputs con_field','label'=>'per :', 'type'=>'select','options'=>$per_values,'selected'=>$labrcfps['Labourcfps']['per'],'empty'=>'- - Select --'));
If i use empty it automatically takes the empty as selected value.
I'm new in cakephp but i do this with array and parameter defaults :)
like that:
$options = array(
'x' => 'Select',
1 => 'Something',
2 => 'Car',
3 => 'Train'
);
echo $this->Form->input('name', array(
'options' => $options,
'default' => 'x'
));
Maybe that will be working fine :)
Variable $options is just your options after that you can validate if name of that field has value integer or 'x'. If 'x' then deny rest of logical code
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]);
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 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.