Symfony forms: avoid having submit button - php

I am using Symfony framework and I want to create a form without the submit button.
I explain myself better: I have a simple form, like the following one:
$form = $this->createFormBuilder($defaultData);
$form->add($fieldname, 'choice', array(
'choices' => array(
'3' => 'label_3',
'2' => 'label_2',
'1' => 'label_1',
'0' => 'label_0',
),
'expanded' => false,
'label' => 'choice',
));
$form->add('send', 'submit', array(
'label' => 'send'
));
Is there a way to avoid having a "submit" button?
The resulting behavior I want to enforce is:
- The User selects the desired choice
- On click, data is immediately submitted and handled by the controller
I searched in the Symfony manual, but I failed finding something that could fit.
Can anyone help me? (Is i even possible to have such a behavior?)
Thanks in advance.

You have to do this via JavaScript:
$('#formFieldId').change(function(){
$(this).closest('form').trigger('submit');
});

you should try with javascript like this :
jQuery().ready( function() {
// for example your fieldname id is "field"
jQuery( "#field").change( function(){
jQuery('form').submit();
});
});
you don t have to put you submit button in your form by the way, the standard way is to include the html for the button within the twig file

Related

Submitting form to different actions in CakePHP 2.6 results in blackhole

I've got a form in my CakePHP 2.6 app which has multiple buttons to submit the form to different actions. With Cake 2.4, this worked, but on 2.6 submitting to anything other than the default form action leads to a blackhole 'auth' error.
Just before the end of my form, I have multiple submit buttons, like so:
echo $this->Form->button('Default', array(
'type' => 'submit',
));
echo $this->Form->button('Alternate 1', array(
'type' => 'submit',
'formaction' => '/posts/otheraction',
));
echo $this->Form->button('Alernate 2', array(
'type' => 'submit',
'formaction' => '/posts/anotheraction',
));
Reading the docs, I see ‘auth’ Indicates a form validation error, or a controller/action mismatch error.. However, this worked in the past- it seems like things have gotten stricter. How can you get Cake to accept submitted forms from other actions without turning off Security completely?
That doesn't seem to be possible (at least not without jumping through enourmously large hoops), as the action is being incorporated in the token unconditionally.
$hashParts = array(
$this->_lastAction, // <<<<<<<<<<<
serialize($fields),
$unlocked,
Configure::read('Security.salt')
);
$fields = Security::hash(implode('', $hashParts), 'sha1');
https://github.com/cakephp/cakephp/blob/2.7.1/lib/Cake/View/Helper/FormHelper.php#L589
Also disabling this behavior would possibly weaken security, as posting to actions the data wasn't ment to be used for, might cause unintended behaviour.
You can handle this with a single action though, for example by giving the various submit buttons a name and a value, and in your controller action evaluate the value and do whatever needs to be done
echo $this->Form->button('Default', array(
'type' => 'submit',
'name' => 'action',
'value' => 'default'
));
echo $this->Form->button('Alternate 1', array(
'type' => 'submit',
'name' => 'action',
'value' => 'alternate1'
));
echo $this->Form->button('Alernate 2', array(
'type' => 'submit',
'name' => 'action',
'value' => 'alternate2'
));
switch ($this->request->data('action')) {
case 'default':
// ...
break;
case 'alternate1':
// ...
break;
case 'alternate2':
// ...
break;
}
I used jQuery to change the form's action
HTML button:
<button id='saveSubmitter'>Save</button>
javascript:
$('body').on('click', '#saveSubmitter',
function(e){
e.preventDefault()
$('form').attr('action', '<?php echo Router::url( array("action"=>"save")); ?>')
$('form').submit()
}
)

How to attach a file using SubmitForm() in Codeception

I'm testing a form through the SubmitForm() function because the form uses javascript to cycle through each individual item.
example:
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
), 'submit');
This works fine but I'm having trouble with a file upload input.
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
'file' => ???
), 'submit');
I tried sending an array to mimic the $_FILES array but that obviously isn't the right way to do it. Is this possible?
I've encountered this issue as well and the only way I can see around it is to manually fill the fields and the click the submit button.
For example
$I->fillField(['name' => 'name'], 'Test');
$I->attachFile('input[name=photo]', 'test.jpg');
$I->click('#formId button[type=submit]');
$I->seeCurrentRouteIs('route.index');
$I->see('Model has been updated.');
You can store any test files in the Codeception tests/_data folder.
This does work, but sadly doesn't help me in my current situation as I have a form which dynamically populates various select elements so I need to submitForm as I can't manually selectOption as the options are populated depending on other form completions.
I realise this is old and already marked as answer but it doesn't answer the specific question, which is still a problem for #alexleonard, who posted the accepted answer.
You can user attachFile in conjunction with submitForm. You just have to call is first. For example:
$I->attachFile('#form input[type=file]', <pathtofile> );
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
), 'submit');

symfony2 + bootstrap 3 readonly select dropdown still editable

