Strange behaviour with Zend_Form_Element_Select - zend-framework

I'm having strange behaviours with my Zend Form. The fields to be filled with a "select" button remain empty (NULL) in the database, and the value of one them is put in another field.
Here's my code:
public function init()
{
$this->setName('projet');
$id = new Zend_Form_Element_Text('codeProjet'); //works great
$id->setLabel('Code du Projet');
$nomproj = new Zend_Form_Element_Text('nomProjet'); //is filled with the codeEntreprise value
$nomproj->setLabel('Nom du Projet')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
$deb = new Zend_Form_Element_Text('dateDebut'); //works great
$deb->setLabel('Date du début')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
$fin = new Zend_Form_Element_Text('dateFin'); //works great
$fin->setLabel('Date de fin')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
$montant = new Zend_Form_Element_Text('montantPrevu'); //works great
$montant->setLabel('Montant prévu')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
$chef = new Zend_Form_Element_Select('matriculeChef'); //remains empty
$options=array('select'=>'[select]');
$chef->setLabel('Chef de Projet')
->setRequired(true);
$boss = new Application_Model_DbTable_Developpeur();
foreach($boss->fetchAll() as $p)
$options[$p['matricule']]=$p['prenom'].' '.$p['nom'];
var_dump($options);
$chef->addMultioptions($options);
$chef->setValue($selected);
$boite = new Zend_Form_Element_Select('codeEntreprise'); //remains empty
$option=array('select'=>'[select]');
$boite->setLabel('Nom de l\'entreprise');
$entr = new Application_Model_DbTable_Entreprise();
foreach($entr->fetchAll() as $p)
$option[$p['codeEntreprise']]=$p['nom'];
var_dump($option);
$boite->addMultioptions($option);
$envoyer = new Zend_Form_Element_Submit('envoyer');
$envoyer->setAttrib('id', 'boutonenvoyer');
$this->addElements(array($id, $nomproj,$deb,$fin,$montant,$chef,$boite, $envoyer));
}
Do you have any idea why one field is filled with a wrong value, and why some remain empty in the database?
Thanks.

on the following line :
$chef->setValue($selected);
where is $selected coming from?

You should use $form->populate($dataFromDb or $_POSTdata) to fill the values IMO.

Related

How to Set value for password filed using Zend Form

