Silverstripe form not showing error or success - php

I have the following form,
public function ContactForm()
{
$fields = FieldList::create(
TextField::create('Nombre', 'Tu nombre')
->setCustomValidationMessage('Por favor coloca tu nombre.')
);
$actions = FieldList::create(
FormAction::create('sendContactForm', 'Send')
);
$required = [
'Nombre',
];
$validator = new RequiredFields($required);
$form = Form::create($this, 'ContactForm', $fields, $actions, $validator);
// I'm using this novalidate for testing
$form->setAttribute('novalidate', 'novalidate');
$form->setFormMethod('POST', true);
return $form;
}
And it's not giving me errors, nor success message on submit.
If I complete the field with a string, the form submits successfully and saves my data to a model, and sends an email as well, but I get no success message.
I have another page type with another form defined the same way and it works fine, I don't know why this one doesn't show me errors.
If I leave the field empty, the sendContactForm function is not called, the page reloads with a clean form and no errors. This makes me think the validation is working correctly because it doesn't reach the submit function, but the fact the form is coming back empty even if I add data to some fields is very strange to me, because the other form comes back with the errors and with the data previously entered.
I've also tried with no JS or CSS to make sure is not a front end interference.
They are both shown in the template using $ContactForm and $MessageForm respectively.
For reference, here are some links to the full files used for both forms/pages.
Contact page
Contact page controller
Working fine form
Working fine form controller
This has been driving me nuts for hours, and I have no idea what could be wrong.

Related

Laravel redirect code 302 when submit form

I faced an issue where Laravel redirects me back to my form page after submitting the form.
I was trying to return my $request data to make sure I got the correct values.
But I ended up getting redirected back to the form.
Controller:
// ServiceController.php
public function saveServiceProgress(Request $request, $id)
{
$service = Service::find($id);
$rules = $this->generateCustomRules($service);
$request->validate($rules);
$input = $request->all();
return $request;
// echo $input;
// return view('services.edit', compact('service'));
}
The form is a huge form with switch case, so I only show the form tag:
{!! Form::open(['route' => ['services.saveServiceProgress', 'id' => $service->id],'method'=>'POST', 'files'=>'true']) !!}
The form consists for dynamic input based on the id passed into the controller. It has text inputs and file inputs as well. So its a very complex form. Currently I chose the simplest version of the form to test the functionalities.
Please shed some light (and probably some code) to help me with my problems! Thanks a lot!!!
Update on route:
Route::post('services/submit/{id}', [ServiceController::class, 'saveServiceProgress'])->name('services.saveServiceProgress');
Update after fixing validator:
Managed to submit the form, now starts working on handling file upload. Thanks #Rwd and #mahmoud\ magdy for pointing out my validation issues!
use Validator::make to handle if yours validation if fails with custom errors response
see this link https://laravel.com/docs/8.x/validation#manually-creating-validators

In Slim, how do I pass a GET variable to render

