Yii2 make an input field appear only in create - forms

I would like to make an input field appear only in the 'create' form and not in the 'update' form. How can I achieve this?

You can do that by following way
if ($model->isNewRecord){
echo $form->field($model, 'password')->passwordInput(['maxlength' => true, 'placeholder' => "Enter password"]);
}

You can use $model->isNewRecord in many creative ways that can help you do some stuff depending on create/update.
i.e.
Hide a field
if ($model->isNewRecord){
echo $form->field($model, 'title')->textInput()
}
Change a style, class or property like readonly
$isEditable = (model->isNewRecord);
$class = (model->isNewRecord)?'class-a':'class-b';
echo $form->field($model, 'title')->textInput([
'readonly'=>$isEditable,
'disabled' => $isEditable,
'class'=>$class])
Changing a label
Html::submitButton( ($model->isNewRecord)
?'Create'
:'Update',
['class' => 'btn btn-success','id' => 'submit-btn',]);
Show a different button
echo ($model->isNewRecord)
? Html::a('Cancel', ['index'], ['class' => 'btn btn-warning'])
: Html::a('Close', ['otherplace'], ['class' => 'btn btn-primary']);
You can play a lot with $model->isNewRecord and actually modify some stuff depending on the state.
And many other stuff.

Related

Hide label for input field

I am trying to hide the label for a specific field in _form.php without success.
I have tried couple of variation like, but none is working:
<?= $form->field($model, 'sample_text')->textArea('label'=>false) ?>
and alternate code:
<?= $form->field($model, 'sample_text')->textArea('label'=>'') ?>
What is the right approach to hide a label?
Ok, I found the solution.
<?= $form->field($model, 'sample_text')->textArea()->label(false) ?>
Or you can modify template value for particular field and remove {label} part from it. I.e.:
<p><?= $form->field($page, 'image', [
'template' => '<div class=\"\">{input}</div><div class=\"\">{error}</div>'
])->fileInput() ?></p>
At time of writing after digging into the core code, I have found this to be the best solution to hide the label and prevent rendering the full field template with errors etc. for hiddenInput.
<?=
$form->field($model, 'address_uuid', [
'template' => '{input}',
'options' => ['tag' => false]
])->hiddenInput([
'readonly' => true,
])->label(false)
?>
<?= $form->field($model, 'password', [
'inputOptions'=>[
'class'=>'form-control',
'placeholder'=>'Password'
]
])->passwordInput()->label(false); ?>
<?= $sffForm->field($sffModel, 'url_keywords', ['enableLabel' => false])->textInput(['placeholder' => 'URL / keywords']) ?>
You can disable label, while creating form field class
$form->field($model, 'email', [
'inputOptions' => [
'enableLabel' => false,
]
])
The best way to hide the label in form input field, is to pass empty value to array on 'attributeLabels()' function in the model.
i.e you have input filed name 'client_name', so on the 'attributeLabels()' function's return array pass the empty string as array value
public function attributeLabels()
{
return [
'id' => 'ID',
'gender' => 'Gender',
'client_name' => '',
.
.
.
]
}

Disable autocomplete in CakePHP form input box

I am creating form inputs with the CakePHP Form helper and some inputs (Most of the time 'username' and 'password') are being autocompleted on create actions, login actions, etc.. this is annoying. I am guessing those are just more common so the browser is using its cookies to try to complete the inputs.
Anyways.. how do I disable this?
In my view:
...
echo $this->Form->input('username', array(
'label' => 'Please enter your username',
'class' => 'pure-u-1-2'
));
echo $this->Form->input('password', array(
'label' => 'Please enter your password',
'class' => 'pure-u-1-2'
));
...
What am I missing?
You can specify attributes to be sent to the form helper. Specify the attribute 'autocomplete' and set its value to 'off'.
...
echo $this->Form->input('username', array(
'label' => 'Please enter your username',
'class' => 'pure-u-1-2',
'autocomplete' => 'off'
));
echo $this->Form->input('password', array(
'label' => 'Please enter your password',
'class' => 'pure-u-1-2',
'autocomplete' => 'off'
));
...
Which results in something like this for your HTML:
<input name="data[Model][username]" autocomplete="off" class="pure-u-1-2" id="ModelUsername" type="text">
You may also do this on the whole form instead of just each input. Just specify the same attribute and value in the form create like so:
...
echo $this->Form->create('Model', array(
'class' => 'class',
'autocomplete' => 'off'
));
This will give you something like this in your HTML:
<form action=".../Model/Action" class="class" autocomplete="off" id="ModelActionForm" method="post" accept-charset="utf-8">
NOTE Several browsers will now ignore autocomplete="off" or autocomplete="false". The workaround is to place a hidden text and password field before all other inputs on your form. The browsers will fill those instead of the ones you want to leave alone.
The best solution is to use autocomplete = new-password
It works great in Chrome and Firefox
Like this:
$this->Form->input('password', array('type' => 'password', 'autocomplete' => 'new-password'));

