Symfony 2.3 form is not validating Gregwar captcha code - php

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.

Related

Symfony2 form validation not in sub rendered twig

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.

Codeigniter controller prevent entire page from reloading when form is validating

Basically, I have my controller function for adding in my case a page but let’s focus on the function:
public function add() {
$this->session->unset_userdata('postID');
if ($this->form_validation->run() == TRUE) {
// Form Validation
}
$this->data['subview'] = 'blah blah';
$this->load->view('blah blah.php', $this->data);
}
Basically when the form is submitted it will still unset the postID in this case, however i want to ensure that if the form is submitted and there are errors that this is missed and it doesn’t redo some of my functions and variables. This is happening for a lot of my content when the form is submitted it re-initiates variables that i want to be ignored.
I also tried the following but it didn’t work either:
if (!$this->form_validation->run()) {
$this->session->unset_userdata('postID');
}
How do i avoid the entire page being redone when the form is validating as it re-performs all the page load content?
Thanks
You can check if the submit button was pushed by using
if($this->input->post('the-name-of-the-submit-button')) {
$this->session->unset_userdata('postID');
}
Basically, $this->form_validation->run() checks if the form was submitted and if it passed validation rules, whereas the above simply checks if the form was submitted at all.
You need to do an ajax submission if you don't want the page to refresh:
http://phpsblog.agustinvillalba.com/sending-forms-ajax-codeigniter/
Alternatively, you can fill in the field values so they don't "RESET", ie <input value="<?php echo $_POST['value'];?>" />, but ajax is a much better solution.
You can return errors in JSON from your codeigniter controller and display them on your page.
If you do any type of request with your browser, the page is always going to refresh, unless you use ajax/javascript.

codeigniter print form specific validation error

I have 2 forms on a page eg: 'create_user' and 'update_user' i want to show validation errors specific to each form just above the form. i mean, i want to show validation errors for 'create_user' form just above it and same for 'update_user'.
Right now if i submit update, form errors are showing above both of the forms.
is there something like
<? echo validation_errors("create_user"); ?>
You could do something like:
//pass validation_errors as data to your view from your controller, like
//in your controller
if ($this->form_validation->run()) { //form create_user form
//process
}
else {
$data['create_user_form_errors'] = validation_errors();
}
//and for second form update_user
if ($this->form_validation->run()) { //form create_user form
//process
}
else {
$data['update_user_form_errors'] = validation_errors();
}
//then in your view
if (isset($create_user_form_errors)) echo $create_user_form_errors; //show above first form
//and
if (isset($update_user_form_errors)) echo $update_user_form_errors; //show above second form
Something like that should work for showing errors based on multiple forms. Hope it helps
Simplest way is this:
Pass some sort of form-identifying hidden field with each form
Pass that same hidden field to the view error state, and use a basic IF condition to run validation_errors() in the correct location
You could probably extend the validation class somehow to do what you're asking, but it would be a very specific implementation, and hardly useful in a framework (which is why CI doesn't "know how" to do this by default). The added complexity of working with the extended class would likely counteract any benefit of not simply using basic logic in the view.

Codeigniter - handle form submission from different controller action

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.

CodeIgniter: form submitted with <enter> fails form validation

I have this problem with CodeIgniter:
- when I click the submit button in a form, the form is submitted and validated correctly
- when I don't click the submit button, just hit <enter>, the form validation always fails
Any solution? Is that a flaw in CI's form validation or am I missing something?
What code I have there:
--- the form view ---
form_open("/");
...some inputs...
echo form_submit('submit', 'Přihlásit');
form_close();
...
--- the controller ---
$this->CI->load->helper('form');
$this->CI->load->library('form_validation');
$this->CI->form_validation->set_rules('id_uziv', 'ID', 'required');
$this->CI->form_validation->set_rules('heslo', 'Heslo', 'required');
//... see, no rules have anyhting to do with the submit button
if ($this->CI->form_validation->run() == FALSE) {
// validation OK
}
else {
// validation failed
}
you're not echoing form_open('/')
your form elements will not be enclosed within a <form> element so when you submit, no post data will be sent to the server resulting in your validation failing.
I solved it by adding an empty validation rule for the submit button, like this:
$this->CI->form_validation->set_rules('submit_button', 'Submit', '');
Now it works. I don't really know what was causing the problem.

Categories