cakePHP input with characters counting - html

I have the HTML code like this:
<label>Main Text (<span class="main_text_count">0</span><span>) Characters</span></label>
<textarea name="data[News][sentence]" id="main_text" class="form-control" required rows="8"></textarea>
And I don't know how to create a text area like this, using Form helper of cakePHP.

Simply done using the Form Helper (both for CakePHP 2 and CakePHP 3):-
echo $this->Form->textarea(
'News.sentence',
[
'id' => 'main_text',
'class' => 'form-control',
'rows' => 8,
'label' => 'Main Text (<span class="main_text_count">0</span><span>) Characters</span>'
]
);
In future make sure you've fully read Cake's documentation.

Related

Confirm message not working on postLink with icon

I need some help with a postLink created with FormHelper in CakePHP 3. The normal postLink works just fine like this:
<?= $this->Form->postLink(__('Delete'),
['action' => 'delete', $member->id],
['confirm' => __('Are you sure, you want to delete {0}?', $member->name)]) ?>
But when I try to use a font-awesome icon / i-tag instead of the text link, the confirmation message is not showing up anymore. The icon itself is showing correctly and the action still works fine, just the message is not working.
I used the following posts for help, but the examples in the answers there are not working for me:
CakePHP equivalent of html code
CakePHP delete confirmation
I tried these two approaches:
<?= $this->Form->postLink('<i class="fa fa-trash"></i> ',
['action' => 'delete', $member->id],
['escape' => false],
['confirm' => __('Are you sure, you want to delete {0}?', $member->name)]) ?>
<?= $this->Form->postLink(
$this->Html->tag('i', '', ['class' => 'fa fa-trash']),
['action' => 'edit', $member->id],
['escape' => false],
['confirm' => __('Are you sure you want to delete {0}?', $member->name)]); ?>
I'm still very new to CakePHP and tried to look this up in the book, but that did not help me. I also tried the exact syntax as shown in the SO links above, which seemingly worked for some others...but the confirmation message is still not working for me.
What am I doing wrong here?
escape and confirm options should be in the same array. Function postLink() looks like this:
postLink(string $title, mixed $url = null, array $options =[])
So working code for you will be:
<?= $this->Form->postLink('<i class="fa fa-trash"></i> ',
['action' => 'delete', $member->id],
[
'escape' => false,
'confirm' => __('Are you sure, you want to delete {0}?', $member->name)
]
) ?>

Define HTML validation attributes with Laravel Blade syntax

I would like to combine the Laravel model binding properties with the easy jQuery Validation plugin, if that is possible.
I can't figure out how to add the 'required' attribute to the blade syntax.
Here is normal form syntax with the "required" attribute:
<input type="text" name="title" required>
Laravel Blade syntax
{{ Form:: text('title', null, array('class' => 'form-control')) }}
Any help would be appreciated.
required is a normal HTML input attribute. The only difference is that it doesn't have different values -- it's either present or it's not, which makes it the so-called boolean attribute.
Anyway, this should do the trick:
{{ Form:: text('title', null, array('class' => 'form-control', 'required' => '')) }}
See more here.

Formgenerator with div

I want to create forms in symfony2. I need this HTML structur for each control.
<div class="form-group">
<label class="col-md-2 control-label">Firstname:</label>
<div class="col-md-10"><input type="text" name="firstname" class="form-control">/div>
</div>
Now is my question, how can i add the div with the class form-group and how can i surround the input element with the div .col-md-10?
$builder->add('firstname', 'text', array(
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'col-md-2 control-label'
)
)
);
Check How to customize Form Rendering section of the documentation.
You may need to override the form_widget block (form_widget_compound block to go into details).
Find here the default behavior of each twig form helper.
The Form Theming Sub Chapter contains many relevant examples on how to customize any given form field block.

CakePHP Span Tag within Anchor Tag

I'm trying to have CakePHP output a link that looks like this:
<a href="/foo/bar" class="some other classes">
<span class="icon new"></span>FooBar</a>
So I use the following code in my view
<?php
echo $this->Html->link(
$this->Html->tag('span', null, array('class' => 'icon new')) . "FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
?>
However CakePHP outputs the following:
<a href="/foo/bar" class="some other classes">
<span class="icon new">FooBar</span></a>
Which breaks my design. How can I get CakePHP to append "FooBar" after the <span> tags?
EDIT: Its also worth mentioning that I know a <span> tags shouldn't be within an anchor tag usually, but in the case its a must.
You need to use an empty string in stead of null as the text for the span, then your code will work as expected.
Looking at the source code of the HtmlHelper, null is treated as a 'special' value, causing only the opening tag of the span to be created. You can see this in this line:
https://github.com/cakephp/cakephp/blob/2.3.2/lib/Cake/View/Helper/HtmlHelper.php#L906
Change your code to this and it should work;
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'icon new')) . "FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
Additional explanation of the closing </span>
A bit of explanation, for those who wonder:
The closing </span> in your example is actually not present in the output generated by CakePHP, but automatically 'added' by your browser. If you view the source of the HTML in your browser, you'll see that this is what is actually in your HTML:
<a href="/foo/bar" class="some other classes">
<span class="icon new">FooBar</a>
As you can see, no closing 'span'
Because the <span> is not closed, the browser will try to correct this error and automatically assumes that you 'forgot' to close it. Therefor it will add a closing </span> before the next tag it finds (in this case the closing </a>).
The 'inspector' in your browser will always show the HTML that the browser uses to render the output. This includes automatic corrections made by the browser and dynamically generated elements (e.g. Elements added via JavaScript).
To check the output of your PHP scripts, always view the source, not the inspector
In this situation, I've avoided the CakePHP helpers entirely because the markup becomes really messy and cannot take advantage of autocomplete or validation within your IDE.
I usually do something like this:
<span class="icon-new"></span>Foobar
This looks a little overkill for me. Just do this:
echo $this->Html->link(
"<span class="icon new"></span> FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
I've been using CakePHP for 4 years and I don't see the benefit of using tag in this instance.
Would you be able to use regular PHP in this case?
I am thinking you could do it like this:
<?PHP
echo('<span class="' . 'icon new' . '"></span>' . 'FooBar' . '')
?>

Zend Form problem:Setting decorators for textarea and textinput length values

In my Zend form code I have the following
$address = new Zend_Form_Element_Textarea('accounts_address');
$address->setLabel('Address')
->setAttrib('rows','4')
->setAttrib('cols','4')
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$address->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array(array('data'=>'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr'))
));
However in my output I do get the attributes set but the text area seems to remain the same size
Output
<tr><td id="accounts_address-label"><label for="accounts_address" class="optional">Address</label></td>
<td>
<textarea name="accounts_address" id="accounts_address" rows="4" cols="4"></textarea></td></tr>
What am I missing?