In my base twig file I'm rendering my footer. In the footer these is an registration form for subscripting to the newsletter. This is de render call in the base twig.
{{ render(controller('MyBundle:Global:footer')) }}
Via this footer controller I'm rendering the footer. Hereby the controller code:
/**
* #Route("/{_locale}/newsletter/", defaults={"_locale": "nl"}, requirements={"_locale": "nl|en|de"}, name="_newsletter")
*/
public function footerAction(Request $request)
{
$form = $this->createForm(new NewsletterType());
$form->handleRequest($request);
if ($form->isValid()) {
return $this->redirectToRoute('_404');
} else {
return $this->render('MyBundle:global:footer.html.twig', array('form' => $form->createView()));
}
}
If I submit the form what only a email input is and an submit button then this route is triggered, only the form is not validated. In this example for test I want to redirect it to the 404 page. But it just reders the footer only?
I assume that you render this form on many pages because you place it in the footer. As any form you need an action attribute in the form element e.g
<form method="post" action="somewhere">
And the value of the action attribute is where your data will arrive if someone hits the Send button.
My Solution is to add an extra page that shows the same form. Like you are used to with Symfony and than render the same form in the footer and be sure that your form submits to the new page with the same form.
e.g.
$form->setAction($this->generateUrl('target_route'))
Now if anybody submits the form it will be sent to the page with the same form and if there are any errors he will see them on this page.
Related
First page has form using method POST with action to the offer page.
<form id="value-form" method="POST" action="/offer/">
When user submits form on the first page, I want on the offer page class="add-info" to be added to the <form so then it fires up the jQuery script
<form id="formOffer" class="add-info">
Jquery
if (formOffer.hasClass('add-info')) {
launchrocket();
}
I can't figure out class tag can be added via PHP to the new form on the process page.
My template is showing image but after submission the form it shows always bad code error.
Any suggestion
My controller
$form = $this->createFormBuilder()
->add('captcha', 'captcha')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
// some activity
} else {
echo 'captcha error';
}
I met this issue too. And I resolve this by use a new action to handle the form submission, not use the same action that create the form and the captcha.
Maybe if you use the same action to create the form and handle the form submission, it will regenerate the captcha code when you submit the form, as the result the captcha field is stale and not validate still.
I would like to create popup contact form with validation like i did here http://89.212.111.174/delovtujini.si and click “VPIS V BAZO”.
You will get popup where you can fill contact form. How can i do this with CI? Here on this example i do everything in the same html page. In CI i try to create new controller for contat form but i dno’t know how to open the window. I also try to use http://fancyapps.com/fancybox/ I try. but none solution works.
Can someone explain me how to do? Maybe is better to use https://github.com/EllisLab/CodeIgniter/wiki/Ajax-Framework-For-CodeIgniter
Thx
there are 3 ways to trackle your issue.
1) Use custom inline lightbox like what is done on
http://89.212.111.174/delovtujini.si
First post the form back to the same page like below:
public function sign_up()
{
// Setup form validation
$this->form_validation->set_rules(array(
//...do stuff...
));
// Run form validation
if ($this->form_validation->run())
{
//...do stuff...
redirect('');
}
// Load view
$this->load->view('my_form');
}
In the view when you detect a POST you must have javascript
to "open" the lightbox on page load since it won't be display by default (i.e. when you load the page normally the lightbox is "closed" and it is "opened" only when the button is clicked.)
2) Use a iframe lightbox
create the form on a separate CI controller/view and display in within an iframe when the button is clicked.
when the form is submitted you can then call javascript to close the lightbox.
3) Use ajax
both inline and iframe lightbox can work with an ajax form
the idea the same as using an iframe lightbox. Once the form is submitted via ajax, use javascript to close the lightbox.
Whats the best way to handle the following situation in codeigniter:
Home controller has an index action and a submit action.
The submit action is used for a form submission. I want to load the page through the index controller though - including after form submission i.e. with form errors data to repopulate form inputs on error etc.
Whats the best way to do this - without having the index controller handle the main page loading and the form submission.
If you redirect(), you won't be able to use set_value() for re-populating your form fields.
What's easiest is having your index controller handle both the default load behavior, and the submission.
function index()
{
if($this->input->post('foo'))
{ // something was POSTed
$this->load->library('form_validation');
//validation rules
} else
{ // normal view
//
}
$this->load->view('home');
}
Alternatively, you can just set up your index and submit controller, and have them point at the same view, which detects if validation_errors() are set and re-populates form fields accordingly.
Third option (hackish): you could use flashdata to keep submission errors and the submitted form values across a redirect back to index. Something like this would work:
$this->session->set_flashdata('errors', $validation_errors());
Simply point your form at your submit action:
<form method="post" action="/home/submit">
Then, use the submit action to validate your input before redirecting back to the index action.
In a custom module, I have the following (to add javascript to a particular form):
function mymodule_form_mynode_node_form_alter(&$form, $form_state) {
drupal_add_js(drupal_get_path('module', 'mymodule') . '/js/mymodule.js', 'module');
}
This function does get called the form is first loaded (i.e. browse to http://myserver.com/node/add/mynode) however this php function does not get called when the same form is reloaded after the form has been invalidated (i.e. missed a required field after clicking 'Submit' or 'Preview').
What do I need to do to have the javascript file added after 'Submit' or 'Preview' is clicked?
Thanks,
John
To solve your problem, you'll need to create a theme function for the form where you render the form and add the js file.
You form alter is only called when the form is build. As forms are cached between request, the same and already build form is used on submit and preview.
You need to unsure that your code is called each time the form is rendered. As pointed out by googletorp, one way to do it is to use a custom form rendering function. You can also do it by attaching a post or pre-rendering function to your form. Actually, the custom, pre or post rendering function doesn't need to be for the form itself and can be used on any form element. Preferably the one which behavior is altered by the JavaScript.