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
});
});
Related
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
I need to translate the error messages from my form type. Here is my form Type code:
class ReferFriendType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$defaultSubject = "This is a default referral subject.";
$defaultMessage = "This is a default referral message.";
$builder->add('email1', 'email',array(
'required' => true,
'label' => 'Email 1* :',
'attr' => array('class' => 'large_text'),
));
$builder->add('email2', 'email',array(
'label' => 'Email 2 :',
'required' => false,
'attr' => array('class' => 'large_text'),
));
$builder->add('email3', 'email',array(
'label' => 'Email 3 :',
'required' => false,
'attr' => array('class' => 'large_text'),
));
$builder->add('email4', 'email',array(
'label' => 'Email 4 :',
'required' => false,
'attr' => array('class' => 'large_text'),
));
$builder->add('email5', 'email',array(
'label' => 'Email 5 :',
'required' => false,
'attr' => array('class' => 'large_text'),
));
$builder->add('subject', 'text', array(
'data' => $defaultSubject,
'required' => true,
'label' => 'Subject* :',
'attr' => array('class' => 'large_text'),
));
$builder->add('message', 'textarea', array(
'data' => $defaultMessage,
'required' => true,
'label' => 'Message* :',
'attr' => array('rows' => '5', 'cols' => '40'),
));
}
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection( array(
'fields' => array(
'email1' => array(
new Email(),
new NotBlank(array(
'message' => 'You must enter atleast one email address for a valid submission',
)),
),
'subject' => new NotBlank(),
'message' => new NotBlank(),
),
'allowExtraFields' => true,
'allowMissingFields' => true,
));
return array(
'validation_constraint' => $collectionConstraint,
'csrf_protection' => false,
);
}
public function getName()
{
return 'referFriend';
}
}
I want to translate 'You must enter atleast one email address for a valid submission' in getDefaultOptions() method into french. I have added the translation in the messages.fr.yml. But it is not getting translated. Any ideas how this can be done?
Validation translations go to the validators.LANG.yml files — not messages.LANG.yml ones.
The replacements are not set in the validation.yml file but by the Validator.
validators.en.yml
noFirstnameMinLimit: Please provide at least {{ limit }} characters
validation.yml
Acm\AddressBundle\Entity\Address:
properties:
firstname:
- Length:
min: 3
minMessage: "noFirstnameMinLimit"
This works for me with Symfony 2.4
There is an example in the docs.
It`s easy, see http://symfony.com/doc/current/book/translation.html#translating-constraint-messages
And set default_locale in /app/config/config.yml or play with $this->get('request')->setLocale('ru');
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',
),
);
I am using zend_form in my project. In a form i want to disable a text box. Here is the code:
$personal_information = new Zend_Form(array(
'method' => 'post',
'elements' => array(
'first_name' => array('text', array(
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
array('NotEmpty', true),
array(),
array('stringLength', false, array(1, 40))
),
'decorators' => $elementDecorators,
'label' => 'First name:'
)),
// THE "NEXT" BUTTON
'signup' => array('submit', array(
'decorators' => $buttonDecorators,
'label' => 'Next',
'required' => false,
'ignore' => true,
))
)
));
How to disable a text box in zend_form?
here is an example of disabled and readonly text field
$lati = new Zend_Form_Element_Text("lati" , array("readonly" => "readonly"));
$lati = new Zend_Form_Element_Text("lati" , array("disabled" => "disabled"));
i think this way is more clear than the way you add elements to the form ,
I want to create a form in Zend framework. I am using the code below for a field:
$this->addElement('text', 'username', array(
'label' => 'Username:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'alnum'
)
));
This works. But now I also want to add a new validator. In this case StrinLength
$element->addValidator('StringLength', false, array(6, 20));
How can I add this validator in the array I already have? Tnx in advanced
Doesn't this work:
<?PHP
$this->addElement('text', 'username', array(
'label' => 'Username:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'alnum',
array('StringLength', false, array(6,20))
)
));
Similar to the example given in the manual
You can specify the names of arguments to the addValidator() method as array keys:
$this->addElement('text', 'username', array(
'label' => 'Username:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'alnum',
// See below
array(
'validator' => 'StringLength',
'options' => array(6, 20)
)
)
));