It's my form class:
<?php
class Application_Form_Message extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->addElement(
'text',
'email',
array(
'label' => 'E-mail',
'filters' => array('StringTrim'),
'class' => 'size-large-big',
'required' => true
)
);
$this->addElement(
'text',
'subject',
array(
'label' => 'Subject',
'filters' => array('StringTrim'),
'class' => 'size-large-big',
'required' => true
)
);
$this->addElement(
'textarea',
'content',
array(
'label' => 'Content',
'filters' => array('StringTrim'),
'class' => 'size-large-big',
'required' => true
)
);
$this->addElement(
'file',
'file',
array(
'label' => 'Attachments',
'filters' => array('StringTrim'),
'class' => 'size-large-big',
'isArray' => true,
'required' => false,
'allowEmpty' => true,
'style' => 'display: inline;'
)
);
$this->file->setDestination(realpath(APPLICATION_PATH . '/../public/uploads'))
->addValidator('Extension', false, 'png, jpg, jpeg', 'pdf', 'doc', 'docx');
$this->file->getValidator('Upload')->setMessages(array(
Zend_Validate_File_Upload::NO_FILE => 'Empty filename',
));
$this->addElement(
'submit',
'submit',
array(
'ignore' => true,
'label' => 'Send',
'class' => 'btn'
)
);
}
}
My view:
<div class="container" >
<section class="main">
<form action="<?php echo $this->form->getAction(); ?>" method="<?php echo $this->form->getMethod(); ?>">
<ul class="form-inputs login">
<h2>Message</h2>
<p><?php echo $this->page->content; ?></p>
<li>
<?php echo $this->form->email->renderLabel(); ?>
<?php echo $this->form->email->renderViewHelper(); ?>
</li>
<li>
<?php echo $this->form->subject->renderLabel(); ?>
<?php echo $this->form->subject->renderViewHelper(); ?>
</li>
<li>
<?php echo $this->form->content->renderLabel(); ?>
<?php echo $this->form->content->renderViewHelper(); ?>
</li>
<li>
<?php echo $this->form->file->renderLabel(); ?>
<?php echo $this->form->file->renderFile(); ?>
</li>
<li>
<?php echo $this->form->submit->renderViewHelper(); ?>
</li>
</ul>
</form>
</section>
</div>
The problem is that form isn't valid, even if I fill all inputs and add one file.
print_r($form->getMessages()); //error messages
print_r($form->getErrors()); //error codes
print_r($form->getErrorMessages()); //any custom error messages
Controller:
public function newAction()
{
$page = new Application_Model_DbTable_Page();
$form = new Application_Form_Message();
if($this->getRequest()->isPost())
{
if ($form->isValid($this->getRequest()->getPost()))
{
$data = $form->getValues();
die('its ok');
$this->_sendComplaint($data['subject'], $data['content'], $data['email']);
}
else
{
print_r($form->getMessages()); //error messages
print_r($form->getErrors()); //error codes
print_r($form->getErrorMessages()); //any custom error messages
}
}
else
$form->email->setValue($this->view->userDetails->email);
$this->view->page = $page->getPageBySlug('message');
$this->view->form = $form;
$this->view->pageName = 'Message';
}
return me nothing, just empty arrays: Array ( ) Array ( [email] => Array ( ) [subject] => Array ( ) [content] => Array ( ) [file] => Array ( ) [submit] => Array ( ) ) Array ( )
Thanks for help.
I found the problem myself, it was that the enctype="multipart/form-data" attribute was missing in the form tag.
Related
In my view I have
<?= $this->Form->create('Asc201516', array(
'url' => array(
'controller' => 'asc201516s',
'action' => 'submit_asc201516'
),
'class' => 'form-inline',
'onsubmit' => 'return check_minteacher()'
)); ?>
<div class="form-group col-md-3">
<?= $this->Form->input('bi_school_na', array(
'type' => 'text',
'onkeypress' => 'return isNumberKey(event)',
'label' => 'NA',
'placeholder' => 'NA',
'class' => 'form-control'
)); ?>
</div>
<?php
$options = array(
'label' => 'Submit',
'class' => 'btn btn-primary');
echo $this->Form->end($options);
?>
In my Controller, I have
$this->Asc201516->set($this->request->data['Asc201516']);
if ($this->Asc201516->validates()) {
echo 'it validated logic';
exit();
} else {
$this->redirect(
array(
'controller' => 'asc201516s',
'action' => 'add', $semisid
)
);
}
In my Model, I have
public $validate = array(
'bi_school_na' => array(
'Numeric' => array(
'rule' => 'Numeric',
'required' => true,
'message' => 'numbers only',
'allowEmpty' => false
)
)
);
When I submit the form, logically it should not get submitted and print out the error message but the form gets submitted instead and validates the model inside controller which breaks the operation in controller.
You have to check validation in your controller like
$this->Asc201516->set($this->request->data);
if($this->Asc201516->validates()){
$this->Asc201516->save($this->request->data);
}else{
$this->set("semisid",$semisid);
$this->render("Asc201516s/add");
}
You will have your ID there in variable $semisid, or you can set data in $this->request->data = $this->Asc201516->findById($semisid);
Created View as
<?php $options = array("Eng_yes" => "English", "teg_yes" => "Telugu", "hin_yes" => "Hindi"); ?>
<?php echo $this->Form->input('read', array(
'label' => __('Read', false),
'type' => 'select',
'multiple' => 'checkbox',
'options' => $options,
)); ?>
<?php $options = array("Eng_yes" => "English", "teg_yes" => "Telugu", "hin_yes" => "Hindi"); ?>
<?php echo $this->Form->input('write', array(
'label' => __('Write', false),
'type' => 'select',
'multiple' => 'checkbox',
'options' => $options,
)); ?>
<?php $options = array("Eng_yes" => "English", "teg_yes" => "Telugu", "hin_yes" => "Hindi"); ?>
<?php echo $this->Form->input('understand', array(
'label' => __('Understand', false),
'type' => 'select',
'multiple' => 'checkbox',
'options' => $options,
)); ?>
Created Controller as
public function enquiry_form()
{
App::uses('CakeEmail', 'Network/Email');
$useremail = $this->data['Feedback']['email'];
$read = $this->data['Feedback']['read'];
$write = $this->data['Feedback']['write'];
$understand = $this->data['Feedback']['understand'];
$Email = new CakeEmail();
$Email->template('enquiry_form')
->subject($usertopic)
->to('xyz#xyz.com')
->from($useremail)
->viewVars(array('read' => $read, 'write' => $write, 'understand' => $understand))
->emailFormat('both')
->send();
$this->Session->setFlash(__('Your Feedback has been sent'), 'flash', array('alert' => 'success'));
$this->redirect($this->referer());
}
Email Template File
<?= $read ?>
<?= $write ?>
<?= $understand ?>
When I sent a mail using this code, In mail body getting
Notice (8): Array to string conversion [APP/View/Emails/html/enquiry_form.ctp, line 1]
Code Context
<p><?=$read ?></p>
$viewFile = '/code/app/View/Emails/html/enquiry_form.ctp'
$dataForView = array(
'useremail' => 'test#gmail.com',
'read' => array(
(int) 0 => 'teg_yes'
),
'write' => array(
(int) 0 => 'teg_yes'
),
'understand' => array(
(int) 0 => 'teg_yes'
)
)
How do I display check-boxes values in mail body.
You are trying to print array as string in your enquiry_form.ctp template file. Use like this.
Email Template File
<?= $read[0] ?>
<?= $write[0] ?>
<?= $understand[0] ?>
Hope this will solve your issue.
I am new at php and yii framework.can any one help me with my form.I have a update form which has 3 fields with drop down menu.how to make the from field value read only.it will be very much helpfull if any one provide me with code.here is my update form code:
Update form:
<div class="row">
<?php
$criteria=new CDbCriteria();
echo $form->dropDownListGroup(
$model,
'user_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(User::model()->findAll($criteria), 'id', 'user_id'),
'dataProvider'=>$model->searchByUserId(Yii::app()->user->getId()),
'htmlOptions' => array('prompt'=>'Select'),
)
)
); ?>
</div>
<div class="row" id="jobTitle">
<?php
$criteria = new CDbCriteria;
$criteria->condition = "status= 'active'";
echo $form->dropDownListGroup(
$model,
'job_title_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(JobTitle::model()->findAll($criteria), 'id', 'name'),
'htmlOptions' => array('prompt'=>'Select job title'),
)
)
); ?>
</div>
<div class="row" id="file_name">
<?php echo $form->textFieldGroup(
$model,'file_name',
array(
'wrapperHtmlOptions' => array(
'class'=> 'col-sm-5',
),
)
);
?>
</div>
<div class="row" id="statustype">
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
)
); ?>
</div>
To make a value readonly - add 'readonly'=>true to the options array of the field you want to make readonly.
For example:
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
'readonly' => true
)
);
?>
It's impossible to set select element readonly. With Yii you can set it disabled and add value into hidden field using unselectValue.
echo $form->dropDownListGroup(
// ...
array(
// ...
'widgetOptions' => array(
'htmlOptions' => array(
'disabled' => 'disabled',
'unselectValue' => $model->user_id,
),
)
)
);
Or use TbSelect2 it has readonly property.
I am trying use zf2 to validate form, but have errors:
An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\InputFilter\Exception\RuntimeException
File:
C:\xampp\htdocs\ZEND\Users\Users\vendor\ZF2\library\Zend\InputFilter\Factory.php:395
Message:
Invalid validator specification provided; was neither a validator instance nor an array specification
Code:
<?php
namespace Users\Form;
use Zend\InputFilter\InputFilter;
class RegisterFilter extends InputFilter{
public function __construct(){
$this->add(array(
'name' => 'email',
'required' =>true,
'validators' => array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
),
),
));
$this->add(array(
'name' => 'user',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringLength'),
),
'validators' => array(
array(
'options' => array(
'encoding' => 'UTF-8',
'min' => 6,
'max' => 32,
),
),
),
));
}
}
RegisterForm
<?php
namespace Users\Form;
use Zend\Form\Form;
class RegisterForm extends Form {
public function __construct($name = null){
parent::__construct('Register');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype', 'multipart/form-data');
$this->add(array(
'name' => 'user',
'attributes' => array(
'type' => 'text',
'required' => 'required'
),
'options' => array(
'label' => 'UserName',
),
));
$this->add(array(
'name' => 'pwd',
'attributes' => array(
'type' => 'password',
'required' => 'required'
),
'options' => array(
'label' => 'Password',
)
));
$this->add(array(
'name' => 'cpwd',
'attributes' => array(
'type' => 'password',
'required' => 'required'
),
'options' => array(
'label' => 'Comfirm Password',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Register',
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'email',
),
'options' => array(
'label' => 'Email',
),
'attributes' => array(
'required' => 'required',
),
'filters' => array(
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID_FORMAT => 'Email Address Format is invalid'
)
)
)
)
));
}
}
RegisterController
<?php
namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Form\RegisterForm;
use Users\Form\RegisterFilter;
class RegisterController extends AbstractActionController{
public function indexAction(){
$form = new RegisterForm();
$viewmodel = new ViewModel(array('form' => $form));
return $viewmodel;
}
public function processAction(){
if(!$this->request->isPost()){
return $this->redirect()-> toRoute(NULL,
array( 'controller' => 'register',
'action' => 'index',
));
}
$post = $this->request->getPost();
$form = new RegisterForm();
$inputFilter = new RegisterFilter();
$form->setInputFilter($inputFilter);
$form->setData($post);
if(!$form->isValid()){
$model = new ViewModel(array(
'error' => true,
'form' => $form,
));
$model ->setTemplate('users/register/index');
return $model;
}
return $this->redirect()->toRoute(NULL,array(
'controller' => 'register',
'action' => 'confirm',
));
}
public function confirmAction(){
$viewmodel = new ViewModel();
return $viewmodel;
}
}
index.phtml
<section class = 'register'>
<h2> Register </h2>
<?php if($this -> error): ?>
<p class='error'> have errors </p>
<?php endif ?>
<?php
$form = $this->form;
$form -> prepare();
$form -> setAttribute('action', $this->url(NULL,
array('controller' => 'Register', 'action' => 'process')));
$form -> setAttribute('method','post');
echo $this->form()->openTag($form);
?>
<dl class='zend_form'>
<dt><?php echo $this->formLabel($form -> get('user')) ;?> </dt>
<dd> <?php echo $this->formElement($form->get('user')) ;?>
<?php echo $this->formElementErrors($form->get('user')) ;?>
</dd>
<dt><?php echo $this->formLabel($form -> get('email'));?></dt>
<dd><?php echo $this->formElement($form-> get('email')) ;?>
<?php echo $this->formElementErrors($form -> get('email')) ;?>
</dd>
<dt><?php echo $this->formLabel($form -> get('pwd')) ;?></dt>
<dd><?php echo $this->formElement($form->get('pwd')) ;?>
<?php echo $this->formElementErrors($form->get('pwd')) ;?>
</dd>
<dt><?php echo $this->formLabel($form ->get('cpwd'));?></dt>
<dd><?php echo $this->formElement($form->get('cpwd'));?>
<?php echo $this->formElementErrors($form->get('cpwd'))?>
</dd>
<dd><?php echo $this->formElement($form->get('submit'));?>
<?php echo $this->formElementErrors($form->get('submit'));?>
</dd>
</dl>
<?php echo $this->form()->closeTag()?>
</section>
Your validator structure is wrong.
The validators key is expecting an array of validators. You are supplying the validator name and options directly.
Change this:
this->add(array(
'name' => 'email',
'required' =>true,
'validators' => array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
),
),
));
To:
this->add(array(
'name' => 'email',
'required' =>true,
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'domain' => true,
),
)
//Another validator here ..
),
));
My application has contacts controller with add action which able to post contacts message to the database table comments. It works fine with its validation.
Now I want to use the add view (The contacts form) as a global form to be rendered in all pages of the application.
I know, to do that, I have to make an element contains the form (or the add view) as follows:
elements/contact.ctp
<div class="panel">
<h4><?php echo __('contact'); ?></h4>
<?php echo $form->create('Contact',array('action'=>'index', 'class' => ''));?>
<?php echo $form->input('name', array('size' => 45, 'class' => 'input-text', 'label' => array( 'text' => __('name',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('email', array('size' => 45, 'class' => 'input-text', 'label' => array('text' => __('email',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('subject', array('type' => 'select', 'options' => array(null => __('choose subject',true), 'g' => __('general', true), 'r' => __('report', true)), 'class' => 'input-text', 'label' => array('text' => __('subject', true).'<sup>*</sup>'),'error' => array('class' => 'error', 'wrap' => 'small'))); ?>
<?php echo $form->input('content', array('class' => 'input-text', 'style' => 'height: 140px', 'title' => __('message', true), 'label' => array('text' => __('message',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php //echo $fck->load('Contact.message','Mini'); ?>
<span>
<?php
App::import('Vendor','FoxCaptcha', array('file' => 'Fox_captcha.php'));
$cap = new Fox_captcha(120,30,5);
$cap->image_dir = $html->url('/').'img/';
$cap->lines_amount = 13;
$cap->en_reload = $html->image('reload.png', array('alt' => __('reload', true), 'title' => __('reload the captcha', true), 'id' => 'frel', 'style' => 'vertical-align:middle'));
?>
</span>
<div>
<span class="display:inline"><?php echo $cap->make_it('HTML');?></span>
<?php echo $form->input('vcode', array('size' => 45, 'class' => 'small input-text', 'label' => array('text' => __('captcha', true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
</div>
<?php echo $form->submit(__('send',true), array('class' => 'nice medium radius white button'));?>
<?php echo $form->end();?>
<div class="alert-box warning"><?php echo __('fields label with * are required');?></div>
</div>
The problem is: When I use this form from any page (for example posts/view/22) it submits to contacts/add. I want after submit posts/view/22 is rendered with any validation message triggered.
There is no solution for this requirement without Ajax and JSON. It includes submitting the form with Ajax to the contacts controller index action with event handler component activated and then check isAjax then render JSON output retrieved by the post's view and then populates the results.