Zend Framework 2 Skeleton Layout [duplicate] - zend-framework

This question already has an answer here:
globally change ZF2 form formatting
(1 answer)
Closed 7 years ago.
I am creating a form using Zend Framework 2 and the Skeleton Application.
I want my layout to replicate the default example:
As you can see above, the form inputs stretch to the width of the screen. The code to produce this is (using just the email address field as an example):
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" placeholder="Email" id="exampleInputEmail1" class="form-control">
</div>
I would like the same result to happen when I add my own form element using Zend Framework 2 (Skeleton application). But when I add an element:
$this->add(array(
'name' => 'campaign_code',
'type' => 'Text',
'options' => array(
'label' => 'Campaign Code',
),
'attributes' => array(
'class' => 'form-control',
)
));
The resulting HTML is:
<div class="form-group">
<label>
<span>Campaign Code</span>
<input class="form-control" type="text" name="campaign_code">
</label>
</div>
Resulting in:
Notice how the label tag is surrounding the span and input elements? I don't want that. I want the label tag to close itself as in the first html example.

The form view helpers are so smart while rendering the elements from your form.
Add an id attribute with respective element
$this->add(array(
'name' => 'campaign_code',
'type' => 'Text',
'options' => array(
'label' => 'Campaign Code',
),
'attributes' => array(
'class' => 'form-control',
'id' => 'your_id',
)
));

Related

Codeigniter, when I try to update, the single quote displays as '

I like set_value. Every time, when I try to update the post, the single quote displays as &#039 ;. Not only single quote, some characters display too. Please, help me
Im using Codeigniter 3, Php 7
View
$description = array(
'name' => 'description',
'type' => 'text',
'placeholder' => 'description',
'class' => 'form-control',
'rows' => '3',
'value' => set_value('description', $post->INTRO_DESCRIPTION),
);
<div class="form-group">
<label for="description" class="col-md-2 control-label">Details</label>
<div class="col-md-10"><?php echo form_textarea($description); ?></div>
<div class="col-md-10"><?php echo form_error('description', '<div class="text-danger">', '</div>'); ?></div>
</div>
Well I found the solution. And it's very simple. Just adding "false" at end of the set_value.
set_value('description', $post->INTRO_DESCRIPTION, FALSE)

How to add class attribute in symfony form [duplicate]