This My Edit user form in Zend Frame work
$this->setAttrib('enctype', 'multipart/form-data');
$this->setName('user');
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$fname = new Zend_Form_Element_Text('fname');
$fname->setLabel('Full Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email Address')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$add = new Zend_Form_Element_Text('add');
$add->setLabel('Address')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$pass = new Zend_Form_Element_Password('pass');
$pass->setLabel('Password')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$city = new Zend_Form_Element_Text('city');
$city->setLabel('city')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$img = new Zend_Form_Element_File('img');
$img->setLabel('Profile picture')
->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
// add element in form
$this->addElements(array($id, $fname, $email, $add, $pass, $city, $img, $submit));
And I am Set the value for form elemets using following code in controller
$form = new Application_Form_User();
$form->submit->setLabel('Save');
$this->view->form = $form;
$id = $this->_getParam('id', 0);
if ($id > 0) {
$user = new Application_Model_DbTable_User();
$info = $user->getUser($id);
//$form->populate($info);
$form->setDefaults($info);
I am used $form->setDefaults($info); methode to set vlaue for form field. This method set value sucessfully for only Text input type but This method can not set value which has input type is password. so, what would i do for set vlaue for to input type is password?
This is my output screen edit.php
Zend does not allow password field to be auto-filled. Html type="password" also prevents the browsers to supports autocomplete. You will have to enter the password manually each time the page is refreshed.
However to disable autocomplete on a field use.
$foo = new Zend_Form_Element_Text('foo', array(
'autocomplete' => 'off',
));
// or...
$form->addElement('text', 'foo', array(
'autocomplete' => 'off',
));
// or...
$element->autocomplete = 'off';
I know this is an old question but I came across it whilst looking for the same answer so I thought I'd post my findings.
There is a setRenderPassword(true) property for password fields in Zend_Form.
$pass = new Zend_Form_Element_Password('pass');
$pass->setLabel('Password')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty')
->setRenderPassword(true);

Zend form for registration with salt

I have a simple registration form and the password hashed using md5 encryption and a salt, I am getting an error saying that the "uni" field can not be null and I am assuming this is due to the fact that I am unsure how to add the $salt into the form(would it not be a hidden element? Like the id field?). I have done the model below:
public function addUser($first_name, $surname, $email, $username, $password, $salt, $age, $gender, $uni) {
$salt=substr(md5(mt_rand()),0,20);
$md5Password=md5($salt.$password);
$data = array(
'first_name' => $first_name,
'surname' => $surname,
'email' => $email,
'username' => $username,
'salt' => $salt,
'password' => $md5Password,
'age' => $age,
'gender' => $gender,
'uni' => $uni,
);
$this->insert($data);
}
and here is the form:
<?php
class Application_Form_Register extends Zend_Form
{
public function init() {
$this->setName('register');
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$first_name = new Zend_Form_Element_Text('first_name');
$first_name->setLabel('Firstname:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$surname = new Zend_Form_Element_Text('surname');
$surname->setLabel('Surname:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$salt = new Zend_Form_Element_Hidden('salt');
$age = new Zend_Form_Element_Text('age');
$age->setLabel('Age:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$gender = new Zend_Form_Element_Text('gender');
$gender->setLabel('Gender:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$uni = new Zend_Form_Element_Text('uni');
$uni->setLabel('University:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id, $first_name, $surname, $email, $username, $password, $salt, $age, $gender, $uni, $submit));
}
}
Thanks
Rik
first 'uni' is a form field.
$uni = new Zend_Form_Element_Text('uni');
$uni->setLabel('University:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
this error occurs when you are passing a 'null' value to the db table column that has been declared 'not null'. To solve this, pass a 'not null' value to your 'addUser()' method, set a default value for 'uni' in your method api or allow the db table 'uni' column to accept 'null' values.
Personally I usually pass an array to these kinds on methods and then pull out the bits I want. Something like:
public function addStation(array $data) {
$data = array(
'station' => $data['station'],
'comment' => $data['comment']
);
$this->insert($data);
}
Again Personally I would not put the salt field in the form at all I would assign the salt in the model. No sense showing the world anything at all about my salt.
good luck.

Not getting the form data loaded into [log:Zend_View_Abstract:private]

I assigned
$this->view->form = $form (form instance) and now trying to display the content in layout.phtml using
echo $this->form;
but not displaying anything. I tried to check contents using
print_r($this);
and I see no data loaded into [log:Zend_View_Abstract:private].
Could anyone please help me on how to get data loaded in phtml file.
AuthForm.php
class forms_AuthForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('login');
$username = new Zend_Form_Element_Text('username','username');
$username->setLabel('Username:')
->setRequired(true)
->setOptions(array('class'=>'longfield'));
$password = new Zend_Form_Element_Text('password','password');
$password->setLabel('Password: *')
->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit','submit');
$submit->setLabel('Login');
$this->addElements(array($username, $password,$submit));
}
}
And AuthController.php
$form = new forms_AuthForm();
$this->view->form = $form;
Regrds
kiran
Iam not sure, but i think you have to assign the form to the layout not the view.
$layout = Zend_Layout::getMvcInstance();
$form = new forms_AuthForm();
$layout->form = $form;
and in your layout.phtml
echo $this->layout()->form;

How to preopulate zend file elements

I have a Zend form like this:
$this->setName('Add Job');
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$file = new Zend_Form_Element_File('file');
$file->setLabel('File')
->setRequired(true);
$category = new Zend_Form_Element_Checkbox('category');
$category->setLabel('Express?')
->setRequired(true)
->setCheckedValue('2')
->setUncheckedValue('1');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
The "add" action is working fine but I'm not working on the "edit" action using this in my controller:
$id = $this->_getParam('id', 0);
if ($id > 0) {
$jobs = new Application_Model_DbTable_Jobs();
$form->populate($jobs->getJob($id));
}
and the form prepopulates just fine except for the file element. In the DB, I've got the filename saved and I'd like to display it in the edit form somehow - is there a standard way of handling this in Zend?
Thanks,
Phil
You cannot prepopulate a file-input-element.
That is not a restriction by Zend_Form. The html input-file-element has no "value" attribute.
i used a trick, to pass the filename to the view, so one can for example show the image next to the upload. In your form type this:
$this->getElement('image')->setAttrib('class', $this->_myParams['data']['image']);
$image->setDecorators(array(
'File',
array('ViewScript', array('viewScript' => '/ditta/_image.phtml', 'placement' => false)))
);/
then in the viewscript use
$image = $this->element->getAttrib('class');//get name of file
$this->element->setAttrib('class', '');//delete class

Validate a set of fields / group of fields in Zend Form

Is there any good solution for the following requirements:
A form with one field for zip code and default validators like number, length, etc.
After submission, the form is checked against a database.
If the zip code is not unique we have to ask for an city.
Examples:
Case 1: Submited zip code is unique in database. Everything is okay. Process form
Case 2: Submited zip code is not unique. Add a second field for city to the form. Go back to form.
We want to handle this in an generic way (not inside an controller). We need this logic for
a lot of forms. First thought was to add it to isValid() to every form or write a
validator with logic to add fields to the form. Subforms are not possible for us, because we need this for different fields (e.g. name and street).
Currently I'm using isValid method inside my forms for an User Form to verify the password and confirm password field. Also, when the form is displayed in a New Action, there are no modifications, but when displayed in an Edit Action, a new field is added to the form.
I think that is a good option work on the isValid method and add the field when the validation return false, and if you want something more maintainable, you should write your own validatator for that purpose.
Take a look at my code:
class Admin_Form_User extends Zf_Form
{
public function __construct($options = NULL)
{
parent::__construct($options);
$this->setName('user');
$id = new Zend_Form_Element_Hidden('id');
$user = new Zend_Form_Element_Text('user');
$user->setLabel('User:')
->addFilter('stripTags')
->addFilter('StringTrim')
->setAllowEmpty(false)
->setRequired(true);
$passwordChange = new Zend_Form_Element_Radio('changePassword');
$passwordChange->setLabel('Would you like to change the password?')
->addMultiOptions(array(1 => 'Sim', 2 => 'Não'))
->setValue(2)
->setSeparator('');
$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password:')
->addFilter('stripTags')
->addFilter('StringTrim')
->setRequired(true);
$confirm_password = new Zend_Form_Element_Password('confirm_password');
$confirm_password->setLabel('Confirm the password:')
->addFilter('stripTags')
->addFilter('StringTrim')
->addValidator('Identical')
->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Save');
$this->addElements(array($id,$name,$lastname,$group,$user,$passwordChange,$password,$confirm_password,$submit));
$this->addDisplayGroup(array('password','confirm_password'),'passwordGroup');
$this->submit->setOrder(8);
$this->setDisplayGroupDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'div','id' => 'div-password'))
)
);
$passwordChange->clearDecorators();
}
public function addPasswordOption()
{
$this->changePassword->loadDefaultDecorators();
$this->getDisplayGroup('passwordGroup')
->addDecorators(array(
array('HtmlTag', array('tag' => 'div','id' => 'div-password'))
)
);
$this->password->setRequired(false);
$this->confirm_password->setRequired(false);
}
public function setPasswordRequired($flag = true)
{
$this->password->setRequired($flag);
$this->confirm_password->setRequired($flag);
}
public function isValid($data)
{
$confirm = $this->getElement('confirm_password');
$confirm->getValidator('Identical')->setToken($data['password']);
return parent::isValid($data);
}
}
So, in my controller:
public function newAction()
{
$this->view->title = "New user";
$this->view->headTitle($this->view->title, 'PREPEND');
$form = $this->getForm();
if($this->getRequest()->isPost())
{
$formData = $this->_request->getPost();
if($form->isValid($formData))
{
$Model = $this->getModel();
$id = $Model->insert($formData);
$this->_helper->flashMessenger('The user data has beed updated.');
$this->_helper->redirector('list');
}
}
$this->view->form = $form;
}
public function editAction()
{
$this->view->title = "Edit user";
$this->view->headTitle($this->view->title, 'PREPEND');
$id = $this->getRequest()->getParam('id');
$form = $this->getForm();
// Add yes or no password change option
$form->addPasswordOption();
$Model = $this->getModel();
if($this->getRequest()->isPost())
{
$formData = $this->getRequest()->getPost();
// Change password?
if($formData['changePassword'] == 2) $form->setPasswordRequired(false);
if($form->isValid($formData))
{
$Model->update($formData);
$this->_helper->flashMessenger('The user data has beed updated.');
$this->_helper->redirector('list');
}
}
$data = $Model->getById($id)->toArray();
$form->populate($data);
$this->view->form = $form;
}
You will probably need a Javascript form validator for that. In the submit function perform an AJAX call to check if the zipcode is unique. If not, show an extra city field.
But you still have to perform the validation server side: never trust user input, even if it's validated on the client side.