zend_form isValid always false - zend-framework

I write code for a site that uses Zend 1.
I want to rewrite the old admin interface in zend, but my form fails to validate.
I'll post the form, the validation code and the debug output here.
Form:
class Form_Admin_Address_Neu extends Zend_Form {
public function init() {
$this->setMethod('post');
$this->addElement('text', '_street', array(
'label' => 'Strasse:',
'size' => 60,
'required' => true,
'filters' => array('StringTrim'),
));
$this->addElement('text', '_zip', array(
'label' => 'PLZ:',
'size' => 6,
'required' => true,
'filters' => array('StringTrim'),
));
$this->addElement('text', '_city', array(
'label' => 'Stadt:',
'required' => true,
'size' => 30,
'filters' => array('StringTrim'),
));
$this->addElement('text', '_lat', array(
'label' => 'Latitude:',
'required' => true,
'size' => 30,
'validators' => array('Float'),
'filters' => array('Stringtrim'),
));
$this->addElement('text', '_lng', array(
'label' => 'Longitude:',
'required' => true,
'size' => 30,
'validators' => array('Float'),
'filters' => array('Stringtrim'),
));
$this->addElement('checkbox', '_hidden', array(
'label' => 'Hidden:',
'size' => 1,
'filters' => array('Int', 'Null'),
));
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Senden',
));
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
$this->setAction('/admin/address/list');
}
}
additional fields (i just post 1 here, 2nd is like it):
class Form_Admin_Elements_CountrySelect extends Zend_Form_Element_Select {
public function init() {
$countrymapper = new Mapper_Country();
$this->addMultiOption(0, 'Please select...');
foreach ($countrymapper->fetchAll() as $country) {
$this->addMultiOption($country->getId(), $country->getName());
}
$this->setLabel("Land:");
}
}
Code:
$addForm = new Form_Admin_Address_Neu();
$regionselect = new Form_Admin_Elements_RegionSelect('region_id');
$regionselect->setRequired(true);
$addForm->addElement($regionselect);
$countryselect = new Form_Admin_Elements_CountrySelect('country_id');
$countryselect->setRequired(true);
$addForm->addElement($countryselect);
if ($addForm->isValid($_POST)) {
...
} else {
print_r($_POST);
print_r($addForm->getErrorMessages());
print_r($addForm->getCustomMessages());
print_r($addForm->getErrors());
}
output:
Array
(
[_street] => sdvdsvsv
[_zip] => 111111
[_city] => sdfgsf
[_lat] => 1.0
[_lng] => 2.1
[_hidden] => 0
[country_id] => 1
[region_id] => 3
[submit] => Senden
[csrf] => d18dfed9d26e28d7a52aa4983b00667e
)
Array
(
)
Array
(
)
Array
(
[_street] => Array
(
)
[_zip] => Array
(
)
[_city] => Array
(
)
[_lat] => Array
(
[0] => notFloat
)
[_lng] => Array
(
[0] => notFloat
)
[_hidden] => Array
(
)
[submit] => Array
(
)
[csrf] => Array
(
)
[region_id] => Array
(
)
[country_id] => Array
(
)
)
As I see it, the validation fails, but i dont know why.
The values are present in the $_POST, but the form doesnt validate.
i even tried with isValidPartial(), but same result.
I think i'm doing something fundamentally wrong.
A hint would be great.
ty in advance

Try to enter a comma instead of a point in Latitude and Longitude.
1,0 and 2,1 instead of 1.0 and 2.1
I think it's a problem about Locale of your validator

Related

How to build a single 'flag' checkbox in prestashop with form helpers?