This question already has answers here:
Symfony - Add class to form tag
(2 answers)
Closed 5 years ago.
I'm looking to add some class attribute in my form using symfony form builder.
I have found how to do this for input and label, but not for the form tag.
$builder->add('label', TextType::class,
[
'attr' => ['class' => 'a'], // for input
'label_attr' => ['class' => 'b'], // for label
]
);
But the render is the following:
<form method="POST">
<label for="label" class="b">Label</label>
<input type="text" class="a" name="label">
<button type="submit">Create</button>
</form>
And i just want
<form method="POST" class="c">
I tried the following:
$builder->->setAttribute('class', 'c');
But it didn't change anything
Either you do it in your builder, controller or your view.
Controller :
$form = $this->createForm(new FormType(), $data, array('attr' => array('class' => 'class')));
View (Twig) :
{{ form_start(form, { 'attr' : { 'class': 'class' } })
As Joe pointed out source here

Drupal 7 forms. How do you wrap an input in a label?

I'm developing a custom form module in Drupal 7. I would like to wrap my form inputs in the label tags like this:
<div class="form-item form-type-textfield form-item-FirstName">
<label for="edit-firstname">First Name
<span class="form-required" title="This field is required.">*</span>
<input type="text" id="edit-firstname" name="FirstName" value="" size="25" maxlength="37" class="form-text required" /></label>
</div>
The label closing tag is after the end of the input. Normally it would be after the span closing tag.
I think I'll need to override the 'theme_form_element_label' function in the 'includes/form.inc' but I'm not sure how to go about it.
I don't know why you would wrap a form element around a form element. But to answer your question use the '#prefix' and '#suffix' keys to add your label.
So your code may look similar to this:
$form['first_name'] = array(
'#type' => 'textfield',
'#prefix' => '<label for="edit-firstname">' . t('First Name'),
'#suffix' => '</label>',
'#required' => TRUE
);
If you are trying to put a label next to a textfield all you would need to do is add the '#title' key to your textfield element. So it may look like this:
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE
);

How to make a selectbox the label of a radio button in Zend Fw

Not my idea, but I need a set of radio buttons, where the last buttons value is a select box. Visual explanation:
o Opt 1
o Opt 2
o |___SelectBox|
What it would look like in HTML:
<input type="radio" name="radioSet">Opt1
</input>
<input type="radio" name="radioSet">Opt2
</input>
<input type="radio" name="radioSet"><!-- Opt 3 -->
<select>
<option value="a"> aaa</option>
<option value="b"> bbb</option>
</select>
</input>
What I've done in ZF so far:
$picker = new Zend_Form_Element_Select('selectBox', array(
'multiOptions' => array('a'=>'aaa', 'b' =>'bbb'),
'decorators' => array(
array('Label', array('escape'=>false))
)
));
$this->addElement(
'radio',
'radioSet',
array(
'multioptions' => array(
'x'=>'Opt1',
'y'=>'Opt2',
'z'=>$picker //'Dropdown picker'
),
'label' => 'My Title:',
'decorators' => array(
'ViewHelper',
'Errors',
array('Description', array('escape' => false)),
'Label',
array('HtmlTag', array('tag'=>'div')),
),
)
);
But this returns just the 3 radio buttons, as well as the labels "Opt1" and "Opt2", but nothing after the third radio button.
I WANT it to be like the HTML code shown above, but this ZF code does not return it. Anyone an idea how this can be accomplished?
Thanks!
unfortunately you probably going to have to write a decorator to replace the label tag with a select. Looking at the code for the Zend_Form_Element_Radio() it specifically adds the label tag to the radio.
Thanks for your advices. I'm quite new to Zend, so I checked up how to make a custom view helper decorator, I couldn't manage to get it to work like that, but it helped me in another problem though.
I came to the solution, that it's easier to just add the select box as an individual element afterwards, and style it to the desired position with css.
Thanks again.

How can I use zend form decorator to render errors inside my paragraph tag wrapping label and input

I would like to render the following markup:
<div class="row">
<p>
<label>Your Name</label>
<input type="text" class="text_field" name="name">
<ul class="errors">
<li>Waarde is vereist en kan niet leeg worden gelaten</li>
</ul>
</p>
</div>
This is my Zend form element + decorator:
$this->addElement('text', 'name', array(
'label' => 'Naam:',
'class' => 'text_field',
'required' => true,
'decorators' => array(
'ViewHelper',
'Label',
'Errors',
array(array('row' => 'HtmlTag'), array('tag' => 'p')),
array(array('content' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row'))
)));
But this always renders the ul list below the p tag and never inside. It also adds an additional p tag below the list.
<div class="row">
<p>
<label class="required" for="name">Naam:</label>
<input type="text" class="text_field" value="" id="name" name="name">
</p>
<ul class="errors">
<li>Waarde is vereist en kan niet leeg worden gelaten</li>
</ul>
<p></p>
</div>
What am I doing wrong?
Found it! My stupid mistake. I did only check the final rendered output in my browser. I am using a template which also loads javascript and this changes the DOM which creates the unwanted result.
So the first decorator setup was working correct.
Try to do the following:
$this->addElement('text', 'name', array(
'label' => 'Naam:',
'class' => 'text_field',
'required' => true,
'decorators' => array(
'ViewHelper',
'Label',
'Errors',
array(array('content' => 'HtmlTag'), array('tag' => 'p')),
array(array('content' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row'))
)));