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
Related
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.
I want to Create login and sign up on same page using CakeDC plugin but the Form->create variable is giving me errror
<?php echo $this->Form->create($register); ?>
because it not defined, i tried to give url for the register funtion but its not working...
<?php echo $this->Form->create($register,['url' => ['action' => 'register']]); ?>
Any Guidence...??
Also guide me the right way to use login, signup using CakeDC plugin in different pages in pop -up....
There is no real difference between a popup and a regular form. Use a form like this one
<?= $this->Form->create() ?>
<?= $this->Form->input('username', ['required' => true]) ?>
<?= $this->Form->input('password', ['required' => true]) ?>
<?= $this->Form->button(__d('CakeDC/Users', 'Login'), ['onclick' => '...']); ?>
<?= $this->Form->end() ?>
And create an onclick javascript ajax post to your login action.
I need to implement a button created in a view. In other programming languages this is very easy: your button has an ID so you can make a reference to it in a controller to implement its action. But in PHP I see like there are some predefined buttons (ex, submitbutton) and I don't understand how can you link an action with a button.
If someone could help me it would be very nice!
First you create an Action in your controller and then in your view try this:
<?= Html::a('YourFormName', ['yourControllerName/yourActionName'], ['class' => 'btn btn-success']) ?>
In an ActiveForm
<?php $form = ActiveForm::begin(); ?>
<div class="form-group">
<?= Html::submitButton('Button caption', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
This is the code for submit button
<?= Html::submitButton('Button Name',['class'=>'btn btn-success'])?>
if you want to make button from link here is the code
<?= Html:a('Caption',['controller/action'],['class'=>'btn btn-success'])?>
if you want to pass some query string in link the her is the code
<?= Html::a('caption',['controller/action','id'=>$model->id],['class'=>'btn btn-success'])?>
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>