I can't figure out from official docs how to build a single checkbox element from the standard helpers. I already have the relevant boolean entity in database and I can build radios or selects as well for it, and they work.
But what I'd really like is to have a single checkbox to use as a boolean flag.
Anyone knows how?
Ok, the answer is to just use the 'switch' type: that will build a 'slider' switch on backoffice page. For future reference, I'm gonna report 3 different ways to accomplish the same task: radio, select and switch.
They have all been tested on AdminAddressesController and are bound to a custom DB boolean field called 'expo'.
//SELECT
$s_options = array(
array( 'expo' => 1, 'name' => 'Yes' ),
array( 'expo' => 0, 'name' => 'No' )
);
$temp_fields[] = array(
'type' => 'select',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'options' => array(
'query' => $s_options,
'id' => 'expo',
'name' => 'name'
)
);
//RADIO
$s_options = array(
array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
);
$temp_fields[] = array(
'type' => 'radio',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => $s_options
);
//SWITCH
$s_options = array(
array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
);
$temp_fields[] = array(
'type' => 'switch',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'is_bool' => true,
'values' => $s_options
);

cakephp form validation password comparision

I have a function to reset a password.
At the point where I have to compare the password and the password_confirm field, I get a strange behavior:
My form
echo $this->Form->hidden('tkn', array('value' => $tkn));
echo $this->Form->hidden('uid', array('value' => $uid));
echo $this->Form->input('password',array('type' => 'password', 'name' => 'data[Appuser][password]'));
echo $this->Form->input('password_confirm',array('type' => 'password', 'name' => 'data[Appuser][password_confirm]'));
My model validation:
var $validate = array(
'password' => array(
'rule' => array('minLength', '6'),
'message' => '{password} minLength 6!'
),
'password_confirm' => array(
'rule' => array('equaltofield','password'),
'message' => '{password_confirm} not equal!'
),
);
function equaltofield($val1, $val2){
return $this->data[$this->alias][key($val1)] == $this->data[$this->alias][$val2];
}
My Controller:
if($this->Appuser->save($this->data)){
$this->Session->setFlash(__('Password has been updated'));
}else{
debug($this->Appuser->invalidFields());
}
Now:
When I submit an empty form I get the following returned from the invalidFields()
array(
'password' => '*****',
'password_confirm' => array(
(int) 0 => '{password_confirm} not equal!',
(int) 1 => '{password_confirm} not equal!'
)
)
Question 1: Why do I not get the message, that the password has not its minlength?
Question 2: Why do I get the second message twice for comparing the password?
When typing in 2 different password with min length, I get this again:
array(
'password_confirm' => array(
(int) 0 => '{password_confirm} not equal!',
(int) 1 => '{password_confirm} not equal!'
)
)
When debug($this->data) I also get this (if that helps somehow)
array(
'Api' => array(
'tkn' => '6837d241bf1076c3c55a95abbcfafa04dc19a33c',
'uid' => '1'
),
'Appuser' => array(
'password' => '*****',
'password_confirm' => 'asdfgh'
)
)
Any ideas regarding my two questions above?
Thanks in advance!!
Try this
var $validate = array(
'password' => array(
'rule' => array('minLength', '6'),
'message' => '{password} minLength 6!'
),
'password_confirm' => array(
'rule' => 'equaltofield',
'message' => '{password_confirm} not equal!'
),
);
function equaltofield($data){
return $this->data[$this->alias]['password'] == $this->data[$this->alias]['password_confirm'];
}

How to add ErrorDescription to Zend Sub Form Element?

I have the following code that makes a sub form from a table, I have the error description saved in the database, I know how to set it for normal elements but not when it is a subform, how can I add a custom error message per subform element?
$subForm = new Zend_Form_SubForm();
foreach($configuration as $config){
$elements[] = array(
new Zend_Form_Element_Text($config->configuration_key, array(
'required' => (($config->is_required == 1) ? true : false),
'label' => $config->configuration_title,
'filters' => array('StringTrim'),
'value' => $config->configuration_value,
'Options' => array('style'=>'width:90%;'),
'Description' => $config->configuration_description,
'errorMessage' => $config->errorMessage,
))
);
}
$subForm->addElements($elements);
$this->addSubForm($subForm, 'configuration');
After playing a lot with the different options and trial/error I found out that I needed to add 'ErrorMessages' as an array, I rewrote my snippet
$elementSettings = array(
'required' => (($config->is_required == 1) ? true : false),
'label' => $config->configuration_title,
'filters' => array('StringTrim'),
'value' => $config->configuration_value,
'Options' => array('style'=>'width:90%;'),
'Description' => $config->configuration_description,
'ErrorMessages' => array($config->errorMessage)
);
$elements[] = array(new Zend_Form_Element_Text($config->configuration_key, $elementSettings));

