Add a disabled (and selected) option to select an item using the form helper

hi I'm trying to add a disabled option to the selection box using a form helper. I use this code to create an extra empty field, but I want this field to be 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 a way to make this simple, or should I just use some js?

+3
source share
5 answers

If you know the parameters in advance, you can build an array $optionsfor use in the selection 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));

Maybe this might work, but I have not tested it:

echo $this- > Form- > input ('User.usertype_id', array ('type' = > 'select', 'empty' = > array ('text' = > 'usertype', ' selected '= > TRUE,' disabled '= > FALSE)));

+9

, 2010 , . CakePHP:

$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

+4

mhmm , :

<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>

, :

echo $this->Form->input('User.usertype_id', array('type' => 'select', 'empty'=> array('text" disabled="disabled" selected="selected' => '')));

: ( "disabled =" disabled "selected =" selected) :

...
<option value="" disabled="disabled" selected="selected"></option>
...

, - , !

+1
source

I merge the solution of Weptunus and stevelove.

In the controller:

$examples = $this->Test->Examples->find('list');
$this->set('examples', $examples);

In view

echo $this->Form->input('example_id', array('empty' => array(
        '' => array(
            'name' => __('Select an Example'),
            'value' => '',
            'disabled' => TRUE,
            'selected' => TRUE
        )
    ))
);
0
source

Try it!

$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' => ''
]);
0
source

Source: https://habr.com/ru/post/1773086/


All Articles