Problem with Auto Complete Concept in Symfony - php

I am working on a Symfony application that works mainly with form submissions and our team needed extreme customisation on validations therefore we chose to not use the forms helper and widgets. Now each form has an action method for submission and retrieval. In between one of those forms I need an auto fill but I don't know how I should send this to a new action method on the keyup event and return data from there. I am completely unable to return the data from there I have checked using Firebug. I have found articles that submit the whole form for a search auto fill. I can't do that because my auto fill field is not the only one on the form. It's a city field which I have to give suggestions on. I am expected to use the jQuery framework. Please help.
Moreover I don't understand the AJAX concept in Symfony very well. It would be really helpful if you could link me to some good articles.
Appreciate all the help.
Thanks a lot.

The main difference of response in Symfony for ajax request is the action wouldn't decorate the view (actionSucces.php) with the Layout (so it won't include the web debug toolbar either).
In your case, you can use the jQuery Autocomplete plugin (http://docs.jquery.com/Plugins/autocomplete) to produce your extreme customisation form :D
you can use slot to define your jQuery object definition in layout
<head>
<?php if (has_slot('head_script')): ?>
<?php include_slot('head_script') ?>
<?php endif ?>
</head>
define your jQuery Autocomplete in the extreme customisation form template
<?php slot('head_script') ?>
<?php echo javascript_tag('$(function{
$("#example").autocomplete("'.url_for('search/cities').'");
});') ?>
<?php echo form_tag('form/result') ?>
<?php echo tag('input', array('name'=>'example','id'=>'example'));
</form>
don't forget to include your jQuery and Autocomplete plugins, when user start typing a request sent to specified backend with a GET parameter q that contains the current value of the input box and a parameter "limit" with the value specified for the max option. (please refer to the doc :D )
now create action search/cities and use $request->getParameter('q') to populate the response
public function executeCities(sfWebRequest $request){
//process the $request->getParameter('q')
return $this->renderText("First Second Third Fourth");
}
cheers and happy new year :D

Related

OctoberCMS using Ajax API with no-script fallback in page php section

My current base for a simple contact form is:
A page containing the form and the form validation in the php section with a native function like so:
function onStart(){if(Request::isMethod('post')) /* do stuff */}
Above is my "no-script" solution.
Now, when I want to enable OctoberCMS' Javascript API I have to call a self defined function in the php section, for example:
function onSubmit(){/* do stuff */}
How could I combine both in one function so that /* do stuff */ triggers, regardless if the request was send via Ajax or pure php?
I found the answer.
OctoberCMS provides a native method to let a form trigger a custom onMyfunction(), just by using another way of implementing the form.
For anybody who might get into the same problem, here's the code:
<?php
function onMyfunction(){}
?>
==
<html>
{{ form_open({ request: 'onMyfunction', id: 'my-form' }) }}
</html>
This will send the post data to the php handler, regardless if Javascript is enabled or not.
Now you only need to initiate your Ajax API with:
<script>
$('#my-form').submit(function(evt){
evt.preventDefault();
$(this).request('onMyfunction',{})
});
</script>
Could you use the html <noscript> tag?? In your css you would put .noscript {display:none;} .script {display:block;} then in your pages, component template, layout etc you could have two forms that change depending on if the user is using script or not. They can use the same onSubmit function.
Any user interaction with any of the sites I have made assume some might not use Javascript so I default to use standard forms and not js or ajax.

Submit form to different action with typo3 flow