Zend_Dojo_Form passwordTextBox validator don't work

I am a newbie in zf, recently I m making a register form using zend_dojo_form and there are two passwordtextbox elements, which u know, one is for entering password and the other is to confirm the former one, then I using the validator 'token' but failed, here is part of my code.
$this->addElement('PasswordTextBox','password',array(
'label'=>'password:',
'required'=>true,
'trim'=>true,
'regExp'=>'^[a-z0-9]{6,18}$',
'invalidMessage'=>'password should be 6-18',
'Decorators' => array(
'DijitElement',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td','align'=>'left')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','align'=>'right'))
)
)
);
//$this->addElement($password1);
$this->addElement('PasswordTextBox','password2',array(
'label'=>'confirm password:',
'required'=>true,
'trim'=>true,
//'regExp'=>'^[a-z0-9]{6,18}$',
'validators'=>array(array('identical',false,array('token'=>'password'))),
'invalidMessage'=>'the password you enter not the same',
'Decorators' => array(
'DijitElement',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td','align'=>'left')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','align'=>'right'))
)
)
);
Here's a sample that should be functional:
class Application_Form_Register extends Zend_Dojo_Form {
public function init() {
$this
->setOptions(array('name' => 'registerForm'))
// username
->addElement('TextBox', 'first_name', array(
'filters' => array('StringTrim', 'StringToLower'),
'id' => 'name_register',
'required' => true,
'label' => 'Fornavn(e):',
))
->addElement('TextBox', 'last_name', array(
'filters' => array('StringTrim', 'StringToLower'),
'id' => 'surname_register',
'label' => 'Efternavn:',
))
->addElement('ValidationTextBox', 'email', array(
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'EmailAddress',
array('StringLength', false, array(3, 60)),
),
'regExp' => "^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$",
'id' => 'email_register',
'required' => true,
'label' => 'E-mail:',
))
//password
->addElement('PasswordTextBox', 'password', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'regExp' => "^([a-zA-Z0-9_\-!-=]){4,32}$",
'tooltipPosition' => 'above',
'invalidMessage' => 'Krav til password: mellem 4 og 32 i længde og kun karakterer som tal, bogstaver eller blandt [ _-!"#%&/()=] er tilladt',
'id' => 'password_register',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Password:',
))
->addElement('PasswordTextBox', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'id' => 'passwordconfirm_register1',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Gentag password:',
))
->addElement('PasswordTextBox', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'id' => 'passwordconfirm_register2',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Gentag password:',
))
->addElement('Hidden', 'action', array(
'id' => 'action_register',
'value' => 'register',
))
->addElement('Button', 'foo', array(
'id' => 'foo_register',
'onClick' => 'application.submitRegistration(dijit.byId(\'registerForm\'))',
'label' => 'Register'
)
);
$this->_decorate();
}
// renders nulled empty labels and adds classnames, related to element name
protected function _decorate() {
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form_contents')),
'DijitForm'
));
foreach ($this->getElements() as $el) {
$el->addDecorator('HtmlTag', array('tag' => 'div', 'class' => $el->getName() . '-wrap'));
if ($el->helper == 'Button')
continue;
else if (strlen(trim($el->getLabel())) == 0)
$el->addDecorator('Label', array('tag' => null));
else
$el->addDecorator('Label', array('tag' => 'div', 'class' => $el->getName() . '-label'));
}
}
}
Focusing on the two password fields - in terms of what makes clientside validation tick, lets filter out the Zend_Form_Element attributes:
->addElement('PasswordTextBox', 'password_2_', array(
// strictly serverside, only accepts alphanumerical pwds lengths 6 through 20
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
// widget id
'id' => 'passwordconfirm_register1',
// both client-/server-side - triggers 'emptyMessage' validation
'required' => true,
// runs with the form.validate() and events onblur, oninit, onkeypress
// if ! dijit.get("value").match(dijit.regExp) show 'invalidMessage'
// this example matches the serverside reqs
'regExp' => "^([a-zA-Z0-9_\-!-=]){6,32}$",
// shown with form.validate(), dijit.validate() and input.blur() if regExp is not a match against value
'invalidMessage' => 'Password musts: 6 to 32 chars and only numbers, letters or amongst [ _-!"#%&/()=]',
))
And a second pwd field, same attributes but with a different validator
->addElement('PasswordTextBox', 'password_2_', array(
// strictly serverside, only accepts alphanumerical pwds lengths 6 through 20
'validators' => array(
array('identical',false,array('token'=>'password'))
),
// widget id
'id' => 'passwordconfirm_register2',
'required' => true,
// in theory, we would define 'validator' in programmatic construct of
// the validationtextbox. but this will not work with the way Zend handles dojo
// 'validator' => 'function(value, constraints) { return "BAD PRACTICE" }'
));
// add javascript code to run onload and to extend the 'register2' properly
$this->getElement()
->getView()
->headScript()
->appendFile($baseUrl . '/js/Validator.js');
By now, only 'required' is validated clientside in the passwordconfirm_register2 widget. Lets fix in what markup factory does in regards to 'bad practice' in the above lines. Remove that line and add the javascript code instead, containing:
// file: baseUrl/js/Validator.js
dojo.addOnLoad(function() {
dijit.byId('passwordconfirm_register2').validate = function(value, constraints) {
if(dijit.byId('passwordconfirm_register1').get("value") != value) {
this.invalidMessage = "Passwords are not identical";
return false;
}
return true
});
});

