I have a problem in symfony2 form.
I have a same field in a form. I create one by one by changing the name.
$formBuilderUpload = $this->createFormBuilder()->setAction($this->generateUrl('bsi_bkpm_backend_uploadfile_create'));
$formBuilder = $formBuilderUpload;
$formBuilder->add('file_1','file', array('required'=> false, 'label'=> 'File 1'))
->add('filename_1' , 'text', array(
'required' => false,
'label' => 'Subject 1'
))
->add('description_1','text',array('required'=> false, 'label'=>' Description 1'))
->add('file_2','file', array('required'=> false, 'label'=> 'File 2'))
->add('filename_2' , 'text', array(
'required' => false,
'label' => 'Subject 2'))
->add('description_2','text',array('required'=> false, 'label'=>' Description 2'))
->add('file_3','file', array('required'=> false, 'label'=> 'File 2'))
->add('filename_3' , 'text', array(
'required' => false,
'label' => 'Subject 3'))
->add('description_3','text',array('required'=> false, 'label'=>' Description 4'))
->add('file_4','file', array('required'=> false, 'label'=> 'File 2'))
->add('filename_4' , 'text', array(
'required' => false,
'label' => 'Subject 4'))
->add('description_4','text',array('required'=> false, 'label'=>' Description 4'))
->add('file_5','file', array('required'=> false, 'label'=> 'File 5'))
->add('filename_5' , 'text', array(
'required' => false,
'label' => 'Subject 5'))
->add('description_5','text',array('required'=> false, 'label'=>' Description 5'))
// add submit button
->add('submit', 'submit', array('label' => 'Create'));
//Create the form
$form = $formBuilder->getForm();
return $form;
But I want the code become efficient by looping it. Does anyone know how to looping it?
Best regards,
hendrawan
This seems like a pretty easy one, just loop over a for loop like so...
$limit = 5;
for ($i = 1; $i <= $limit; $i++) {
$builder
->add('file_'.$i, 'file', array(
'required' => false,
'label' => 'File '.$i,
))
->add('filename_'.$i, 'text', array(
'required' => false,
'label' => 'Subject '.$i,
))
->add('description_'.$i, 'text', array(
'required' => false,
'label' => 'Description '.$i,
))
;
}
Related
I have added product attribute as follows
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'product_activation',
[
'type' => 'datetime',
'backend' => '',
'frontend' => '',
'label' => 'Product Activation Timestamp',
'input' => 'date',
'class' => '',
'source' => '',
'time' => true,
'date_format' => 'yyyy-MM-dd',
'time_format' => 'hh:mm:ss',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => ''
]
);
Still unable to get the datetime picker in my admin product form.
Please help me to solve this
Please try below code :
<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* Install attributes
*/
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
/**
* #var \Magento\Catalog\Setup\CategorySetupFactory
*/
protected $categorySetupFactory;
/**
* Init
*
* #param \Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory
*/
public function __construct(
\Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory
) {
$this->categorySetupFactory = $categorySetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$setup->startSetup();
$categorySetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'product_activation',
[
'type' => 'datetime',
'backend' => '',
'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
'label' => 'Product Activation Timestamp',
'input' => 'date',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => '',
'sort_order' => 9,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
$setup->endSetup();
}
}
I have a form like this:
$formMapper
->add('formats', 'collection', array(
'type' => new FormatType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => 'remove'
))
->add('papers', 'collection', array(
'type' => new PaperType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => 'remove'
))
;
As you can see I changed the prototype_name to remove.
Originally it looks like this:
When I click on the plus sign I get this:
As you can see the remove text is added. But also label__* . How can I fix this that the label__* text isn't added or displayed?
Use 'label_attr' => ['style' => 'display: none']
$formMapper
->add('formats', 'collection', array(
'type' => new FormatType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label_attr' => ['style' => 'display: none']
))
->add('papers', 'collection', array(
'type' => new PaperType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label_attr' => ['style' => 'display: none']
))
;
->add('appointment', CollectionType::class,[
'label' => false,
'entry_type' => AppointmentType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => false,
'prototype' => true,
])
this is my collection for add appointments see the entry_options
When the collection element is rendered , description element does not appear.
ZF2 seems to ignore the description option in the form collection.
Form
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'value',
'options' => array(
'label' => 'Please choose value',
'count' => 1,
'should_create_template' => true,
'target_element' => new ValueFieldset($objectManager)
)
));
Form Fieldset: ValueFieldset
$this->add(array(
'name' => 'name',
'type' => 'Zend\Form\Element\Text',
'options' => array(
'label' => 'Values',
'label_attributes' => array(
'class' => 'form-label'
),
'description' => 'X',
'multiple' => true,
),
)
);
View
echo $this->formCollection($values);
I am using zend form and try to validate a client side validation. My code is like:
$this->addElement('text', 'email', array(
'label' => 'Email:',
'required' => true,
'class' => 'span12',
'attribs' => array(
'required' => true,
'pattern'=> "^[A-Za-z0-9._]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
)
));
$this->setAttrib('id', 'lead_form_creation');
$this->addElement('text', 'name', array(
'label' => 'Name:',
'required' => true,
'class' => 'span12',
'attribs' => array(
'required' => true,
'pattern' => '[a-zA-Z]{4,}'
)
));
$this->addElement('text', 'phone', array(
'label' => 'Phone:',
'required' => true,
'class' => 'span12',
'attribs' => array(
'required' => true,
'pattern' => '\d{4,}'
)
));
By the above three fields I am able to create a form. Also my code is validating above three fields also. But whenever I am writing some valid email address and press tab then name field and phone number fields are showing red simultaneously. But user's perspective it should be only name field. Then if name is not validating and press tab, name field s well as phone field should be show error.
Please let me know whether anything I have to mention in the attrbs attribute or anywhere else?
Thanks in advance.
Typo in your code (attribs should be attributes):
$this->addElement('text', 'email', array(
'label' => 'Email:',
'required' => true,
'class' => 'span12',
'attributes' => array(
'required' => true,
'pattern'=> "^[A-Za-z0-9._]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
)
));
$this->setAttrib('id', 'lead_form_creation');
$this->addElement('text', 'name', array(
'label' => 'Name:',
'required' => true,
'class' => 'span12',
'attributes' => array(
'required' => true,
'pattern' => '[a-zA-Z]{4,}'
)
));
$this->addElement('text', 'phone', array(
'label' => 'Phone:',
'required' => true,
'class' => 'span12',
'attributes' => array(
'required' => true,
'pattern' => '\d{4,}'
)
));
I want to implement a sort of onkeyup() function in Zend form to add an element. I don't know the syntax.
This is my code:
$this->addElement('text', 'userid', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:212px'),
Use the following code:
$this->addElement('text', 'userid', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:212px'),
'attribs' => array('onkeyup'=>'jsFunction();')
));
OR you can use below code:
$element = $this->addElement('text', 'userid', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:212px'),
));
$element->setAttrib('onkeyup', 'jsFunction();');
you have to use attribs options to add extra attributes
Try This
$this->addElement('text', 'userid', array(
'label' => '',
'required' => true,
'filters' => array('StringTrim'),
'style' => array('width:212px'),
'attribs' => array('onkeyup'=>'handler()')
you can also use setAttrib