hi
I'm trying to add a disabled option to a select box with the form helper I use this code to generate an extra empty field, but I want this field disabled.
echo $this->Form->input('User.usertype_id',array('type'=>'select', 'empty'=>'usertype');
this generates:
<div class="input select">
<label for="UserUsertypeId">Usertype</label>
<select name="data[User][usertype_id]" id="UserUsertypeId">
<option value="">usertype</option>
<option value="1">athlete</option>
<option value="2">trainer</option>
</select>
</div>
but I want this:
<div class="input select">
<label for="UserUsertypeId">Usertype</label>
<select name="data[User][usertype_id]" id="UserUsertypeId">
<option value="" disabled="disabled" selected="selected">usertype</option>
<option value="1">athlete</option>
<option value="2">trainer</option>
</select>
</div>
Is there any way to do this simply, or should I just use some js?
If you know the options in advance, you can build the $options array to use in the select menu. This should give you exactly what you want:
$options = array(
array(
'name' => 'usertype',
'value' => '',
'disabled' => TRUE,
'selected' => TRUE
),
'athlete',
'trainer'
);
echo $this->Form->input('User.usertype_id', array('type' => 'select', 'options' => $options));
Or possibly this might work, but I haven't tested it either:
echo $this->Form->input('User.usertype_id', array('type' => 'select', 'empty'=> array('text' => 'usertype', 'selected' => TRUE, 'disabled' => FALSE)));
I know this question was last updated in 2010, but I have the actual answer.
Look at the example from the CakePHP docs:
$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox',
'disabled' => array('Value 1')
));
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select
Try this one!!
$operations = [
'' => 'Select Operation',
'query' => 'Query',
'create' => 'Create',
'update' => 'Update',
'upsert' => 'Upsert',
'delete' => 'Delete'
];
echo $this->Form->input('operation[]',[
'type' => 'select',
'options' => $operations,
'class' => 'operation-class',
'id' => 'operation-id-1',
'required' => 'required',
'label' => false,
'disabled' => [''],
'value' => ''
]);
mhmm looks like it isn't possible to add some codeblocks to a comment
so, both of your options generated:
<select name="data[User][usertype_id]" id="UserUsertypeId">
<option value="text">usertype</option>
<option value="selected">1</option>
<option value="disabled"></option>
<option value="1">athlete</option>
<option value="2">trainer</option>
</select>
so this didn't work, but I did it this way:
echo $this->Form->input('User.usertype_id', array('type' => 'select', 'empty'=> array('text" disabled="disabled" selected="selected' => '')));
this generates an option with the value: (" disabled="disabled" selected="selected)
so it becomes:
...
<option value="" disabled="disabled" selected="selected"></option>
...
this is a temporary solution, until I find something better, suggestions are welcome!
I merge the solution of Weptunus and stevelove.
In the controller:
$examples = $this->Test->Examples->find('list');
$this->set('examples', $examples);
In the view
echo $this->Form->input('example_id', array('empty' => array(
'' => array(
'name' => __('Select an Example'),
'value' => '',
'disabled' => TRUE,
'selected' => TRUE
)
))
);
i found this success in cake 1.3
<?php echo $this->Form->input('example', array('type' => 'select','id' => 'id','options' => array('empty "disabled="disabled" selected="selected"' => 'name empty', 'val1' => 'text1', 'val2' => 'text2')); ?>
Related
I am trying to add below style..
<div class="rowElem noborder">
<label>Language:</label>
<div class="formRight noSearch">
<select name="select2" class="chzn-select">
<option value="opt1">Choose the Language</option>
<option value="opt2" selected="selected">Kannada</option>
<option value="opt3">Telugu</option>
<option value="opt4">Tamil</option>
</select>
</div>
<div class="fix"></div>
</div>
But in cakephp, I have this code
<?php echo $this->Form->input('language_id', array('class' => 'chzn-select' )); ?>
Please give me the solution..
If I understand what you are asking, this is what you need to do.
In your controller you will create your options array for the select box:
$this->set('languageOptions', array('opt1' => 'Choose Language', 'opt2' => 'Kannada', 'opt3' => 'Telugu', 'opt4' => 'Tamil'));
Then in the view, you create the form:
<div class="rowElem noborder">
<label for="language_id">Language:</label>
<?php echo $this->Form->input('language_id', array('class' => 'chzn-select', 'options' => $languageOptions, 'label' => false, 'div' => array('class' => 'formRight noSearch'))); ?>
<div class="fix"></div>
</div>
$langs = array('opt1' => 'Choose Language', 'opt2' => 'Kannada', 'opt3' => 'Telugu', 'opt4' => 'Tamil');
$this->set(compact('langs')); // if you set options from controller
Then in view try this:
$this->Form->input('language_id', array(
'type' => 'select',
'options' => $langs,
'selected' => 2 // suppose default select Kannada
)
);
Cake Php Select Option Code
Language:
Form->input('language_id', array('class' => 'chzn-select', 'options' => $languageOptions, 'label' => false, 'div' => array('class' => 'formRight noSearch'))); ?>
I'm having some issues trying to get checkbox inputs and labels to output the correct HTML using CakePHP and Twitter Bootstrap.
The Bootstrap specific output should be:
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Keep me logged in
</label>
</div>
</div>
However, using the inputDefaults mentioned here (http://stackoverflow.com/a/9496242/1247225), this Cake form input:
echo $form->input('auto_login', array(
'type' => 'checkbox',
'label' => __('Keep me logged in', true)));
Outputs this:
<div class="control-group">
<input type="hidden" name="data[User][auto_login]" id="UserAutoLogin_" value="0" />
<input type="checkbox" name="data[User][auto_login]" class="" value="1" id="UserAutoLogin" />
<div class="controls">
<label for="UserAutoLogin">Keep me logged in</label>
</div>
</div>
Any ideas how to adjust this individual input so it outputs the correct Bootstrap HTML, like above?
The best way to control the form output, or the format of the output, is by passing a 'format' argument. Try playing with this:
'format' => array('before', 'label', 'input', 'between', 'after', 'error')
I don't think it is document, but by reading the code, you can find it in there.
the prettiest way to do it is this:
<?php
echo '<div class="col-lg-2">';
echo $this->Form->input('Content.checkbox', array(
'div' => array(
'class' => 'input-group',
),
'label' => false,
'type' => 'checkbox',
'before' => '<span class="input-group-addon">',
'after' => '</span><span class="input-group-addon"><label for="ContentCheckbox">What does the fox say?</label></span>',
));
echo '</div>';
I using :
<?php
echo $this->Form->input('coupDeCoeur',
array('div' => false,
'label' => false,
'type' => 'checkbox',
'before' => '<label class="checkbox">',
'after' => '<i></i>coupDeCoeur</label>'
));
?>
You need to build the form widget manually for checkboxes instead of using FormHelper::input.
For example:
echo '<div class="control-group">';
echo $this->Form->label('Model.field', null, array('class' => 'control-label'));
echo '<div class="controls">';
echo $this->Form->checkbox('Model.field');
echo '</div>';
echo '</div>';
Try following code:
<div class="control-group">
<div class="controls">
<label class="checkbox">
<?php echo $this->Form->input('auto_login', array('type'=>'checkbox','label' => false, 'div' => false,'class'=>false,'after'=>__('Keep me logged in')));?>
</label>
</div>
</div>
If you're using security component, you may have problems if you create your inputs without FormHelper::input. If that's the case, you should try with this:
echo "
<div class='control-group'>
<div class='controls'>
<label class='checkbox'>";
echo $this->Form->input('Model.field', array('label' => false, 'after' => 'Model.field'))."
</label>
</div>
</div>";
Here is the ready-to-use solution:
App::uses('FormHelper', 'View/Helper');
class InputHelper extends FormHelper {
// var $helpers = array('Form', 'Html');
public function create($model, $options = array()) {
$options['class'] = (isset($options['class']) && $options['class']) ? $options['class'] : 'form-horizontal table';
$options['inputDefaults'] = (isset($options['inputDefaults']) && $options['inputDefaults']) ? $options['inputDefaults'] : array(
'div' => 'control-group',
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>'
);
return parent::create($model, $options);
}
public function input($fieldName, $options = array()) {
$this->setEntity($fieldName);
$options = $this->_parseOptions($options);
if ($options['type'] == 'checkbox') {
$options['format'] = array('before', 'label', 'between', 'input', 'after', 'error');
}
fdebug($options);
return parent::input($fieldName, $options);
}
}
If you want a "one line" answer i got it:
echo $this->Form->input('Model.Field', array('class'=>'form-inline', 'after'=>'</br>'));
In my case, I used AdminLTE template with Bootstrap 3 and this code was working for me:
<?php
echo $this->Form->input('Model.fieldname', array(
'label' => false,
'type' => 'checkbox',
'before' => '<label>',
'after' => 'Field name label here</label>',
));
?>
Good luck here!!!
Here's a simple solution that works for me:
The CSS (LESS):
input.form-control[type="checkbox"] {
width: auto;
height: auto;
margin-right: 5px;
display: inline;
}
Then use the Form helper:
echo $this->Form->input(
'field_name',
array(
'div' => 'form-group',
'class' => 'form-control',
'type' => 'checkbox',
'label' => array(
'text' => 'Your label',
'class' => 'label-checkbox'
),
'format' => array('input', 'label')
)
);
I have a registration page and within this registration page the user will be required to fill in some interest, the interests values is called from a model with the name interest.php, all values are pulling through and saves as expected.
The relationship to the user model is
var $hasAndBelongsToMany = array(
'Interest' => array(
'className' => 'Interest',
'joinTable' => 'users_interests',
'foreignKey' => 'user_id',
'associationForeignKey' => 'interest_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
Whenever I add a validation rule in interest.php the little required star appears next to the checkboxes but it is not validating at all
<label for="InterestInterestId">Interests</label>
<input type="hidden" value="" name="data[Interest][interest_id]">
<div class="checkbox">
<input id="InterestInterestId8" type="checkbox" value="8" name="data[Interest][interest_id][]">
<label for="InterestInterestId8">Interest 1</label>
</div>
<div class="checkbox">
<input id="InterestInterestId1" type="checkbox" value="1" name="data[Interest][interest_id][]">
<label for="InterestInterestId1">Interest2</label>
</div>
in my view I call the multiple checkboxes like this
echo $form->input('Interest.interest_id', array('label' => __l('Interests'), 'multiple' => 'checkbox'));
This is my validation rule in interest.php
$this->validate = array(
'interest_id' => array(
'rule' => array(
'equalTo',
'1'
) ,
'message' => __l('Please select some interests')
)
);
Am I doing something wrong here, or missing something, any help will be appreciated!!!!
Found the answer with the help of this link HABTM form validation in CakePHP provided by bfavaretto
The problem was this $this->User->Interest->set($this->data);
I forgot to add it
i'm trying to get a checkbox with his label
echo $this->Form->checkbox('straordinari', array('div'=>'true', 'label' => 'Straordinari'));
in browser i get
<input id="ReportStraordinari_" type="hidden" value="0" name="data[Report][straordinari]">
<input id="ReportStraordinari" type="checkbox" value="1" label="Straordinari" div="true" name="data[Report][straordinari]">
but there is no label
where is the problem?
You should get what you are looking for with the following:
echo $this->Form->input('straordinari', array('type' => 'checkbox'));
I using :
<?php
echo $this->Form->input('coupDeCoeur',
array('div' => false,
'label' => false,
'type' => 'checkbox',
'before' => '<label class="checkbox">',
'after' => '<i></i>coupDeCoeur</label>'
));
?>
I'm new in cakephp. What I try to accomplish is this output :
<p><label> </label><input class="adminbut rad2" type="submit" name="submit" value="Login" /></p>
And this is what I did in my view file
<?php echo $this->Form->end(array(
'div' => false,
'label' => 'Login',
'class' => 'adminbut rad2',
'name' => 'submit',
'value' => 'Login',
'before' => '<p>',
'after' => '</p>'
));?>
And what I got is :
<input class="adminbut rad2" name="submit" value="Login" type="submit" /></p>
And as you can see, my output is missing :
<label> </label>
Any solution?
Thanks :)
Try
$form->create();
$form->submit("Login",array( 'div' => false, 'class' => 'adminbut rad2', 'name' => 'submit', 'value' => 'Login', 'before' => '<p><label> </label>', 'after'
=> '</p>'
));
$form->end();
echo $form->input('submit', array(
'type'=>'submit',
'value'=>'Login',
'class'=>'adminbut rad2',
'div'=>array('tag'=>'p'),
'label'=>" "
));
Try This my friend
Cakephp 2.X
$this->Form->submit(__('Submit'), array('class'=>'adminbut rad2'));
Cakephp 1.x
$form->submit(__('Submit'), array('class'=>'adminbut rad2'));