Can I use this Form in symfony 4? - symfony

I have added this form in twig, i need to know if this is okay and how can i recupere the input name="comments" in controller
<form action="{{ path('Update') }}" method="POST">
<input class="form-control" type="text" name="comments"
value=""></td>
<td>
<input type="submit" value="Save"/>
</form>

You can take a look at the Symfony Request Object section of the doc:
// retrieves $_GET and $_POST variables respectively
$request->query->get('id');
$request->request->get('category', 'default category');
So you can retrieve in the controller as:
$request->request->get('comments');

In your controller you can use the Request object to get all the parameters of your form, for example:
/**
* #Route("/Update")
*/
public function update(Request $request){
$comments = $request->request->get('comments');
...
}
But I recommend you to use the forms component.

Related

How to create custom html form with redirect back with success message in silverstripe?

I am new in SilverStripe. I want to create a custom HTML form in SilverStripe.
<form class="form-inline" $HelloForm.FormAttributes>
<p id="{$HelloForm.FormName}_success" class="message" style="">$HelloForm.Message</p>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" id="email" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
</div>
<div class="checkbox">
</div>
$HelloForm.fields
<input type="hidden" value="{$AbsoluteLink}" name="redirectURL" class="action" id="{$HelloForm.FormName}_action_doSayHello"/>
And in my controller
public function HelloForm()
{
$form = Form::create(
$this,
'HelloForm'
);
$actions = new FieldList(
FormAction::create('doSayHello', 'Submit')->setAttribute('class', 'btn btn-success')
);
$form = new Form($this, 'HelloForm',$actions);
return $form;
}
public function doSayHello($data,$form)
{
$form->sessionMessage('thanks for contact us','good');
return $this->redirectBack();
//i am not getting success message after submit
}
Can I get a success message after submitting in this case?
When I use standard SilverStripe form it's working but when using custom HTML form like above I am stuck
You could submit the form via ajax, which would have many advantages.
The page does not have to refresh itself
You can animate the form after submitting, by sliding up or some kind of similar animation.
You can handle form states like success or error
Some example code (in jQuery):
let form = $('.form-inline');
$(form).ajaxSubmit({
success: function() {
$(form).slideUp();
$('#form-state').text("Successfully submitted form.");
}
})

Form POST data handling with WordPress

