How to set value to phonenumber in register.php from the signup.php in Yii2? - yii2-basic-app

This is my field in signup.php (view file)
<?php $form = ActiveForm::begin(['method' => 'post','id' => 'form-signup','action' => 'http://localhost/basic1/web/index.php?r=site/reg']);?>
<?= $form->field($model, 'PhoneNumber') ?>
How to set value in register.php (view file)
<?=$form->field($model, 'PhoneNumber',array('value'=>'<?php echo $_POST['PhoneNumber']';?>)) ?>
Is there is any code to include in controller?

Try this.. (after submitting signup page).
in register.php
if(isset($_POST['PhoneNumber']) && !empty($_POST['PhoneNumber'])) // for preventing error
{
$model->PhoneNumber = $_POST['PhoneNumber'];
}
<?= $form->field($model, 'PhoneNumber')->textInput(); ?>

Related

Is there a way to call modeless form in pages using cakephp3

as what I read online it will only be available for like this
http://localhost/xxxxx/contact then the form will display
but I want it to display in many pages like contact us, or about us page
when i call this pages I want the form appear in the content?
Template
index.ctp
<?= $this->Form->create($contact); ?>
<?= $this->Form->input('name'); ?>
<?= $this->Form->input('email'); ?>
<?= $this->Form->input('body'); ?>
<?= $this->Form->button('Submit'); ?>
<?= $this->Form->end(); ?>
ContactController.php
<?php
// In a controller
namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContactForm;
class ContactController extends AppController
{
public function index()
{
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->data)) {
$this->Flash->success('Your message has been sent; we\'ll get back to you soon!');
$this->request->data['name'] = null;
$this->request->data['email'] = null;
$this->request->data['body'] = null;
} else {
$this->Flash->error('There was a problem submitting your form.');
}
}
$this->set('contact', $contact);
}
}
?>
ContactForm.php
<?php
namespace App\Form;
use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;
use Cake\Mailer\Email;
class ContactForm extends Form
{
protected function _buildSchema(Schema $schema)
{
return $schema->addField('name', 'string')
->addField('email', ['type' => 'string'])
->addField('body', ['type' => 'text']);
}
protected function _buildValidator(Validator $validator)
{
return $validator->add('name', 'length', [
'rule' => ['minLength', 10],
'message' => 'Please enter your name'
])->add('email', 'format', [
'rule' => 'email',
'message' => 'Please enter a valid email address',
])->add('body', 'length', [
'rule' => ['minLength', 25],
'message' => 'Please enter your message text',
]);
}
protected function _execute(array $data)
{
// Send an email.
return true;
}
}
You can fixed it by moving the contact template form into the element so that it will be available in any pages.
inside element in the contact folder, form below must be present
<legend><?= __('Our Form') ?></legend>
<fieldset>
<?php
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('body');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end(); ?>
then in your pages
you can just call
<?php
echo $this->element('contact/index');
?>
assuming you created index.ctp inside contact folder in element
Hope it solved your problem.

cakephp3- unable to post form data to controller function

I have a contact us form and user enter data and submits it. This data needs to accessed in controller function so I can send a mail to admin informing about the user request recently received.
But when I press submit button, nothing happens. Form just reloads and the contactus form (view page) is shown to the users. I don't know why the data is not getting passed to the controller function.
I'mm new to CakePHP framework and I'm using CakePHP3 for development purposes.
Here is my form:
<?php echo $this->Form->create(null, ['url' => ['controller' => 'Pages', 'action' => 'contactus']]); ?>
<div class="col-md-6">
<?php echo $this->Form->input('fname', ['placeholder' => 'Your name.' , 'id' => 'fname', 'required' => 'required']); ?>
</div>
<div class="col-md-6">
<?php echo $this->Form->input('mail', ['placeholder' => 'Your email.' , 'id' => 'mail', 'required' => 'required']); ?>
</div>
<div class="col-md-6">
<?php echo $this->Form->input('subject', ['placeholder' => 'Write something.', 'id' => 'subject']); ?>
</div>
<div class="col-md-9">
<?php echo $this->Form->button(__('Submit')); ?>
</div>
<?php echo $this->Form->end(); ?>
And my controller function is:
public function contactus()
{
$pages ='';
if ($this->request->is('post'))
{
$pages = $this->request->data('Contact');
}
$this->set(compact('pages'));
$this->set('_serialize', ['pages']);
}
Can anyone tell me the mistakes I made?
I think your form is submitting but not through the post method. So I would like to say you that, please make the bellow changes before submitting the form.
$pages ='';
if ($this->request->is('post'))
{
echo "Request is post";die;
$pages = $this->request->data('Contact');
}else{
echo "request is not post";die;
}
$this->set(compact('pages'));
$this->set('_serialize', ['pages']);
Now check, which is printing in the display. Then I can help you further.
Remember: - fill the form, then after change the controller method, then press submit method.

Can't validate form CakePHP 3

