So I'm displaying the logged user's information on a page, followed by a form which can be used to change basic user info, such as e-mail address, username and so on.
In the ProfileController file, I have the Index action, which handles the user info and, theoretically, the form too:
public function actionIndex() {
$user = User::find()->where(['id' => Yii::$app->user->identity->id])->one();
if ($user->load(Yii::$app->request->post())) {
$user->save();
//var_dump($user->first_name);die;
}
return $this->render('index', [
'user' => $user,
]);
}
In the index view file, I'm displaying the form as it follows:
<div class="row form">
<div class="user-form col-md-8 col-md-offset-2">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($user, 'last_name')->textInput(['maxLength' => true]) ?>
<?= $form->field($user, 'first_name')->textInput(['maxLength' => true]) ?>
<?= $form->field($user, 'email')->textInput(['maxLength' => true]) ?>
<?= $form->field($user, 'image')->textInput([]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
Problem is, whenever I press the submit button beneath the form, the controller receives the post request, but does not update the new values given in the form fields, thus displaying the old info at the var_dump(). What should I do?
Probably validation failed. Proper way of doing this is:
// ...
if ($user->load(Yii::$app->request->post())) {
if ($user->save()) {
// validated and saved, check $user state now
}
}
Method save() calls method validate() automatically. You can call it on your own as well if you want to operate on this model only when it's validated.
Related
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
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
I have a form which is opening in popup so I want to validate my form by ajax validation but when i click on submit button my page getting refreshed so I am not getting any validation error
View file:
<?php $form = ActiveForm::begin([
'id' => 'signup-form',
'enableAjaxValidation' => true,
//'action' => Url::toRoute('user/ajaxregistration'),
'validationUrl' => Url::toRoute('user/ajaxregistration')
]); ?>
<div class="col-md-12">
<div class="formbox">
<div class="inputbox signup">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon name"></i></span>
<?= Html::textInput('userFullName', '', ['placeholder' => "Name",'class'=>'form-control']); ?>
</div>
<?php ActiveForm::end(); ?>
Controller File:
public function actionValidate() {
$model = new SignupForm;
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model); \Yii::$app->end();
}
}
Model Code:
return [
['userFullName', 'trim'],
['userFullName', 'required'],
];
Please suggest me what should i do so that my page will not get refrsh and I will get the validation error
You are using an ActiveForm without any ActiveFields, this means that the validation rules that have been defined within the model aren’t even being assigned to the text input. I have written an implementation for your problem:
Model:
use Yii;
use yii\base\Model;
class ExampleForm extends Model
{
// this 'virtual attribute' defines a form field
public $userFullName;
// validation rules
public function rules()
{
return [
['userFullName', 'trim'],
['userFullName', 'required'],
];
}
}
Controller:
use models\ExampleForm;
use yii\web\Response;
use yii\widgets\ActiveForm;
public function actionExample()
{
$model = new ExampleForm;
// validate any AJAX requests fired off by the form
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
// code to process form data goes here.. This will execute on a form submission.
}
return $this->render('example-form', [
'model' => $model,
]);
}
View:
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
$this->title = 'Example';
?>
<div class="exampleForm">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the form.</p>
<!-- this form will submit via POST to the same action that renders it -->
<?php $form = ActiveForm::begin() ?>
<!-- this is an active field. Any validation rules for the 'userFullName' field -->
<!-- that have been defined within the $model object will be applied to this field -->
<!-- the validation has also been set to validate via ajax within the $options array -->
<!-- read more about ActiveFields here: http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html -->
<?= $form->field($model, 'userFullName', ['enableAjaxValidation' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Submit!', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
The best way to see whether ajax requests are being sent off / the form is being validated is to check chromes developer tools, go to the network tab and inspect the activity.
use renderAjax() Method:
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}else {
return $this->renderAjax('YourViewFileName', [
'model' => $model,
]);
}
My apologies for the vague title.
I'm having trouble coming up with a way to pass input from a textfield forward to an action I'm calling with the button on that view.
The code below is an ActiveForm I have on my view.
<div class="user-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'unique_key') ?>
<?= Html::a('Test', ['//order/create', 'unique_key' => 'entered key here'], ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</p>
What I want to accomplish is that the user fills the text field with a (randomly generated) key.
Then, when they click the button, their entered key should be passed through to the order/create action. Problem is that I think it is impossible to get the entered key at this stage as I think it isn't saved anywhere yet.
I know this probably is wrong in so many different ways but I'm still trying to learn.
Is there any way I can accomplish what I'am trying to do ?
Thanks in advance
You simply need a better form config and a button to submit your form :
<?php $form = ActiveForm::begin([
'action' => '//order/create',
'method' => 'get',
]); ?>
<?= $form->field($model, 'unique_key') ?>
<?= Html::submitButton('Test', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
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>