I am using symfony2 and bootstrap 3 and when I set the readonly attribute to a form field, it gets greyed and I have the forbidden cursor but the field is still editable (in my case a select dropdown).
The readonly attribute would work great for a simple text field, but not for a select.
How can I make sure users can't click a select and change its value ?
I can't use "disabled" as I need the value to be passed to the form.
Using jquery to rewrite the readonly attribute also did not work.
my form:
->add('product', 'entity', array(
'label' => 'Produit',
'class' => 'AppBundle:MarketPlace\Product',
'read_only' => true,
))
Create a data transformer ProductToTextTransformer for your entity as explained in the doc, and then use it in your formbuilder, adding a select or a readonly text according to the condition for the select to be disabled or not :
//...
// this assumes that the entity manager was passed in as an option
$entityManager = $options['em'];
$transformer = new ProductToTextTransformer($entityManager);
if ($condition_to_disabled_the_select){
$builder->add('product', 'entity', array(
'label' => 'Produit',
'class' => 'AppBundle:MarketPlace\Product',
));
}
else{
$builder->add(
$builder->create('product', 'text', array('label' => 'Produit', 'read_only' => true))
->addModelTransformer($transformer)
);
}
The following is working but I don't liek it that much, I feel it's not clean like it should be:
<script>
$(function(){
$(':input[readonly]').each(function(){
$(this)
.hide()
.parent().append('<p>' + $(this).find(":selected").text() + '</p>')
})
;
})
</script>

How to create new button in Yiibooster TBGridview

I'm using Yiibooster and the TbGridView to show some results. I'm also using the following code to provide smart looking icons to a view, update and delete link.
array(
'htmlOptions' => array('nowrap'=>'nowrap'),
'class'=>'bootstrap.widgets.TbButtonColumn',
'viewButtonUrl'=>'Yii::app()->createUrl("/item/view", array("id"=>$data["id"], "sector" => $data["sector"]["slug"],"title" => $data["slug"]))',
'updateButtonUrl'=>'Yii::app()->createUrl("/item/update", array("id"=>$data["id"]))',
'deleteButtonUrl'=>null,
)
What I'd like to do is basically be able to show another button in there or in replace of the delete button. I'm just unsure how (or where specifically) I need to code the values for the this button.
I'm currently looking at the TbButtonColumn.php file and tried just adding a button just to see if it would work it didn't.
What would be the correct process to to do this?
Thanks in advance
Jonny
There is a buttons parameter for additional buttons, it is in docs od CButtonColumns, here is sample from link:
array(
'class'=>'CButtonColumn',
// Template to set order of buttons
'template' => '{postview} {preview}',
// Buttons config
'buttons' => array(
'postview' => array(
'label' => '...', // text label of the button
'url' => '...', // the PHP expression for generating the URL of the button
'imageUrl' => '...', // image URL of the button. If not set or false, a text link is used
'options' => array(...), // HTML options for the button tag
'click' => '...', // a JS function to be invoked when the button is clicked
),
'preview' => array(
// Another button config
),
),
),
NOTE: This is example for CButtonColumn but TbButtonColumn is a subclass of CButtonColumn, so everything applies to both.

Yii framework,CGridView Checkbox help

I want an alert that pops when user tries to click "add to favorite"
1) if there's no checkboxes checked.
2) I also want to know how to get the values of the checked boxes here's my current code on backend
<?php $this->widget('zii.widgets.grid.CGridView',array(
'id' => 'wsrecruitcvhead-grid',
'dataProvider' => $model->search(),
#'filter' => $model,
'columns' => array(
array(
'name' =>'',
'value' => 'CHtml::checkBox("rid[]",null,array("value"=>$data->ResumeID,"id"=>"rid_".$data->ResumeID))',
'type'=>'raw',
'htmlOptions' => array('width'=>5),
'visible' => !Yii::app()->user->isGuest,
),
array(
'name' => 'ResumeTitle',
'value' =>$model->ResumeTitle,
),
'ResumeSummaryIntroduction',
'Name',
array(
'class' => 'CButtonColumn',
'viewButtonUrl' => 'Yii::app()->createUrl("wsrecruitcvhead/view",array("id"=>$data["ResumeID"]))',
'template'=>'{view}',
),
),
));
?>
and here's the screen shot http://pastebin.com/sEpJBCiU
I hope this helps you.
Your code works just fine, just tried it. I would implement the functionality you mentioned using jQuery, like this:
<script type="text/javascript">
$("input:checkbox").click(function () {
var thisCheck = $(this);
if (thisCheck.is (':checked')){
// do what you want here, the way to access the text is using the
// $(this) selector. The following code would output pop up message with
// the selected checkbox text
$(this).val());
}
});
</script>
This code selects all those DOM elements of type checkbox, and then checks with the if condition if it is selected or not. Here you can find more information about this jQuery selector. jQuery is such a powerful tool!!
For get all seleccions of a Grid yii provides the follow code
var seleccions = ($.fn.yiiGridView.getSelection('id_my_cgridview'));
this return an array with the values of checkboxes selected

Categories