How to add a class in li-Tag of a menu? - class

I seem to be blind, stupid or both but I can't add own classes to li-items of a menu. Here are multiple tries:
<?php
$options = array(
'items' => 'menu1', //menu to be displayed
'class' => 'own-class-test-1',
'css' => array(
'class' => 'own-class-test-2',
),
'active' => 'own-active-test-2',
'attributes' => array(
'id' => 'ipsTest',
'class' => 'nav nav-pills pull-right'
)
);
echo ipSlot('menu', $options); ?>
Can anybody help me with this? I am only able to change the preset like active-class. Adding classes to the ul works without problems.
Thank you & cheers,
Thomas

I've check the code. Currently you can modify <li> only through variable defined here - https://www.impresspages.org/docs/navigation
There's no "default" things to add extra class for all <li>. However, if it selects every tag then what's the point adding it? Add required styles directly through CSS.

Related

How to pass data from relations before creating form?

Here is my part of my form where i'm creating a new student:
$students = new students;
$form = $this->createFormBuilder($students)
->add('name', TextType::class, array('label' => 'Imię','attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('grupa_id', ChoiceType::class,array('choices' => $id), array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('save', SubmitType::class,array('label' => 'Dodaj ucznia', 'attr' => array('class' => 'btn btn-primary', 'style' => 'margin-bottom:15px')))
->getForm();
I have relations between students Entity and Grupa Entity.
When I'm adding new student I want to choice him only to one group. So I need to create ChoiceType field and pass there id of groups. But I don't know how to do that. Like above it doesn't work of course.
I show them later like that:
$studentsList = $em->getRepository('AppBundle:students')->findBy(array('grupa_id' => $id));
$Grupa = $em->getRepository('AppBundle:Grupa')->Find($id);
grupa_id is FK in students Entity.
I don't know what else I should tell about my problem. If you can help me and need some more information just ask me.
Thanks for help
As #Rawburner said you should use EntityType and then if you want more control on your field (EntityType) grupa_id use choice_name and choice_value overriden options.
Another thing, if you want to pass date before creating the form you can use FormEvents::PRE_SET_DATA events. Now these solutions will depend on what you actually want to do.

How do I allow html tags in label for Zend form element using addElement()?

I am brand new to Zend and I've been given a project to make adjustments on. I'd like to add html to the labels for my form elements but I can't seem to get it right.
Here's what I have:
$this->addElement('text', 'school_name', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 150)),
),
'required' => true,
'label' => 'Name* :<img src="picture.png">,
'size' => '90',
));
As is, of course, the <img src="picture.png"> text gets escaped and the whole string is displayed.
I've read that I need to use 'escape' => false in some capacity but I can't figure out where/how to use it in my specific case.
Any help would be great. Thanks!
After calling addElement fetch the label's decorator and change the escape setting:
$form->getElement('school_name')->getDecorator('label')->setOption('escape', false);
If you use this type of label a lot, you should consider writing a custom decorator.
You can also use the disable_html_escape in 'label_options' when adding an element to the form:
$this->add(array(
....
'options' => array(
'label' => '<span class="required">Name</span>,
'label_options' => array(
'disable_html_escape' => true,
)
),
...
));
Credit to Théo Bouveret's post 'Button content in ZF2 forms' for the answer.

Individual class option for radio buttons

Is there an individual class option for radio buttons in Cake 2.x?
In my opinion the logical approach would be like:
<div class="input radio required">
<?php
$options = array(
array('1' => 'Kuchen', 'class' => 'cake'),
array('2' => 'Kekse', 'class' => 'biscuits'),
array('3' => 'Eis', 'class' => 'iceCream'),
);
$attributes = array(
'legend' => false,
'default' => '1'
);
echo $this->Form->radio('INCOMETYPE', $options, $attributes);
?>
</div>
But that doesn´t work. I hope you can help. Thanks :)
You could extend FormHelper to add implement this functionality if you intend to use it elsewhere. If it's a one off, you might be better to produce the markup by hand. Use the code produced by $this->Form->radio to give you a start.

Example of Zend Form with Collection Element using Forms not Fieldsets in Zend Framework2