I'm new on CakePHP and I can't validate a login form. I'm getting the following error: Notice (8): Undefined variable: user [APP/Template\Users\login.ctp, line 5]
I already tried to use this code: <?= $this->Form->create('User'); ?> The error is removed but the validation doesn't works.
Can someone help me?
login.ctp:
<br>
<div class="index large-4 medium-5 large-offset-4 medium-offset-4 columns">
<div class="panel">
<h2 class="text-center">Login</h2>
<?= $this->Form->create($user); ?>
<?php
echo $this->Form->input('email');
echo $this->Form->input('password');
?>
<?= $this->Form->submit('Login', array('class' => 'button')); ?>
<?= $this->Form->end(); ?>
</div>
</div>
login function - UsersController.php:
// Login
public function login()
{
if($this->request->is('post'))
{
$user = $this->Auth->identify();
if($user)
{
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'comentario']);
}
// Erro no Login
$this->Flash->error('Erro de autenticaĆ§Ć£o');
}
}
First of all, change this line
<?= $this->Form->create($user); ?>
to this
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
Then, you can simplify your submit like this
<?= $this->Form->button(__('Login')); ?>
Make sure you create UsersTable.php in you src/Model/Table and put this code
// src/Model/Table/UsersTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table
{
public function validationDefault(Validator $validator)
{
return $validator
->notEmpty('username', 'A username is required')
->notEmpty('password', 'A password is required')
}
}
It's not good using redirect the specific controller inside login method. Change it:
return $this->redirect($this->Auth->redirectUrl());
Then tell your Auth Component where user should be redirect after login
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Articles',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'display',
'home'
]
]);
And the most important. Read Authentication and Authorization Tutorial

Yii2 saving form with multiple models

Hi I am very close to finish a project but got stuck saving a from with multiple models.
I have a grid that calls a controllers action that calls a form.
public function actionToday() {
$ID = $_GET["0"];
$modelCustomers = Customers::find()->where(['ID' => $ID])->one();;
$today = date("Y-m-d");
$beforeToday = 'DropinDate>'.$today;
$modelAttendance = Attendance::find()->where(['CustomersID' => $ID])->andwhere(['DropinDate' => $today])->one();
return $this->render('//attendance/_form-today-attendance', ['modelCustomers' => $modelCustomers, 'model' => $modelAttendance]);
}
In the form i have 3 main fields to update or in case is not record i need to create a new record.
This is the _form-today-attendance
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
?>
<?php $form = ActiveForm::begin(); ?>
<h3>Your Personal Details</h3>
<?= $form->field($modelCustomers, 'Name')->textInput(['readonly' => true]) ?>
<?= $form->field($model, 'DropinDate')->textInput(['readonly' => true]) ?>
<div class="attendance-form">
<?= $form->field($model, 'Dropin')->checkbox() ?>
<?= $form->field($model, 'Doctor')->checkbox() ?>
<?= $form->field($model, 'Lawyer')->checkbox() ?>
<?= $form->field($model, 'Observation')->textInput(['maxlength' => 250]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
When I debug i cant get anything happend in the Attendence model or the Customers model.
Any ideas?
Thanks a lot,
Eduardo
Try this function and check what you get.
public function actionToday()
{
$ID = $_GET["0"];
$modelCustomers = Customers::find()
->where(['ID' => $ID])
->one();;
$today = date("Y-m-d");
$beforeToday = 'DropinDate>' . $today;
$modelAttendance = Attendance::find()
->where(['CustomersID' => $ID])
->andwhere(['DropinDate' => $today])
->one();
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
//do something with $data
}
return $this->render('//attendance/_form-today-attendance', [
'modelCustomers' => $modelCustomers,
'model' => $modelAttendance]);
}
There will be something in array, you can assign it to model instances.

how to, hidden field value not showing up on request in zend framework?

i have a simple form that has a textarea ans a hidden field
$textarea = new Zend_Form_Element_Textarea('post');
$textarea->setRequired(true);
$textarea->setLabel('');
$hidden = new Zend_Form_Element_Hidden('post_id');
$hidden->setLabel('');
$hidden->setValue('1');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('test');
$this->addElement($textarea);
$this->addElement($hidden);
$this->addElement($submit);
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'div', 'class' => 'form_class')),
'Form'
));
in my view i do
<?php echo $this->form->getElement('post')->render(); ?>
<?php echo $this->form->getElement('submit')->render(); ?>
then in my controller
$request = $this->getRequest();
if( $request->isPost() && $form->isValid($request->getParams()))
{
Zend_Debug::dump($request->getParams());
}
what happens is that i get
array(8) {
["module"] => string(6) "testr"
["controller"] => string(8) "posts"
["action"] => string(9) "post"
["post"] => string(10) "testgfdgfg"
["submit"] => string(26) "submit"
}
but no post_id
this is a bit wired and i cant figure it out. Ive looked for any code that might screw this up but nothing. I've also tried to echo the hidden field in the view, but i still get nothing on the request
any ideas?
thanks
In your view do
<?php echo $this->form->getElement('post'); ?>
<?php echo $this->form->getElement('post_id'); ?>
<?php echo $this->form->getElement('submit');?>
You are simply not echoing post_id element like you did with post and submit . Also you don't need to call render() since php magic function __toString() proxy render() in all Zend_Form_Element_XXX .
In View part you are just set two elements only
<?php echo $this->form->getElement('post')->render(); ?>
<?php echo $this->form->getElement('submit')->render(); ?>
WHERE form->getElement('post_id')->render(); ?>
<?php echo $this->form->getElement('post')->render(); ?>
<?php echo $this->form->getElement('submit')->render(); ?>
<?php echo $this->form->getElement('post_id')->render(); ?>
Try once with this.
I think it will work.