In my update form, I want to show the original value rather than it's id. Below is my _form.
<div class="mdc-meter-config-form">
<?php $form = ActiveForm::begin(['id'=>'con','options' => ['enctype' => 'multipart/form-data']]) ?>
<label class="control-label">Select Meters</label><br />
<input type="text" id="the-mter-id" class="form-control col-md-12" value="<?=$model->meter_id?>" />
<div style="clear: both;"></div>
<div id="selected_mters_container"></div>
<div style="clear: both;"></div>
<br/>
<?= $form->field($model, 'p_id')->dropDownList([''=>'Please Select']+\common\models\MdcProtocol::toArrayList()) ?>
<?= $form->field($model, 'time')
->dropDownList([''=>'Please Select','5' => '5', '10' => '10', '15' => '15','20'=>'20'])->label("Set Time (in seconds)") ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
In above view I want to change the 2 value to its actual one which is 4A60193390662
Dont know why you are not using the relation here for the meter to display the meter serial rather than the id.
Easiet way to avoid any further work as you want to just display it, 2 things you need to do
Assign the value to the input manually via relation.
Make the field disabled to exclude it out of the model collection array. (in case you use active form for the field).
I assume you have a relation to the meter in your current model with name getMeter() and the number you are trying to display 4A60193390662 is in field serial_number. You can change accordingly.
If you havent defined a relation than define one now
public function getMeter(){
return $this->hasOne(Meter::class,['id'=>'meter_id']);
}
Although you are using a custom html field you can still use the active form and mark the field disabled so that the activeform wont submit it with the model collection array and there wont be errors when saving the records like
meter_id should be an integer.
echo $form->field($model, 'meter_id')->textInput(['disabled' => 'disabled', 'value' => $model->meter->serial_number]);
try add an entry also for the field with value 4A60193390662 eg: $model->meter_name
<input type="text" id="the-mter-name" class="form-control col-md-12" value="<?=$model->meter_name?>" />
and if you don't want see the entry for meter_id .. you could use hidden input for this so mantain the related avlue for the model loading and popolation
And as suggested in comment by Michal Hyncica (sorry for improper charset ) you could wrap the input inside a check fo new record
<?php
if(!$model->isNewRecord) {
echo '<input type="text" id="the-mter-name" class="form-control col-md-12" value="' . $model->meter_name . '" />';
}
Related
How to fill a form automatically, but only when it is requested by the user.
That is, the fields are not populated when the form is generated.
Por example:
<?= $form->field($model, 'city')->textInput() ?>
When the form is accessed, the field is empty. And what I want is that there is a button, which when the user clicks, automatically the field is populated with the value of the "city" that is in the "User" table.
Form:
City : __________ | get my city |
After user clicked on button "get my city":
City : _London___ | get my city |
At last no ajax needed
<div class="row">
<div class="col-sm-12">
<?= Html::activeLabel($model, 'city') ?>
</div>
<div class="col-xs-9">
<?= $form->field($model, 'city')->textInput()->label(false) ?>
</div>
<div class="col-xs-3">
<?= \yii\helpers\Html::button('get my city', ['id' => 'get-user-city-btn']) ?>
</div>
</div>
<?php $this->registerJs("
$('#get-user-city-btn').on('click', function(){
$('#id-of-city-input-goes-here').val('"
. Yii::$app->user->identity->city
. "');
});
", $this::POS_END, 'get-user-city-script'); ?>
My backend login form submits with a single click of the login button. It creates an <input type="hidden" name="login-button"> element then shortly thereafter submits. On the frontend the same is created on click but never posts the data until I click the button again.
Here is a sample of the view code:
<div id="login-form" class="col-sm-12">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="col-sm-5 col-sm-offset-4 forgotPassword">
<?= $form->field($model, 'rememberMe')->checkbox() ?>
<div style="margin:auto;color:#fff;margin:1em 0;font-size:10px;">
if you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>
</div>
<div class="form-group">
<?= Html::submitButton('login', ['class' => 'btn btn-primary loginButton', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
The only difference I am seeing between the two is that there are some additional <div> tags that have been added for styling purposes. I have even removed those and I am still seeing the same issues.
Any ideas would be appreciated.
It looks like the id on the form div was causing the issue. Removing this has cleared everything up.
It was causing a conflict because it had the same id as the form.
I just want to add that usually you assign model "formName" as your form id
<?php $form = ActiveForm::begin(['id' => $model->formName()]); ?>
I have a form like this:
email
[input]
password
[input]
Login-Button
I want to eliminate the "email" and "password" text and put them inside the inputs like placeholder and also change the "login" text to "LOGIN".
This is my login ctp.
<fieldset style="background:#F44336">
<?= $this->Form->create() ?>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
<div class="forgot">
<p>Forgot Password?</p>
</div>
Your question is confusing and vague.
But I believe you are asking you dont want the value or words "Email" and "Password" to be next to the input but rather inside the input textbox?
If so, you can add HTML code inside the HTML form
<input type="email" name="email_form" placeholder="Email">
Keep in mind that is HTML 5.
In HTML 4, the traditional way would to be adding an attribute to the HTML form to email or password as value="Email..."
Above code is correct but you should use helpers.
So a better way to achieve the same task with Html helpers is:
<?= $this->Form->input('email', array('label' => false)); ?>
<?= $this->Form->input('password', array('label' => false)); ?>
It's a suggestion to make your code better which we all should focus on.
As we know,
<?= $form->field($model, 'name_field')->textInput() ?>
Adds a text field connected to 'name_field' in the model/table.
I want to add a field NOT in the model/table, and then run some JS when it loses focus to calculate the other fields.
How firstly do you add a free text field not connected to the model ?
Second, does anyone have any examples of adding JS/Jquery to the _form.php ?
The Html class contains the functions for generation of fields. In fact, your code above ends up calling Html::textInput(). To add a field
<?= Html::textInput("name", $value) ?>
To add javascript to a view just use registerJs():
$this->registerJs("alert('true');");
You can have the field rendered the same way as the ActiveField, with a label and classes. For example, let’s add a Cc field to a Mail form.
First display the To: field (in the model):
<?= $form->field($model, 'to')->textInput() ?>
Let’s add the Cc field (not in the model):
<?= Html::beginTag('div', ['class' => 'form-group field-mail-cc']) ?>
<?= Html::label('Cc:', 'mail-cc', ['class' => 'control-label']) ?>
<?= Html::textInput('Mail[cc]', '', ['id' => 'mail-cc', 'class' => 'form-control']) ?>
<?= Html::endTag('div') ?>
The class and id names mail-cc and field-mail-cc follow the ActiveForm naming pattern. The input name Mail[cc] adds your field to the ActiveForm group, so you can easily retrieve it with the usual
$form = Yii::$app->request->post('Mail');
When i am adding form to view & specifying parameters as
<?= $form->field($model, 'form_name', ['options' => ['id' => 'formName', 'name' => 'formName']])->textInput(); ?>
But, when i run in the browser & check for view page source, there it shows me
<input type="text" id="submitform-form_name" class="form-control" name="SubmitForm[form_name]">
this disturbs my javascript calling for field input. How to stop yii2 from generating its own id???
You are passing options to ActiveField. If you want override id and name attributes, pass them in textInput() options like so:
<?= $form->field($model, 'form_name')->textInput(['id' => 'formName', 'name' => 'formName']) ?>
Generated html output will be:
<input type="text" name="formName" class="form-control" id="formName">
Note that after that client validation for this attribute will stop working and that attribute won't be massively assigned.