Individual class option for radio buttons

Is there an individual class option for radio buttons in Cake 2.x?
In my opinion the logical approach would be like:
<div class="input radio required">
<?php
$options = array(
array('1' => 'Kuchen', 'class' => 'cake'),
array('2' => 'Kekse', 'class' => 'biscuits'),
array('3' => 'Eis', 'class' => 'iceCream'),
);
$attributes = array(
'legend' => false,
'default' => '1'
);
echo $this->Form->radio('INCOMETYPE', $options, $attributes);
?>
</div>
But that doesn´t work. I hope you can help. Thanks :)
You could extend FormHelper to add implement this functionality if you intend to use it elsewhere. If it's a one off, you might be better to produce the markup by hand. Use the code produced by $this->Form->radio to give you a start.

how to add form id in cakephp when creating a form

i am new in cakephp so i dont know how can i do this ..
i want to add custom form id in my form but it is not adding the id ..it is using the default one adding the 'UserIndexForm' id..
how can i add this id
i want to do like this
<form method="post" action="#" id="form-login">
here cakephp code
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false,
'id' =>'form-login'//not working
)
));
?>
please help me if anyone know this
thankyou in advance
The inputDefaults option is only changing the input fields so you need to set the id on the root level of the array:
<?php
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
?>

Display boolean as radio button in CakePHP form

Does anyone know how to display two radio buttons in a form in CakePHP and have them save to a boolean field in the DB?
I can get my boolean value to display as a checkbox (default behaviour) but I need to display two radio buttons, however when I do this the changes don't get saved to the DB.
I feel like it's something very simple. Here's my code:
<h1>Edit Content</h1>
<?php
$thisVal = $this->data['LessonContent']['is_exercise'] ? "1" : "0";
$options = array(
'0' => 'Learning Material',
'1' => 'Exercise'
);
echo $this->Form->create('LessonContent', array('action'=>'edit'));
echo $this->Form->input('name', array('label' => 'Name'));
echo $this->Form->input('description', array('label' => 'Description'));
echo $this->Form->input('is_exercise', array(
'type' => 'radio',
'class' => 'radio',
'legend' => false,
'name' => 'Type',
'options' => $options,
'value' => $thisVal
));
echo $this->Form->input('id', array('type'=>'hidden'));
echo $this->Form->input('lesson_id', array('type'=>'hidden'));
echo $this->Form->end('Save Content');
echo $this->Html->link('Cancel', array('controller'=>'Lessons', 'action'=>'view', $this->data['Lesson']['id']));
?>
Thanks
You're overriding the name of the input, therefore the value for is_excercise will be sent as Type.
Remove the name option;
echo $this->Form->input('is_exercise', array(
'type' => 'radio',
'class' => 'radio',
'legend' => false,
'options' => $options,
'value' => $thisVal
));
note after making this change, you probably don't even have to manually set the value of the radio-button.
debugging
In situations like this, always check/debug the posted form-data, either via FireBug or by debugging it in CakePHP, by putting this in your controller;
debug($this->request);
Even better, install the CakePHP DebugKit Plugin this plugin shows all the information (request data, queries, session variables etc.) without having to add debug-lines