When I submitted the form with the smyfony4 app, I got the following error.
Since it is an error of the entire form, it is not possible to identify the cause.
I want to debug the value of the submitted form.
What should I do?
I'm sorry for the question like a beginner.
Error
shop => Not a valid value.
{{ form_start(form) }}
{{ #The part that issued the error #}}
{{ form_errors(form) }}
<div class='formGroup'>
{{ form_label(form.tel, 'TEL') }}
{{ form_widget(form.tel) }}
{{ form_errors(form.tel) }}
</div>
.
.
.
{{ form_end(form) }}
$form = $this->createForm(ShopType::class, $shop, array(
"method" => "PUT",
"action" => $this->generateUrl("app_shop_shop_update"),
"em" => $this->getDoctrine()->getManager(),
));
if ($request->isMethod('PUT')) {
if ($form->handleRequest($request)->isValid()) {
// save
$this->get("admin.shopService")->save($shop);
$this->get('session')->getFlashBag()->add('success', 'I saved my shop profile.');
return $this->redirect($this->generateUrl('ahi_sp_admin_shop_shop_edit'));
} else {
$this->get('session')->getFlashBag()->add('error', 'The shop profile could not be saved. Please check the input contents.');
}
}
The function getData()
if ($form->isSubmitted() && $form->isValid()) {
dump ($form->getData());
}
Symfony's developer tools were showing the value I was passing when saving, which was the cause of the error.
When saving the basic data of the shop, validation did not work well, and when I tried to update it again, an error occurred with the basic data that is not in the form, so an error message for each error of the form item It seems that there was no display of.
Related
I have a problem with form.
For start, in my main twig I include the twig i use for the form:
{% include 'MainBundle::manage.html.twig'%}
The controller is mainController
The controller displays everything that appears in main twig.
The action of the form :
public function manageAction(Request $request){
$manageForm = $this->createFormBuilder()
->add('submitFile', FileType::class, array('label' => 'File to Submit'))
->add('send', SubmitType::class)->getForm();
if ($request->getMethod('post') == 'POST') {
// Bind request to the form
$manageForm->bindRequest($request);
// If form is valid
if ($manageForm->isValid()) {
// Get file
$file = $manageForm->get('submitFile');
$file->getData();
}
}
return $this->render('MainBundle::main.html.twig', array(
'manageForm' => $manageForm->createView()
));
}
routing.yml
manage:
path: /manage
defaults:
_controller: MainBundle:Main:manage
requirements:
_method: POST
manage.html.twig
{% extends 'MainBundle::main.html.twig' %}
{% block content %}
<form action="" method="post">
{{ form_widget(manageForm) }}
<input type="submit" />
</form>
{% endblock %}
when i run in the route /main I should have my form display but i have an error
Variable "manageForm" does not exist.
I guess the controller does not take the view...
In your particular case - change to following.
return $this->render('MainBundle::manage.html.twig', array(
'manageForm' => $manageForm->createView()
));
If you extend the main/base twig in other twig, then you need to render this other twig (always).
Parameters which you pass to extended twig will be available in twig you have extended (in this case the main.html.twig), but it doesn't work the other way around.
{% include 'MainBundle::manage.html.twig'%} // why ?
Don't do anything with main - just extend it in manage!!!!
I see other problems with your overall code also.
CONTROLLER:
// the more modern way instead of checking manually for request method
if ($form->isSubmitted()) {
if ($form->isValid()) {
// perform actions...
} else {
// was not valid...return error messages
}
}
FORM
<form action="" method="post"> // this is the oldschool way
{{ form_widget(manageForm) }}
<input type="submit" />
</form>
Better something like... (some of my recent coded examples)
{{ form_start(form, {'attr' : {'method' : 'post', 'enctype' : 'multipart/form-data', 'class' : 'test'}}) }}
{{ form_errors(form.resume) }}
{{ form_widget(form.resume, {'attr' : {'id' : 'file-upload-rec', 'class' : 'file-upload-rec js-file-upload-rec', 'accept' : '.pdf, .doc, .docx'}}) }}
{{ form_label(form.resume, 'select', {'label_attr' : {'class' : 'file-upload js-file-upload'}}) }}
{{ form_row(form._token) }}
{{ form_end(form, {'render_rest': false}) }}
ps. you might also want to read all of https://symfony.com/doc/current/forms.html
You are rendering main.html.twig instead of manage.html.twig. I guess the first one uses an importForm variable which is therefore not passed to the view.
If not, and you really need to render main.html.twig which includes manage.html.twig, then why is manage.html.twig extending main.html.twig? Either include one in another, or extend one from another, not both.
You need to pass the variable while including your template
{% include 'MainBundle::manage.html.twig' with {'manageForm': manageForm} %}
For more information see https://twig.symfony.com/doc/2.x/tags/include.html
Thanks!
I have a form controlling the search bar in the websites navbar:
public function searchFormAction()
{
$form = $this->createFormBuilder()
->setAction($this->generateUrl('search'))
->setMethod('GET')
->add("value", TextType::class, array('label' => false))
->add("Search", SubmitType::class)
->getForm();
return $this->render('components/search-form.html.twig', [
"form" => $form->createView()
]);
}
As you can see, the form has a specific action path to this function:
/**
* #Route("/search", name="search")
*/
public function searchAction(Request $request)
{
return $this -> render ("post/post-search.html.twig", [
"value" => $request->query->get('value')
]);
}
For now this shouldn't do much more than just display the value on the page.
The problem is that the website fails to redirect when the form is used
So when I put foo in the search, and click submit the path looks like this:
localhost:8000/page?form%5Bvalue%5D=foo&form%5BSearch%5D=&form%5B_token%5D=PsouIRAy2QaQ8j2XO_uYrs7PcaR6jyjQN3W3_xRMdgw
Moreover if I go to localhost:8000/search and try to put anything into the search bar, no value is printed.
Here is how the form is rendered:
//search-form.html.twig
<form class="navbar-form navbar-left">
{{ form_start(form) }}
<div class="form-group">
{{ form_row(form.value) }}
</div>
{{ form_row(form.Search) }}
{{ form_end(form) }}
</form>
And placed in the base navbar:
//base.html.twig
//...
{{ render(controller(
'AppBundle:Form:searchForm'
)) }}
//...
Inspecting the element shows that the form tag has no action and method attributes
What could be the issue here and how can I fix it?
Fixed! Made a simple mistake in the twig file.
Placed the form start inside html form tags, that way the submit button would send to an empty form.
I have an issue trying to render a controller which returns a template with formView.
I understood about the sub-request, but I am having difficult time to show any kind of errors.
I think the problem is that after it sees the form is invalid it redirectsToRoute and it looses the POST Request.
If I don't say redirectTo it just renders the view.
base.html.twig
{{ render(controller('AppBundle:Utility:renderSignUpWizard'), {request: app.request}) }}
Utility Controller
/**
* #Route("/registration/wizard/", name="registration.wizard")
*/
public function renderSignUpWizardAction(Request $request)
{
/** #var $user User */
$user = $this->getUser();
$form = $this->createForm(SignUpWizardType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
// save changes to user
$this->persistAndSave($user);
// redirect to profile
return $this->redirectToRoute('profile');
}
else if($form->isSubmitted() && !$form->isValid())
{
return $this->redirectToRoute('home');
}
return $this->render('partials/signup-wizard.html.twig', array
(
'form' => $form->createView(),
));
}
If you could show the twig file where you put the form I could have given a clearer answer. Check the twig file you tell your controller to render and add the following:
Simple way to generate your form(doesn't include errors):
{{ form_start(form) }}
{{ form_widget(form) }
{{ form_end(form) }}
Add:
{{ form_errors(form) }}
if you want erors for a specific field:
{{ form_errors(form.name) }}
I have a login/registration system I am looking to build in Laravel. Currently I am having trouble pulling the information from the form and inputting it into the table. When I submit the form it submits, but the data goes nowhere, I am admittedly new to Laravel.
This is how the form is written:
{{ Form::open() }}
#if (Session::get("error"))
{{ Session::get("error") }}<br />
#endif
{{ Form::label("first_name", "First Name") }}
{{ Form::text("first_name") }}
{{ $errors->first("first_name") }}<br />
{{ Form::label("email", "Email") }}
{{ Form::text('email', Input::old('email')) }}
{{ $errors->first("email") }}<br />
{{ Form::label("password", "Password") }}
{{ Form::password("password") }}
{{ $errors->first("password") }}<br />
{{ Form::label("password_confirmation", "Confirm") }}
{{ Form::password("password_confirmation") }}
{{ $errors->first("password_confirmation") }}<br />
{{ Form::submit("register") }}
{{ Form::close() }}
The user.register (part of a larger controller) which this POSTs to and GETs from is as follows:
public function register()
{
return View::make("user/register");
$validation = Validator::make(Input::all(), User::$rules);
if ($validation->fails())
{
return Redirect::to('register')->withErrors($validation)->withInput();
}
$users = new User;
$users->first_name = Input::get('first_name');
$users->email = Input::get('email');
$users->password = Hash::make(Input::get('password'));
if ($users->save())
{
Auth::loginUsingId($users->id);
return Redirect::to('profile');
}
return Redirect::to('register')-withInput();
}
}
Currently when I submit I get no errors simply a blank redirect to the registration page and nothing ends up in my DB. I am wondering if there is something wrong with my paths? The only other page that is similar to this that I have previously worked on (inputting data to a database) is a page to reset passwords (which works great), but that used a slightly different system through the Auth extension of Laravel.
This I am not familiar with. Can someone point me in the right direction? I have been compiling the knowledge I have gained from guides online but keep ending up in the same place!
Thanks a lot in advance,
Anything else you need (models, routes, etc. I just didn't think they were necessary) lemme know!
Change
return Redirect::to('register')-withInput();
to
return Redirect::to('register')->withInput();
edit:
oh - here is the problem:
public function register()
{
return View::make("user/register");
$validation = Validator::make(Input::all(), User::$rules);
...
remove the "return" function - it should be
public function register()
{
$validation = Validator::make(Input::all(), User::$rules);
...
I am trying to post data through Symfony form's button but it does not validate form.
Here is my controller file:
public function PurchaseProductAction(Request $request)
{
$defaultData = array('message' => 'Type your message here');
$form = $this->createFormBuilder($defaultData)
->setMethod('POST')
->add('CompanyName', 'text', array(
'label'=>false
))
->add('Address1', 'text', array(
'label'=>false
))
->add('Continue_to_Step_2', 'submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid())
{
// It does not come here
$data = $form->getData();
$value = $data['CompanyName'];
echo $value;
}
}
Its my twig file:
{% block content %}
Company Name{{ form_row(form.CompanyName) }}
Address Line 1{{ form_row(form.Address1) }}
{{form_widget(form.Continue_to_Step_2)}}
{% endblock %}
Kindly guide me what I am doing wrong due to which my method does not call?
As explained in the Rendering a Form in a Template part of the documentation, you've to include the {{ form_start(form) }} and the {{ form_end(form) }} twig form helpers.
This will generate the appropriate <form> tags according to your form definition.
Also, keep in mind that Support for submit buttons was added in Symfony 2.3. Before that, you had to add buttons to the form's HTML manually.
Update,
form_end should be called with render_rest option set to false if you don't want it to show unrendered fields,
{# don't render unrendered fields #}
{{ form_end(form, {'render_rest': false}) }}