how to do hide and show elements in zend? - php

I had written the hide and show script for the two elements by checking checkbox in zend, but the value 1 will be sent to the function for both check and uncheck, So it only shows the element but it doesn't hides the element. how to resolve it ?
$this->addElement('checkbox', 'Change_Password',
array('decorators' => $this->elementDecoratorsTr ,'label' => 'Want to Set password ? :',
'required' => false,
'onchange' => 'passwordShow(this.value)',
'Options' => array(
'checkbx1' => 'checkbx1'),
));
$this->addElement('Password', 'User_Password',array(
'decorators' => $this->elementDecoratorsTr,
'label' => 'Password:',
'style' => 'display:none;',
'required' => false,
'filters' => array('StringTrim'),
'validators' => array(array(
'validator' => 'StringLength'))
));
$this->addElement('Password', 'Confirm_Password',array(
'decorators' => $this->elementDecoratorsTr,
'label' => 'Confirm Password:',
'style' => 'display:none;',
'required' => false,
'filters' => array('StringTrim'),
'validators' => array(array(
'validator' => 'StringLength'))
));
Here is my script function to hide and show the two elements User_Password and Confirm_Password
<script>
function passwordShow(password)
{
alert(password);
if(password)
{
$("#User_Password").show();
$("#Confirm_Password").show();
}
else
{
$("#User_Password").hide();
$("#Confirm_Password").hide();
}
}
</script>

Related

how to create an array element in form - zend framework 2

