Render Zend\Form\Element\Button with content and without label - php

It seems like it's impossible to render a button element automatically using the FormCollection view helper without label.
Whenever possible, I render forms this way:
<?php echo $this->form()->openTag($this->form); ?>
<?php echo $this->formCollection($this->form); ?>
<?php echo $this->form()->closeTag($this->form); ?>
However, the FormButton view helper, that is invoked by FormCollection, awaits the button's content as second param to the render() method -- or a label option. If I set the label, the button renders correctly, but, you guess it, with label.
I tried to figure out a workaround for this by browsing through the code, but I can't see one.
Note: FormButton renders <button>...</button> elements. I could also live with a <input type="button" /> element.
Am I missing something here? Thanks in advance!

When I was struggling with this problem, I've got the solution accidentally! It is so easy (or hackish?), that I wouldn't have thought to work.
Add this to your fieldset:
$this->add(array(
'name' => 'delete',
'attributes' => array('type'=>'button', 'value' => 'Delete', 'onclick'=>'delete()'),
));
That's all. It is working for me very well.

Write your own formCollection ViewHelper that uses your very own formButton ViewHelper.

Related

CakePHP Form->postLink does not work for the first row [duplicate]

Using code that has been baked into CRUD, I have the following code for deleting an item:
<?php echo $this->Form->postLink(__('Delete'), array('controller'=>'attachments', 'action' => 'delete', $attachment['Attachment']['id']), null, __('Are you sure you want to delete "%s?"', $attachment['Attachment']['name'])); ?>
The problem is that it lies wrapped in a FORM tag, and so what ends up happening is Cake doesn't include the Form that the postLink would submit.
Is there another way that still holds true to the integrity of Cake's infrastructure that would work even when I increase the security settings? Probably needs to be a link to /attachment/delete/id, but baking for some reason chose to create a form and post it vs. creating a link, so I figured there was a reason for that and if so I want to uphold that reason.
You probably didn't read the warnings in the doc block regarding this method
(http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink)
This method creates a <form> element. So do not use this method inside an existing form.
Instead you should add a submit button using FormHelper::submit()
So don't do that. You would need to write to a buffer and output later (see this closed PR).
I have the same problem in cakephp 3.0 $this->Form->postLink() was not working for first entry.
Then i do some R&D but not found any useful. Then i make some changes in form tag and $this->Form->postLink(). I remove the $this->Form->create() from the .ctp files and use only $this->Form->postLink(); and it start working.
Do not use the $this->Form->postLink() inside any other form tag
i.e $this->Form->create(null, ['url' => ['action' => 'ExportCustomers']]);
If you want both then you have to adjust the $this->Form->postLink() and form tag according to it will not effect each other.

Yii independent linkButton not working within form tags

I am using Yii and I have a form wrapped in tabs. In one of these tabs I need to put a link (call a controller/action with parameters) independent from the form content.
I've tried to use CHtml::linkButton but it doesn't work.
The code scheme of the form is the following:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'project-form',
'enableAjaxValidation'=>true,
)); ?>
//...input elements
<?php echo CHtml::linkButton('Download',
array(
'submit'=>$this->createUrl('controller/action'),
'params'=>array(
'results'=>CJSON::encode(array('foo'=>'bar'))
))
); ?>
//...other input elements
<?php $this->endWidget(); ?>
When I click on the link nothing happens. If I put the linkButton code outside the form it works properly.
Is there a workaround for this?
It looks like you're trying to use a button to let the user download some sort of file. For this situation AJAX won't work as it's not capable of saving files to a users computer.
What you want to do is to create just the anchor tag. The function linkButton is designed for is to be a button that will submit the current form. If you're using bootstrap the below will also make it look like a button.
CHtml::link('Download', array('controller/action', 'myparam' => 'paramvalue'), array('class', 'btn btn-primary'));
Make sure you set the correct headers for downloading the file.
In this case you should use ajaxLink
Example:
echo CHtml::ajaxLink(
$text = 'Click me',
$url = '/',
$ajaxOptions=array (
'type'=>'POST',
'dataType'=>'json',
'success'=>'function(html){ jQuery("#your_id").html(html); }'
),
$htmlOptions=array ()
);