I have a page with a form for creating users. A user has an hobby, which can be created on the same page by clicking on the second button which opens the page for creating a hobby. After creating the hobby, the previous user form should be shown with the user input inserted before going to the hobby page.
Is there a way to do something like with typo3 flow / fluid without using AJAX?
I tried to submit the input to a different action by clicking on the createHobby button --> The action redirects to the new hobby page, where the user can create the hobby and after creation it should redirect back to the user form with the already filled out input fields by the user .
I used...
<input type='submit' value='Create' formaction='/hobby/create' />`
to achive this, but it seems there are some problems with the uris... I get following error:
#1301610453: Could not resolve a route and its corresponding URI for the given parameters.
I think the using the attribute formaction is not a good solution for every case, as it is not supported by IE < 10 as you can see here. I think a JavaScript backport should also be considered (dynamically change the action attribute of the form when clicking on the second button, before actually submitting the form).
Concerning your error, you should not – and probably never – use direct HTML input, instead try to focus on Fluid ViewHelpers, which allow TYPO3 to create the correct HTML input.
Try this instead:
<f:form.submit value="Create" additionalAttributes="{formaction: '{f:uri.action(controller: \'hobby\', action: \'create\')}'}" />
You can make an $this->forward(...) in an initializeActiondepending on an param of your action.
Lets imagine your default Form action is "create". So you need an initializeCreateAction:
public function initializeCreateAction()
{
if ($this->arguments->hasArgument('createHobby')) {
$createHobby = $this->request->getArgument('createHobby');
if ($createHobby) {
$this->forward('create', 'Hobby', NULL, $this->request->getArguments());
}
}
}
Now you must name your input createHobby and assign your createAction this param:
In fluid:
<f:form.button type="submit" name="createHobby" value="1">Create Hobby</f:form.button>
In your Controller:
public function createAction($formData, $createHobby = false)
{
...
}
can you explain something more ... what you show has nothing to do with typo3, I don't know where you inserted that, what version of typo3 , using any extension extra ?

Codeigniter PHP - loading a view at an anchor point

I have a form at the bottom of a long page, if a user fills out the form but it doesn't validate the page is reloaded in the typical codeigniter fashion:
$this->load->view('template',$data);
however because the form is way down at the bottom of the page I need the page to load down there like you do with HTML anchors. Does anyone know how to do this in codeigniter?
I can't use the codeigniter
redirect();
function because it loses the object and the validation errors are gone. Other frameworks I've used like Yii you can call the redirect function like:
$this->redirect();
which solves the problem because you keep the object. I've tried using:
$this->index()
within the controller which works fine as a redirect but the validation errors are in another method which is where the current page is loaded from:
$this->item($labs)
but when I use this it get stuck in a loop
Any ideas? I've seen this question a lot on the net but no clear answers. I'm researching using codeigniter "flash data" but think it's a bit overkill.
cheers.
I can't personally vouch for this, but according to this thread if you append the anchor to the form's action, it will work.
CodeIgniter helper:
<?php echo form_open('controller/function#anchor'); ?>
Or vanilla HTML:
<form method='post' action='controller/function#anchor'>
If you were open to using Javascript, you could easily detect a $validation_failed variable and appropriately scroll. Or, even better, use AJAX.
Another option is to put the form near the top of the page?
Ok, as far as I understood your problem, it isn't much related to the back end(codeigniter). You want the form at the bottom of the page to be 'what-users-sees-on-page-load' (since you mention anchors).
Now, what you can do is, you can set delimiters for your validation error messages using:
echo validation_errors('<div id="bottom_form_error">', '</div>');
Using jQuery ScrollTo, do:
$( function() { $('#bottom_form_error').ScrollTo(); } );
And, the user will be scrolled to the errors at the bottom of the page. Don't forget to include jQuery too.
Anchor hash fragment click is different - it is scrolling at ∞ speed.
I hope that is what you wanted.
P.S. I am ignoring what you said below this line:
Does anyone know how to do this in codeigniter?
as I felt it is not really relevant to the question.

What hook to listen to a form submit?

I'm writing a module that acts on another module. The other module's submit form is at admin/settings/image-toolkit. When its form is submitted, my module needs to respond to that event.
What hook do I need to listen for and how do I know the name of the form?
I'm not even sure where to print dsm in this case to get more information about this form. Is there something like hook_nodeapi but for forms that I could give me more info about the form?
All forms come with a $form[#submit] property that describes what functions are run when the form submits. The default is formname_submit, of course, but you just need to add new ones to that array.
So, you should use hook_form_alter and add another item to the $form['#submit'] array.
You can get the form id easily using the Devel module, or by looking for in the HTML of the pages themselves. (Hyphens should be translated to underscores if you take the latter route)
I get system_image_toolkit_settings for that form on my installations, but that might be dependent on which image library you're using (I use GD).
Though, I'll admit I'm scratching my head a bit about what submit handlers you're wanting to add to that one ;p
Edit:
Some sample code in reply to OP's comment:
What you're basically looking for is this: (from http://drupal.org/node/144132)
function my_module_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'my_form') {
$form['#submit'][] = 'my_additional_submit_handler';
}
}
Of course, you'll need to follow that up with function my_additional_submit_handler in your custom module for anything to happen.

Add and remove form fields in Cakephp

Im looking for a way to have a form in cakephp that the user can add and remove form fields before submitting, After having a look around and asking on the cake IRC the answer seems to be to use Jquery but after hours of looking around i cannot work out how to do it.
The one example i have of this in cake i found at - http://www.mail-archive.com/cake-php#googlegroups.com/msg61061.html but after my best efforts i cannot get this code to work correctly ( i think its calling controllers / models that the doesn't list in the example)
I also found a straight jquery example (http://mohdshaiful.wordpress.com/2007/05/31/form-elements-generation-using-jquery/) which does what i would like my form to do but i cannot work out how to use the cakephp form helper with it to get it working correctly and to get the naming correct. (obviously the $form helper is php so i cant generate anything with that after the browser has loaded).
I an new to cake and have never used jQuery and i am absolutely stumped with how to do this so if anyone has a cakephp example they have working or can point me in the right direction of what i need to complete this it would be very much appreciated.
Thanks in advance
I would take the straight jquery route, personally. I suppose you could have PHP generate the code for jquery to insert (that way you could use the form helper), but it adds complexity without gaining anything.
Since the form helper just generates html, take a look at the html you want generated. Suppose you want something to "add another field", that when clicked, will add another field in the html. Your html to be added will be something like:
<input type="text" name="data[User][field][0]" />
Now, to use jquery to insert it, I'd do something like binding the function add_field to the click event on the link.
$(document).ready( function() {
$("#link_id").click( 'add_field' );
var field_count = 1;
} );
function add_field()
{
var f = $("#div_addfield");
f.append( '<input type="text" name="data[User][field][' + field_count + ']" />' );
field_count++;
}
Of course, if a user leaves this page w/o submitting and returns, they lose their progress, but I think this is about the basics of what you're trying to accomplish.
This was my approach to remove elements:
In the view, I had this:
echo $form->input('extrapicture1uploaddeleted', array('value' => 0));
The logic I followed was that value 0 meant, not deleted yet, and value 1 meant deleted, following a boolean logic.
That was a regular input element but with CSS I used the 'display: none' property because I did not want users to see that in the form. Then what I did was that then users clicked the "Delete" button to remove an input element to upload a picture, there was a confirmation message, and when confirming, the value of the input element hidden with CSS would change from 0 to 1:
$("#deleteextrapicture1").click(
function() {
if (confirm('Do you want to delete this picture?')) {
$('#extrapicture1upload').hide();
// This is for an input element that contains a boolean value where 0 means not deleted, and 1 means deleted.
$('#DealExtrapicture1uploaddeleted').attr('value', '1');
}
// This is used so that the link does not attempt to take users to another URL when clicked.
return false;
}
);
In the controller, the condition $this->data['Deal']['extrapicture1uploaddeleted']!='1' means that extra picture 1 has not been deleted (deleting the upload button with JavaScript). $this->data['Deal']['extrapicture1uploaddeleted']=='1' means that the picture was deleted.
I tried to use an input hidden element and change its value with JavaScript the way I explained above, but I was getting a blackhole error from CakePHP Security. Apparently it was not allowing me to change the value of input elements with JavaScript and then submit the form. But when I used regular input elements (not hidden), I could change their values with JavaScript and submit the form without problems. My approach was to use regular input elements and hide them with CSS, since using input hidden elements was throwing the blackhole error when changing their values with JavaScript and then submitting the form.
Hopefully the way I did it could give some light as a possible approach to remove form fields in CakePHP using JavaScript.

Categories