I want to create the following element :
<input type="file" name="file[]">
I have tried the following code in myproject/module/Member/src/Member/Form/EditForm.php :
$this->add(array(
'name' => 'file',
'type' => 'file',
'attributes' => array(
'class' => 'form-control col-md-7 col-xs-12',
'id' => 'file',
),'options' => array(
'multiple'=>TRUE
),
));
and
$this->add(array(
'name' => 'file[]',
'type' => 'file',
'attributes' => array(
'class' => 'form-control col-md-7 col-xs-12',
'id' => 'file',
),'options' => array(
'multiple'=>TRUE
),
));
but it is not working.
For file upload Zend Framework 2 has a special FileInput class.
It is important to use this class because it also does other important things like validation before filtering. There are also special filters like the File\RenameUpload that renames the upload for you.
Considering that $this is your InputFilter instance the code could look like this:
$this->add(array(
'name' => 'file',
'required' => true,
'allow_empty' => false,
'filters' => array(
array(
'name' => 'File\RenameUpload',
'options' => array(
'target' => 'upload',
'randomize' => true,
'overwrite' => true
)
)
),
'validators' => array(
array(
'name' => 'FileSize',
'options' => array(
'max' => 10 * 1024 * 1024 // 10MB
)
)
),
// IMPORTANT: this will make sure you get the `FileInput` class
'type' => 'Zend\InputFilter\FileInput'
);
To attach file element to a form:
// File Input
$file = new Element\File('file');
$file->setLabel('My file upload')
->setAttribute('id', 'file');
$this->add($file);
Check the documentation for more information on file upload.
Or check the documentation here on how to make an upload form

Populate values in Zend Form multi-select option

So my form is
class Form_Group_ShareGroup extends Form_Abstract
{
public function init()
{
$this->setAttrib('class', 'shareForm');
//Row 1
$this->addElement('select', 'groups', array(
'decorators' => $this->getElementDecorators(),
'label' => Zend_Registry::get('Zend_Translate')->translate('Groups_Colon'),
'style' => 'width:320px;',
'multiple' => true,
'data-help' => Zend_Registry::get('Zend_Translate')->translate('Please_Select_Groups_Share')
));
$this->addElement('select', 'users', array(
'decorators' => $this->getElementDecorators(),
'label' => Zend_Registry::get('Zend_Translate')->translate('Users_Colon'),
'style' => 'width:320px;',
'multiple' => true,
'data-help' => Zend_Registry::get('Zend_Translate')->translate('Select_Users_To_Share_Group_With')
));
//Row 2
$this->addElement('button', 'share', array(
'decorators' => $this->buttonDecorators,
'required' => false,
'ignore' => true,
'type' => 'submit',
'label' => 'Share',
'class' => 'btn primary'
));
}
}
and I want to populate some user data into "users" element.
example could be :
array('10000105' => 'aamaddy1 m aamaddy1','10000106' => 'aamaddy2 m aamaddy2' )
How can I achieve this ?
I have use populate method but it didnt worked out.
to view the form I am using :
$usersGroup = new Form_Group_ShareGroup();
$this->view->usersGroup = $usersGroup;
Please help !
Thanks for reading .
Try this:
//$data = array('10000105' => 'aamaddy1 m aamaddy1','10000106' => 'aamaddy2 m aamaddy2' );
$usersGroup = new Form_Group_ShareGroup();
$usersGroup->getElement('users')->setMultiOptions($data);

How to validate multiply select using Zend Framework 2

I am trying to validate a multiply select using input filter, but every time I see a error. The error is "notInArray":"The input was not found in the haystack".(I use ajax but it doesn`t metter).
I will show part of my code to be more clear.
in Controller:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
//.... more code...
When I put print_r($post['positions']); I see:
array(0 => 118, 1 => 119)
in ..../form/UserForm.php I create the multiply element
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
and in the validation file the code is:
$inputFilter->add($factory->createInput(array(
'name' => 'positions',
'required' => false,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(118,119),
'messages' => array(
'notInArray' => 'Please select your position !'
),
),
),
),
What can be the reason every time to see this error, and how I can fix it?
By default selects have attached InArray validator in Zend Framework 2.
If you are adding new one - you will have two.
You should disable default one as follow:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
'disable_inarray_validator' => true, // <-- disable
),
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
And you should get rid of the additional error message.
Please let us know if that would helped you.

How to set default value for form fields- Magento?

I have an admin form field(textbox),
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
));
I need to set a default value for this text field. I tried, setting 'value' => '120', in it. But its not working.
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'value' => '120',
));
How to set a defalt value in that field?
In my case, setValues() function was overriding the 'value' that I had set for the field.
Cause:
In my _prepareForm() function, I set the value as below:
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'value' => '120',
));
At the end of _prepareForm() function, there was the following line of code which would reset the form values:
$form->setValues($model->getData());
Solution:
Set model data before setValues() function as below:
if (!$model->getId()) {
$model->setData('ajax_time_interval', '120');
}
$form->setValues($model->getData());
you can do that by adding "default" attribute in field configurations.
$fieldset->addField('ajax_time_interval', 'text', array(
'label' => Mage::helper('dealroom')->__('Page Refresh Time Interval'),
'class' => 'required-entry',
'required' => true,
'name' => 'ajax_time_interval',
'default' => '120',
));
Remove the last semicolon....
Check this site help-me a lot:
http://www.excellencemagentoblog.com/magento-admin-form-field
Gl mf

upload framework extension .swf php

i want to upload a .swf format and i am getting an exception "false extension" and i tried to put a validator on the extension and it still didn't work any ideas how to upload .swf and do i need a special uploader. This is my form code.
class Admin_Form_Banner extends ZendX_Form_Designed {
public function init() {
$this->setEnctype(self::ENCTYPE_MULTIPART);
$this->setMethod(self::METHOD_POST);
$this->setMethod('post');
// Add an email element
$this->addElement('text', 'banner_title', array(
'label' => 'Banner Title',
'required' => true,
"class" => 'required',
'filters' => array('StringTrim')
));
$this->addElement('select','banner_type',array(
'label'=>'type',
'required'=>TRUE,
'class'=>'required',
'multiOptions'=>array('1'=>'Image','2'=>'Flash','3'=>'HTML')
));
$this->addElement('text', 'banner_link', array(
'label' => 'Banner Link',
'required' => true,
"class" => 'required url',
'value'=>'http://www.',
'filters' => array('StringTrim'),
));
$this->addElement('select','link_open',array(
'label'=>'choose how do you want the link to open ?',
'required'=>TRUE,
'multiOptions'=>array('self'=>'Same Page','_new'=>'Tab Page')
));
$this->addElement('checkbox', 'is_active', array(
'label' => 'Is Active',
'required' => true,
"class" => 'required',
'filters' => array('StringTrim')
));
$banner_position = new Zend_Form_Element_Select('banner_position');
$banner_position->setMultiOptions($this->getBannerPositions())->setLabel('Banner Position');
$this->addElement($banner_position, 'banner_position');
$this->addElement('hidden', 'file_path', array(
'required' => true,
"class" => 'required',
'Extension'=>'.swf'
));
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => ''
));
}
Well it's a Zend_Form_Element_Hidden how could you receive a fileupload from that element ?
Maybe you've made some customization to allow that but we can't really guess with this limited code sample.

Categories