First postLink() doesn't create form

Using code that has been baked into CRUD, I have the following code for deleting an item:
<?php echo $this->Form->postLink(__('Delete'), array('controller'=>'attachments', 'action' => 'delete', $attachment['Attachment']['id']), null, __('Are you sure you want to delete "%s?"', $attachment['Attachment']['name'])); ?>
The problem is that it lies wrapped in a FORM tag, and so what ends up happening is Cake doesn't include the Form that the postLink would submit.
Is there another way that still holds true to the integrity of Cake's infrastructure that would work even when I increase the security settings? Probably needs to be a link to /attachment/delete/id, but baking for some reason chose to create a form and post it vs. creating a link, so I figured there was a reason for that and if so I want to uphold that reason.
You probably didn't read the warnings in the doc block regarding this method
(http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink)
This method creates a <form> element. So do not use this method inside an existing form.
Instead you should add a submit button using FormHelper::submit()
So don't do that. You would need to write to a buffer and output later (see this closed PR).
I have the same problem in cakephp 3.0 $this->Form->postLink() was not working for first entry.
Then i do some R&D but not found any useful. Then i make some changes in form tag and $this->Form->postLink(). I remove the $this->Form->create() from the .ctp files and use only $this->Form->postLink(); and it start working.
Do not use the $this->Form->postLink() inside any other form tag
i.e $this->Form->create(null, ['url' => ['action' => 'ExportCustomers']]);
If you want both then you have to adjust the $this->Form->postLink() and form tag according to it will not effect each other.

How to pass a class to make invisible in the input field of YII?

<?php
$this->widget('CAutoComplete', array(
'name'=>'search',
'id'=>'input-box',
'attribute'=>'search',
'url'=> $this->createAbsoluteUrl('products/suggestions'),
'value'=>($_GET['search'] == '')?'Search for Mobiles, Cameras & Laptops':$_GET['search'],
'minChars'=>2,
'scroll'=>false,
'resultsClass'=>'searchAutoComplete ac_results',
'htmlOptions'=> array(
'class'=>'searchClickClear',
),
'methodChain'=>'.result(function(){$("form#search-form").submit();})'
));
?>
This is my widget for taking input, and I am passing the class searchAutoComplete. What the class should be? What I want is, when I click on the field the value will be empty.
When you click on input field, event onclick is going on.
So, you can handle it and change input value to empty. Howto?
Change your 'htmlOptions' like this:
'htmlOptions'=> array(
'onclick'=>'$("#input-box").val("")';
),
#input-box - css-selector of your input.
val() - jQuery method for setting some value to elements. See more info about this method
resultsClass - the CSS class for the dropdown list. Defaults to "ac_results". More info
For the task solution you don't need change resultsClass from the default value. Remove this string.

How to Update two text fields onchange of a dropdown list

Im building a web app using the yii framework. I have a dropdownlist and im calling an action and updating a div tag using ajax array 'update'=>'#price' field. the code works fine and it updates the price div.
But i want to update two fields like that, i tried passing an array to the update field. but it didnt work.
Any Idea how I can update two div tags and show two values using one action call?
Heres My Code..
echo CHtml::beginForm();
echo CHtml::dropDownList('amount_'.$position,'', array(1=>1,2=>2,3=>3),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('shoppingCart/updateAmount'),
'update'=>'#price_'.$position, //selector to update
)));
echo CHtml::endForm();
and in my action im just echoing
echo 'LKR '.Shop::priceFormat(#$product->getPrice($cart[$position]['Variations'], $value));
It'll be great if someone could help.
It's simply jQuery selector. I believe you can use comma for few ids. Or you can use class selector. (I think class selector would be better here)
'ajax' => array(
/* ... */
'update' => '#price_1, #price_2, #price_3',
/* or */
'update' => '.price'
)

Categories