I have hard time figuring out how to recieve form data. This is how my form looks like:
<form action="register" method="POST">
<input type="hidden" name="action" value="process_form">
<input type="email" placeholder="Enter email" name="email">
<input type="password" placeholder="Enter password" name="password">
</form>
How can I access the data of my form?
You should change your form action to something like this:
action="<?= esc_url(admin_url('admin-post.php')) ?>"
And add a hidden input in your form like this:
<input type="hidden" name="action" value="add_foobar">
And then in your backend class add an action like this:
namespace Class\Namespace;
class ClassName {
public function init() {
add_action( 'admin_post_add_foobar', [$this, 'handleForm'] );
}
public function handleForm() {
// your logic here
// use $_POST to retrieve post data
}
....
Make sure to include your class in your functions.php of your theme, like this:
(new \Class\Namespace\ClassName)->init();
To read form data, then in your handleForm method just use $_POST.
For more examples have a look at this page.
After the form has been submitted, you can access it on the page it loads with the PHP $_POST variable.
eg.
$email = $_POST['email'];
Remember to validate and sanitise this variable as it will be what the user has entered.

Retrieve non-form-element value through form post in Spring 3 MVC Controller Class

When I submit this:
<form:form action='/controller.do' method='POST'>
<input type='text' value='test' name='myName" />
<input type='submit'/>
</form:form>
How can I retrieve the value of 'myName' in Spring 3 MVC Controller without using form tag?
If I understand your question correctly, you put some plain HTML <input> tags inside Spring MVC tag library's <form:form> tag.
Though it may work, it probably would be a better idea to use (depending on your situation) either plain HTML:
<form action='/controller.do' method='POST'>
<input type='text' value='' name='myName" />
<input type='submit'/>
</form>
or Spring MVC tags only:
<form:form action='/controller.do' method='POST' modelAttribute='fooModelAttribute'>
<form:input path='myName' />
<input type='submit'/>
</form>
You can retrieve the myName value by declaring the following parameter in your controller method:
#RequestParam("myName") String myNameVal
Alternatively, you can declare a class which has a myName field with a getter and setter (say, FooClass). And then you can declare the following parameter in the controller:
FooClass foo
This parameter may have an optional #ModelAttribute("fooModelAttribute") annotation.
See the Spring Reference for more details on #RequestParam, #ModelAttribute, and <form:form>.

Get value of a field not declared in FormType

I have a form declared in nameType.php and the view render all field but I want add another field manually.
Form:
<form action="{{ path('create') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="text" value="2">
</form>
And get the values in the controller:
$form->bindRequest($request);
How can I collect the value of the input in the controller?
If you are trying this because the form is linked to your entity field you can add a field to FormType as not mapped. Then you do not need getters and setters on your entity.
->add("inputName", "text", array("mapped"=>false, "data"=>2, "label"=>false))
To get the data in the controller:
$form->get("inputName")->getData();
You can not retrieve the input value from the $form, because it's not part of it.
You have to retrieve it from the request in the Controller by using the name attribute :
HTML : <input type="text" value="2" name"var_name">
Controller: $request->request->get('var_name')
how could collect the value of the input to the controller?
The instant-gratification way would be to use
$form->get('inputName')->getViewData()
for an unmapped field. But I'm sure there are better ways which are Symfony validation-compliant.
After calling $form->bindRequest($request) you can call: $form->getData() to get input from user.
But if you want to receive input data for field that is not mapped you need to use mentioned $request->request->get('field_name').

Symfony2 functional test to select checkboxes

I'm having trouble writing a Symfony 2 functional test to set checkboxes that are part of an array (i.e. a multiple and expanded select widget)
In the documentation the example is
$form['registration[interests]']->select(array('symfony', 'cookies'));
But it doesn't show what html that will work with and it doesn't work with mine. Here is a cutdown version of my form
<form class="proxy" action="/proxy/13/update" method="post" >
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_1" name="niwa_pictbundle_proxytype[chronologyControls][]" value="1" />
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_2" name="niwa_pictbundle_proxytype[chronologyControls][]" value="2" />
<input type="checkbox" id="niwa_pictbundle_proxytype_chronologyControls_3" name="niwa_pictbundle_proxytype[chronologyControls][]" value="3" />
</form>
Once it get it working there I'm going to move on to a manually made form
<input type="checkbox" id="13" name="proxyIDs[]" value="13">
<input type="checkbox" id="14" name="proxyIDs[]" value="14">
<input type="checkbox" id="15" name="proxyIDs[]" value="15">
I have tried things like
$form = $crawler->selectButton('Save')->form();
$form['niwa_pictbundle_proxytype[chronologyControls]']->select(array('3'));
$form['niwa_pictbundle_proxytype[chronologyControls][]']->select(array('3'));
but the first fails saying select is being run on a non-object and the second says Unreachable field "".
Try
$form['niwa_pictbundle_proxytype[chronologyControls]'][0]->tick();
It indexes it from 0 even in the form it says []
Or if it doesn't really helps you, you can try POSTing an array directly to the action instead of using symfony's form selectors. See: Symfony2: Test on ArrayCollection gives "Unreachable field"
Hope one of them helps you.
I think the most bulletproof solution working in 2017 is to extend your test class:
/**
* Find checkbox
*
* #param \Symfony\Component\DomCrawler\Form $form
* #param string $name Field name without trailing '[]'
* #param string $value
*/
protected function findCheckbox($form, $name, $value)
{
foreach ($form->offsetGet($name) as $field) {
$available = $field->availableOptionValues();
if (strval($value) == reset($available)) {
return $field;
}
}
}
And in the test call:
$this->findCheckbox($form, 'niwa_pictbundle_proxytype[chronologyControls]', 3)->tick();

Resources