How can I add extra form data in yii ActiveForm? - php

I would like to know how can I add data to an ActiveForm like this:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
.
$data = ["username"=>"test"] as property "username"
I would like to send something like {name:name, email:email, ..., username:data}

you can use hidden input of \yii\helpers\Html class like follows:
<?= Html::hiddenInput('username', $value = $data['username']) ?>
or if you want it to be filled by users, you can use like this:
<?= Html::textInput('username') ?>

Related

Change value of attributes in a form field in Yii2

i have a simple question, that is how can i change the attributes values of a a field of an ActiveForm in Yii2
For example:
The following code :
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name'); ?>
<?= $form->field($model, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
generates this :
<input id="testform-email" class="form-control" name="TestForm[email]" aria-required="true" type="text">
I want to change the name and id attributes to a custom value.
Thanks.
you could assign directly the attribute you need this way
<?= $form->field($model, 'name')->textInput(['id' => 'your_id', 'name' => 'your_name']); ?>

CakePHP Email signup form in home page

I need my signup form with email input to work wherever I want on the site.
I have an EmailsController that has add() and will add an email when I am on the add page.
How do I get the layout for Emails/Add.ctp to be injected into my home page and wherever I like while keeping the functionality and giving feedback to a user?
<div>
<?= $this->Element('upone_scriptimports'); ?>
<?= $this->Form->create() ?>
<?php echo $this->Form->control('email'); ?>
<?= $this->Form->button(__('Submit'), ['class'=>'btn btn-default text-right right']) ?>
<?= $this->Form->end() ?>
</div>
You will use elements, see CookBook
Put your code in here: src/Template/Element/Emails/add.ctp, you use your element like this echo $this->element('Emails/add');
Also add an action attribute to your form, specifying the controller
and action that will process the data.
<?= $this->Form->create(null, [
'url' => ['controller' => 'Articles', 'action' => 'publish']
]); ?>
<?= $this->Form->control('email'); ?>
<?= $this->Form->button(__('Submit'), [
'class' => 'btn btn-default text-right right'
]); ?>
<?= $this->Form->end(); ?>
See Setting a URL for the Form

How to set a different label for tabular input - Yii2

I have followed this guide http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html to build a tabular input. But in my case, I want to add a different label for each input. How can I do that?
Update action:
public function actionUpdate()
{
$emailModel = EMAIL::find()->indexBy('ID')->all();
if (Model::loadMultiple($emailModel, Yii::$app->request->post()) && Model::validateMultiple($emailModel)) {
foreach ($emailModel as $email) {
$email->save(false);
}
return $this->redirect('update');
}
return $this->render('update', ['emailModel' => $emailModel]);
}
Update View
<?php $form = ActiveForm::begin(); ?>
<?php foreach ($emailModel as $index => $email) { ?>
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true])->label(false) ?>
<?php } ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
I'm on learning Yii2. Thank you.
Changing the label for multiple models is the same as for one model.
1) To retrieve it from attributeLabels() or generate automatically based on according database column name (in case it's ActiveRecord and there is no according entry in attributeLabels()) just omit ->label(false) call:
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true]) ?>
2) To apply custom label just for this form:
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true])->label('Your custom label') ?>
3) To have different label for each $model in a set just create helper function and call it in the loop:
function getCustomLabel($model, $index)
{
$number = $index + 1;
return "Content for model number $number";
}
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true])->label(getCustomLabel($model, $index)) ?>
Check the official docs:
yii\widgets\ActiveField label()
Creating forms (ActiveField)
Add label(label name) used as per Tabular Input
<?php $form = ActiveForm::begin(); ?>
<?php foreach ($emailModel as $index => $email) { ?>
<?= $form->field($email, "[$index]CONTENT")->textArea(['maxlength' => true])->label('Email '.$index+1) ?>
<?php } ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
Refer the Docs
Yii2 Active Field

Yii2 > How to store html output of a _form view into a string variable

I want to store the rendered html output of a form view which uses ActiveForm and Html Helper into a variable within my Controller.
I've tried storing result of a renderPartial directly to a variable, which did not work:
$htmlform = Yii::$app->controller->renderPartial('_form', ['model' => $model]);
I've also tried using output buffering to echo the output into a variable, but I could not store the output:
ob_start();
echo Yii::$app->controller->renderPartial('_form', ['model' => $model]);
$htmlform = ob_get_contents();
ob_end_clean();
View File: _form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $model frontend\models\Epic */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="epic-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'closed')->textInput() ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'organizationid')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
If anyone has an idea on a solution that would be great..
I have tried this way in a simple ControllerAction and work right ...in var_dump($test) there the aspected result
public function actionView($id)
{
$test = $this->renderPartial('_form', [
'model' => $this->findModel($id),
]);
var_dump($test);
}

How to create form in view from different model in yii2?

Iam new to yii. I am developing customer project app. I have a view wherein iam displaying the data from both the models, customer and projects.
How do i create a form to add new projects?
my project is here
To display project data in customer view, iam using
$query=Projects::find()
->where(['projects_clients_id'=> $model->customer_id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
]);
You can render several model and/or dataProvider in a view (properly constructed)
eg:
return $this->render('viewTestMulti', [
'modelOne' =>$modelOne,
'dataProviderTwo' => $providerTwo,
'dataProviderThree' => $providerThree,
'modeFour' => $modelFour,
]);
And then you can use a view with several gridView related to proper dataProvider and several forms everyone with a proper action
so when you press the specified submit you invoke the proper controller action
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $formOne = ActiveForm::begin();
$formOne->action= yii\helpers\Url::to('ControllerOne\create');
?>
<?= $formOne->field($modelOne, 'name') ?>
<?= $formOne->field($modelOne, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php $formFour = ActiveForm::begin();
$formFour->action= yii\helpers\Url::to('ControllerFour\create');
?>
<?= $formFour->field($modelFour, 'name_four') ?>
<?= $formFour->field($modelFour, 'email_four') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
I hope this could be useful
You have all the documentation about ActiveForms and I recommend, if you are new in Yii2 to use The Definitive Guide to Yii 2.0, here the Forms Section
This is a basic form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>

Categories