how to set up a zend multiCheckbox form field with the checkboxes checked?

i have this form:
$this->addElement (
'multiCheckbox', 'servers2',
array (
'checkedValue' => '0',
'multiOptions' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
));
the problem is that the checkedValue doesn't work for this setup, it does for a simple checkbox. I've also tried 'checkedValues' => array('1','0'), singular or plural,
but no end in sight.
any ideas?
THanks
To mark certain checkboxes as checked, try this:
$multiCheckElement->setValue(array('11.com', '3.com'));
// or
$this->addElement (
'multiCheckbox', 'servers2',
array (
'value' => array('11.com', '3.com'), // select these 2 values
'multiOptions' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
)
);
See also Zend_Form_Element_MultiCheckbox
ZF2 will require you to use value_options;
$form->add(
array(
'name' => 'servers2',
'type' => \Zend\Form\Element\MultiCheckbox::class,
'attributes' => array(
'id' => 'servers2',
'class' => 'form-control',
),
'options' => array(
'label' => 'Servers 2',
'column-size' => 'sm-10',
'label_attributes' => array('class' => 'col-sm-2'),
'twb-layout' => 'horizontal',
'value_options' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
),
)
);
To specify the checked options, as seen at
use the 'selected' => true attribute:
$options = array(
array(
'value' => '0',
'label' => 'Apple',
'selected' => false,
'disabled' => false,
'attributes' => array(
'id' => 'apple_option',
'data-fruit' => 'apple',
),
'label_attributes' => array(
'id' => 'apple_label',
),
),
array(
'value' => '1',
'label' => 'Orange',
'selected' => true,
),
array(
'value' => '2',
'label' => 'Lemon',
),
);