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
Related
I want to save collection form.
I have a base form BasePromotionsType, in which I want to save another form NumberOfVisitsType. But I have an error with This form should not contain extra fields. In the debager, an array comes to me in field number_of_visits:
array:2 [▼
"count_visits" => 10
"count_points" => 100
]
To fill out the form, I submit this json:
{
"active": true,
"use_promotion": true,
"label": "string",
"description": "string",
"picture": 1,
"available_cities": [
1
],
"promotions_settings": 1,
"list_users": [
],
"number_of_visits": {
"count_visits": 10,
"count_points": 12
}
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('active', CheckboxType::class, ['label' => 'active', 'required' => false])
->add('use_promotion', CheckboxType::class, ['label' => 'Одноразовая/Многоразовая', 'required' => false])
->add('label', TextType::class, ['label' => 'Заголовок', 'required' => false])
->add('description', TextType::class, ['label' => 'Описание', 'required' => false])
->add('picture', IntegerType::class, [
'invalid_message' => 'avatar is not a valid',
])
->add('available_cities', CollectionType::class, [
'entry_type' => IntegerType::class,
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true,
'invalid_message' => 'cities is not a valid',
])
->add('promotions_settings', IntegerType::class, [
'invalid_message' => 'settings are invalid',
])
->add('list_users', CollectionType::class, [
'entry_type' => IntegerType::class,
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true,
'invalid_message' => 'cities is not a valid',
])
->add('number_of_visits', CollectionType::class, [
'entry_type' => NumberOfVisitsType::class
])
->add('send', SubmitType::class, ['label' => 'Отправить'])
;
$builder->get('picture')
->addModelTransformer($this->avatarTransformer);
$builder->get('available_cities')
->addModelTransformer($this->citiesTransformer);
$builder->get('promotions_settings')
->addModelTransformer($this->promotionsSettingsTransformer);
$builder->get('list_users')
->addModelTransformer($this->usersTransformer);
}
As a result, I need to keep the basic form and the form that is built into the basic form.
I solved the problem, it was necessary to register in the field number_of_visits
->add('number_of_visits', CollectionType::class, [
'entry_type' => NumberOfVisitsType::class,
'invalid_message' => 'number are invalid',
'allow_add' => true,
'allow_extra_fields' => true
])
I'm looking for a way to add placeholders to the collection field prototype. Adding the attribute 'placeholder' does not make any effect.
I tried using prototype_data and replacing the value-attribute with placeholder in twig, but it throws an error if field type is a number, and passed text value can't be converter to a number.
$builder->add('fees', CollectionType::class, [
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'entry_type' => TextType::class,
'prototype' => true,
'attr' => [
'placeholder' => 'Fee Type',
],
])
Generally you can embed any available option for the Form Type specified in entry_type option (in your case TextType) to all the entries of your CollectionType through the entry_options.
Specifically for your problem you should use this:
$builder->add('fees', CollectionType::class, [
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'entry_type' => TextType::class,
'prototype' => true,
'entry_options' => [
'attr' => [
'placeholder' => 'Fee Type',
]
]
])
Documentation here
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,
))
;
}
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