I've only been working with Slim for a short while. I'm still on version 2. So far everything has been going just fine, but I've hit a little snag. I have a page that displays content based on a GET variable at the end of the URL. The url looks like the following...
http://localhost/trailcache.com/checklist/21
The first line of the get route looks like this...
$app->get('/checklist/:Id', function($Id) use($app) {
It ends like this...
})->name('checklist');
That Id parameter controls what info I'm pulling into the page and everything has been going just fine, but now I've added a contact form and with it, some validation. I'm writing to the DB and rendering the new content okay. The problem arises when I try to send errors back to the page. Currently it looks like this...
$app->render('user/checklist.php', [
'Id' => $Id,
'errors' => $v->errors(),
'request' => $request
])->name('checklist');
This doesn't work. The page is blank. The url it returns is...
http://localhost/trailcache.com/checklist
The documentation for render shows...
$app->get('/books/:id', function ($id) use ($app) {
$app->render('myTemplate.php', array('id' => $id));
});
Wouldn't that work in the post route the same way? It has on all my other pages.
How can it get pass those errors along with the correct GET variable so that the correct content displays?
This works for me just fine.
return $this->renderer->render('myTemplate.php', array('id' => $id));
Hope it works for you.
After a few days of struggling with this, I found a solution. I first discovered map() and changed the first line to...
$app->map('/checklist/:Id', function($Id) use($app) {
and the end to...
})->via('GET', 'POST')->name('checklist');
so now the route renders the same whether it's coming from post or get. I then pointed the form to this named route...
<form action="{{ urlFor('checklist', {Id: Id}) }}" class="form" method="post">
and moved all the post logic inside this route. Since there is no need to validate if the request wasn't a post I used...
if($app->request->isPost()) {
and put the validation code inside. Refreshing the page after making a form submission would resubmit the form so if validation passed I added...
return $app->response->redirect($app->urlFor('checklist', array(
'Id' => $Id
)));
All I had to do was pass the errors to the template and it seems to be working perfectly.

Initializing the php form created with Formtools.org

I'm working on a form with Form tools http://www.formtools.org
My task is to create php multi-page form, using their API. And it should also save data after every step, not only after the last one.
But I already have a problem while initializing the form into the system.
I have all pages built, and when I'm doing test submission it works good. I successfully see my 'Thank you page'.
Following is the code from my 1st page
<?php
require_once("/app-administration/global/api/api.php");
$fields = ft_api_init_form_page("", "test");
$params = array(
"submit_button" => "submit",
"next_page" => "lc_2.php",
"form_data" => $_POST
);
ft_api_process_form($params);
?>
Then, due to the tutorial, I go to the admin panned, create new form there. And then, I need to initialize it.
So, I'm changing this line:
$fields = ft_api_init_form_page(12, "initialize");
But then, when I try to submit the form I get 100 error on the last page:
http://docs.formtools.org/api/index.php?page=error_codes#100
It says there is something with ID, but 12 is correct one because I have it from admin panel, and I double-checked it there.
Anybody have any ideas why I might have this error? Any help will be appreciated.
Thanks

How to use ExpressionEngine form validation class in module to repopulate form in template?

Is it possible to display errors and repopulate fields on a form that is hard coded into a template? So far I’ve only figured out how to display the errors in a module’s view, but not a template.
Based on the logic of ExpressionEngine, I’m guessing I need to somehow make the validation errors visible through a tag in my module, or even generate the whole form from the module, but I’m not sure how to best approach this.
Here is what I have right now.
function submit_form()
{
$this->EE->load->helper('form');
$this->EE->load->library('form_validation');
$this->EE->form_validation->set_rules('first_name', 'First Name', 'required');
$this->EE->form_validation->set_rules('last_name', 'Last Name', 'required');
$this->EE->form_validation->set_rules('address', 'Address', 'required');
$this->EE->form_validation->set_rules('city', 'City', 'required');
$this->EE->form_validation->set_rules('province', 'Province', 'required');
if ($this->EE->form_validation->run() == FALSE)
{
return $this->EE->load->view('form_errors');
}
else
{
// success
}
}
And for testing, the view simply contains:
echo validation_errors();
Can anyone help?
Great question, and one which took me a long time to figure out the best way to solve.
The CodeIgniter Form Validation library is great, but can only be used with proper views and controllers, so it doesn't work out of the box when you are developing a front end tag.
Normally, the preferred way of submitting a front-end form is to register an 'Action' in your upd.addon.php file (I'm guessing you have done this for your submit_form() function). This is then assigned a number, which you can post with the url /index.php?ACT=37 or something similar. This is a good system, because it means we know the form submission came from our module. However, with input forms, this is a hindrance, because it means we can't repopulate input fields. You therefore need to configure the input form to post back to the current URL, and wait until the template engine tries to render your tag before handling the form submission.
The easiest, and visually ugliest way to achieve this, is to use $this->EE->output->show_user_error(FALSE, array_of_errors). You can actually use this from either an action, or within your module code. It displays the standard grey EE message page we have all grown to know and hate.
With that sort of intro, you probably knew the solution wasn't going to be quite that simple, right? Here's a skeleton of a tag function which implements inline error checking:
function my_form()
{
// load default tag variables
$tag_vars = array();
$tag_vars[0] = array(
'first_name' => '',
'error:first_name' => '',
'last_name' => '',
'error:last_name' => ''
);
// handle a form submission
if ($this->EE->input->post('my_form_hidden') == '1'))
{
// load POST data into tag
$tag_vars[0]['first_name'] = $this->EE->input->post('first_name', TRUE);
$tag_vars[0]['last_name'] = $this->EE->input->post('last_name', TRUE);
// use CI validation library to check submission
$this->EE->load->helper('form');
$this->EE->load->library('form_validation');
$this->EE->form_validation->set_rules('first_name', 'lang:first_name', 'required');
$this->EE->form_validation->set_rules('last_name', 'lang:first_name', 'required');
$valid_form = $this->EE->form_validation->run();
if ($valid_form)
{
// probably save something to database, then redirect
}
else
{
$form_errors = array();
foreach (array('first_name', 'last_name') as $field_name)
{
$field_error = form_error($field_name);
if ($field_error)
{
$form_errors[] = $field_error;
$tag_vars[0]['error:'.$field_name] = $field_error;
}
}
if ($this->EE->TMPL->fetch_param('error_handling') != 'inline')
{
// show default EE error page
return $this->EE->output->show_user_error(FALSE, $form_errors);
}
}
}
// parse and output tagdata
$out = $this->EE->functions->form_declaration(array(
'action' => $this->EE->functions->fetch_current_uri(),
'hidden_fields' => array('my_form_hidden')));
$out .= $this->EE->TMPL->parse_variables($tagdata, $tag_vars);
return $out.'</form>';
}
This way, the designer can specify error_handling="inline" in the tag if they want inline errors, otherwise they will just be redirected to the standard error form. If they do request inline error handling, they will simply need to make sure their inputs look like this:
<input type="text" name="first_name" value="{first_name}" />
{error:first_name}
Note the hidden field we submit along with the form, this allows us to ensure we only handle submission of this form, and not any other forms on the page, such as a login form or something. If you have more than one instance of your form on a page (say inside a channel entries loop or something), you will need to implement some trickery to make sure that you only handle the form instance which was submitted, for example by submitting along entry_id as a hidden field too.
Glad to get that all documented, hopefully this will be useful to other EE developers too!

Populating posting.php message field from form submission on phpBB3 forum

I'm trying to add a form button that will take a variable string and insert it into $_POST['message'], such that when someone presses my 'post this on forum' button it takes them to the new topic page with my variable string already in the message textarea.
I've been messing with submit_post and have a form that submits a new post correctly when it's completed, however I don't want it to submit straight away; all I want is for it to load posting.php with my string already in the message field. Does anyone have any ideas?
You might have to modify the source of phpBB3 in order to do this. Unless posting.php is programmed to accept data from $_POST and insert it into the message textarea, you'll have to program it to do so.
As an alternative, you could try doing this with JavaScript: You could pass the text to posting.php in a cookie or a session variable which then displays in a hidden div or textarea or some other means (I'd need more specific information about your environment to provide specifics) and then insert that text into the textarea using JavaScript after the page loads. This should be more upgrade safe, but obviously requires users to have JavaScript enabled.
I found an alteration you can make to posting.php to let it accept get parameters
http://www.phpbb.com/community/viewtopic.php?f=46&t=2119831
Here it is for the sake of completion:
Find the following in posting.php:
if ($submit || $preview || $refresh)
Add the following on a line before it:
if( !$submit&& !$preview&& !$refresh&& !$save&& !$load&& !$delete&& !$cancel&& ( $mode== 'post'|| $mode== 'reply'|| $mode== 'quote' ) ) {
$post_data['post_subject']= utf8_normalize_nfc( request_var( 'subject', '', TRUE ) );
$message_parser->message= utf8_normalize_nfc( request_var( 'message', '', TRUE ) );
Voila! Test it with http://yourdomain/forum/posting.php?mode=post&f=2&subject=hello&message=world

Categories