I need a straight forward working example how I can include a collection element in Zend Form, I have seen some examples from Zend Framework 2 site and from previous posts in StackOverflow where most of them pointed to this link. But right now I am not using Fieldsets and staying with Forms, so in case if someone can direct me in the right way, how I can include a simple collection element when the user gets a page where the user can choose multiple choices from the shown collection form. Much better would be populating the collection form from database.
I have searched in the internet for quite a sometime now and thought I would post here, so that Zend profis can give their suggestions.
Just For Information:
Normally one can include a static dropdownbox in Zend Form in this fashion
$this->add(
array(
'name' => "countr",
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => "Countries",
'options' => array(
'country1' => 'Brazil',
'country2' => 'USA',
'country3' => 'Mexico',
'country4' => 'France',
)
)
)
);
So I am expecting a simple example which could give me a basic idea how this can be done.
To be honest, I don't see your problem here. Since form collections extend Fieldset which extends Element, you can just add it to the form as a regular element. The view helpers will take care of the rendering recursively.
Step 1: Create a form collection (create an instance of Zend\Form\Element\Collection). If the elements have to be added dynamically in some way, I'd create a factory class for this purpose.
Step 2: Add it to the form. (For example using $form->add($myCollectionInstance).)
Step 3: Render it. Zend\Form\View\Helper\Collection is a pretty good view helper to render the whole form without any pain.
You can also create a new class extending Zend\Form\Element\Collection and use the constructor to add the fields you need. Thus, you can add it to the form using the array you've pasted in your question. Also, you could directly use it in annotations.
Hope this helps.
If you just want to fill in a select list with option values you can add the array to the select list in a controller:
$form = new MyForm();
$form->get('countr')->setOptions(array('value_options'=>array(
'country1' => 'Brazil',
'country2' => 'USA',
'country3' => 'Mexico',
'country4' => 'France',
));
the array can be fetched from db.
this is a different example for using form collections in the simplest way.
In this example it creates input text elements in a collection and fills them in. The number of elements depends on the array:
class MyForm extends \Zend\Form\Form
{
$this->add(array(
'type' => '\Zend\Form\Element\Collection',
'name' => 'myCollection',
'options' => array(
'label' => 'My collection',
'allow_add' => true,
)
));
}
class IndexController extends AbstractActionController
{
public function indexAction
{
$form = new MyForm();
$this->addElementsFromArray($form, array(
'country1' => 'Brazil',
'country2' => 'USA',
'country3' => 'Mexico',
'country4' => 'France',
));
//the above line can be replaced if fetching the array from a db table:
//$arrayFromDb = getArrayFromDb();
//$this->addElementsFromArray($form, $arrayFromDb);
return array(
'form' => $form
);
}
private function addElementsFromArray($form, $array)
{
foreach ($array as $key=>$value)
{
$form->get('myCollection')->add(array(
//'type' => '\Zend\Form\Element\SomeElement',
'name' => $key,
'options' => array(
'label' => $key,
),
'attributes' => array(
'value' => $value,
)
));
}
}
}
index.phtml:
$form->setAttribute('action', $this->url('home'))
->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form->get('myCollection'));
echo $this->form()->closeTag();

Can you add an error decorator to a Zend subform?

I have a custom validator that checks all the values in a subform to make sure that they make sense in relation to each other. In the event that this validator fails, I'd like to have an error decorator at the top of the subform to display the error message. Is this possible?
I've already set up the decorators like so:
protected $_decorators = array(
array(
'decorator' => 'FormElements',
'options' => array()
),
array(
'decorator' => 'HtmlTag',
'options' => array(
'tag' => 'ul',
'class' => 'test'
)
),
);
And it seems like I should be able to add
array(
'decorator' => 'Errors',
'options' => array(
'tag' => 'ul',
'class' => 'errors',
'placement' => 'prepend',
)
),
but that causes Zend to fail with the error "htmlspecialchars() expects parameter 1 to be string, array given". What am I doing wrong then? Thanks!
I believe nothing is wrong in your code, just ZF doesn't handle the Errors decorator within Zend_Form_SubForm properly. I hope they will fix this soon.