how to, hidden field value not showing up on request in zend framework? - 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.

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.

How to set value to phonenumber in register.php from the signup.php in Yii2?

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(); ?>

Zend Framework multiple form elements names

I have multiple Zend Framework(v1.12) forms in my application.
Main form:
<?php
class Application_Form_Main extends Zend_Form
{
public function init()
{
$this->setMethod('post')->setAction('some/url');
}
}
?>
My subform:
<?php
class Application_Form_User extends Zend_Form_SubForm
{
public function init()
{
//first name element
$this->addElement('text',
'first_name',
array(
'label' => 'Name',
'required' => true,
'filters' => array('StringTrim')
)
);
//last name element
$this->addElement('text',
'last_name',
array(
'label' => 'Surname',
'required' => true,
'filters' => array('StringTrim')
)
);
$this->setElementDecorators(array(
'ViewHelper',
'Errors'
));
}
}
?>
In my custom controller (for example UsersController.php) Im rendering main form with multiple user subforms:
<?php
$mainForm = new Application_Form_Main();
for($i=0; $i<2; $i++){
$userForm = new Application_Form_User();
$mainForm->addSubForm($userForm, 'user_'.($i+1));
}
//passing main form to the template
$this->view->mainForm = $mainForm;
?>
So Im getting the form with 2 users first_name and last_name fields.
In my template Im rendering form this way:
<form action="<?php echo $this->mainForm->getAction(); ?>"
enctype="<?php echo $this->form->getEnctype(); ?>"
method="<?php echo $this->form->getMethod(); ?>"
">
<?php echo $this->mainForm->getSubForm('user_1')->first_name; ?>
<?php echo $this->mainForm->getSubForm('user_1')->last_name; ?>
<?php echo $this->echo $this->mainForm->getSubForm('user_2')->first_name; ?>
<?php echo $this->echo $this->mainForm->getSubForm('user_2')->last_name; ?>
</form>
The problem is first_name and last_name text field names are identical in both forms. How can I make it to have unique names? If I output the form:
<?php echo $this->mainForm; ?>
Then everything is ok, I get different field names.
So any ideas?

ZF2 How to set search form in layout

What is the right way to set a (search) form with Zend Framework 2 in the layout.ptml who is visible on any page of the website?
Thanks in advance.
Nick
It is really simple to set any variables to all layouts in ZF2 by EventManager, just attach the EVENT_RENDER event such as:
class Module
{
public function onBootstrap($e)
{
$app = $e->getParam('application');
$app->getEventManager()->attach(MvcEvent::EVENT_RENDER, array($this, 'setFormToView'), 100);
}
public function setFormToView($event)
{
$form = new MyForm();
$viewModel = $event->getViewModel();
$viewModel->setVariables(array(
'form' => $form,
));
}
}
For view in layout use:
<?php if ($user = $this->identity()): ?>
<?php echo 'Login with user' . $this->escapeHtml($user->nome); ?>
| <?php echo $this->translate('Sair'); ?>
<?php else: ?>
<?php
echo $this->form()->openTag($form);
echo "<h5>Forneça seu login e senha </h5>";
echo $this->formRow($form->get('username'));
echo $this->formRow($form->get('password'));
echo $this->formRow($form->get('rememberme'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
<?php endif; ?>