Symfony 2 - Contact form and validation - oop

I'm having trouble with contact form on Symfony 2.
Simple form
<form id="contact-form" method="post" action="">
<label for="name">Name: </label>
<input type="text" name="name" id="name" class="input" />
<label for="subject">Subject: </label>
<input type="text" name="subject" id="subject" class="input" />
<label for="text">Text: </label>
<textarea name="text" id="text" class="input"></textarea>
</form>
I read the documentation about validations and form, but I didn't really understand what it was about.
I have two questions
where should post values go, I'm talking about action attribute in form.
how can I validate this simple contact form.
The best would be if you guys show me here or link to how to validate this simple form.
Thanks

You can set the action to any path in your routing. Example <form action="{{ path(...) }}">. From the path, there will be redirect to your controller. In your controller, you can validate the form. I will give you example.
<form id="contact-form" method="post" action="{{ path('form_contact') }}">
<label for="name">Name: </label>
<input type="text" name="name" id="name" class="input" />
<label for="subject">Subject: </label>
<input type="text" name="subject" id="subject" class="input" />
<label for="text">Text: </label>
<textarea name="text" id="text" class="input"></textarea>
</form>
From here, this form will go through routing
// in your routing.yml
form_contact:
pattern: contact
defaults:
_controller: YourNewBundle:Test:contact
Now, let's go to controller part
// TestController.php
public function contact(){
$request = $this->getRequest();
if ($request->query->has('name') || $request->query->has('subject') || $request->query->has('text')){
...
}
}
Just customize with your own code.

Related

How to render form on button clic in Vue?

I have two registration forms ona a page, but I only want to render one by clicking on proper button: 'Contract Form' or 'Company Form'. Should I do it with v-if? In the code below I just copied few lines of the form.
<template>
<div>
<button #click="contract">UMOWA</button>
<button #click="company">FIRMA</button>
<div v-if="contract">
<form method="post" #submit.prevent="onSubmit">
<label for="firstname">IMIĘ</label>
<input id="firstname" v-model="firstName" type="text" required />
<label for="lastname">NAZWISKO</label>
<input id="lastname" v-model="lastName" type="text" required />
<button type="submit">Zarejestruj się</button>
</form>
</div>
<div v-if="company">
<form method="post" #submit.prevent="onSubmit">
<label for="firstname">IMIĘ</label>
<input id="firstname" v-model="firstName" type="text" required />
<label for="lastname">NAZWISKO</label>
<button type="submit">Zarejestruj się</button>
<input id="lastname" v-model="lastName" type="text" required />
<label for="email">ADRES E-MAIL</label>
<input id="email" v-model="email" type="text" required />
<button type="submit">Zarejestruj się</button>
</form>
</div>
</div>
</template>
Yes but you will need to add true in the buttons onclick:
<button #click="contract = true">UMOWA</button>
<button #click="company = true">FIRMA</button>

How do I iterate of form elements and then pass those to my controller in ASP.NET Core?

I have a form that has one text input and many checkbox inputs. I'm trying to not only pass the data to my controller without a model but also iterate of the many checkboxes and pass those as an array that can be iterated over and passed into a model at a later stage. Before that, however, I need to grab my form data and pass it to my controller without a model and then work with it from there.
My form code is, shortened for brevity:
<form id="ModalFormAddCustomView" asp-action="AddCustomView" asp-controller="Position">
<input type="text" for="Name" />
<input type="checkbox" class="form-check-input" id="Check1">
<input type="checkbox" class="form-check-input" id="Check2">
<input type="checkbox" class="form-check-input" id="Check3">
<input type="checkbox" class="form-check-input" id="Check4">
</form>
As you can see it's a simple form, posts to a controller action and contains a single text input and lots of checkboxes. In my controller, I opted to use [FromForm] as a string with a hope I could work with the data after it reached the action. content is always null so that approach isn't working.
[HttpPost]
public string AddCustomView([FromForm] string content)
{
return content;
}
Obviously the return type is set to content for the purposes of testing and until I get this working.
My question is, how do I post my form data to my controller and also iterate over my checkboxes in the action once I have done this?
You should have a name attribute for each input, because the model binding is based on name.
View:
<form id="ModalFormAddCustomView" asp-action="AddCustomView" asp-controller="Position">
<input type="text" name="text" />
<br />
<div class="form-check">
<input type="checkbox" class="form-check-input" name="Check" value="A">
<label class="form-check-label" for="exampleCheck1">A</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="Check" value="B">
<label class="form-check-label" for="exampleCheck2">B</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="Check" value="C">
<label class="form-check-label" for="exampleCheck3">C</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="Check" value="D">
<label class="form-check-label" for="exampleCheck4">D</label>
</div>
<input type="submit" value="submit" />
</form>
Controller:
[HttpPost]
public IActionResult AddCustomView([FromForm]string text,[FromForm] string[] Check)
{
return View();
}
Result:

Asp .net MVC 4.0 | how can I post a HTML form using URL.Action to a secure page?

I have a HTML form on a page which is not running on the secure binding but I need to post this form to a secure page. Now issue I am facing is that my form data gets lost when I submit this form.
I am using following following code for the form:
<form id="pdp-form" action="#Url.Action("action", "Controller")" method="post">
<input type="hidden" value="" name="Size" id="pdp-size">
<input type="hidden" value="" name="ProductId" id="pdp-code">
<input type="hidden" name="Product" id="pdp-ProductName" value="Product Name">
<input type="hidden" name="Category" id="pdp-Category" value="Category Name">
<input type="hidden" value="buyID" name="ProdBuyId" id="ProdBuyId">
</form>
How can I post my from on the secure page without loosing my data?
Note : Secure page where we are posting the form has both get and post methods.
i don't know what are you trying to do, but you're missing the submit button
<button type="submit">Submit</button>
<form role="form" action="#Url.Action("action", "Controller")" method="post">
<input type="hidden" value="" name="Size" id="pdp-size">
<input type="hidden" value="" name="ProductId" id="pdp-code">
<input type="hidden" name="Product" id="pdp-ProductName" value="Product Name">
<input type="hidden" name="Category" id="pdp-Category" value="Category Name">
<input type="hidden" value="buyID" name="ProdBuyId" id="ProdBuyId">
<button type="submit">Submit</button>
</form>

using Bootstrap3 form in laravel5.1

I am trying to make a form using bootstrap3 in laravel5.1 so that I can get values from user and store it on the database and want to redirect the same page.
here is the form part of my code --
<div class="row">
<form action="{{url('fastshops/menu')}} " method="post" role="form">
<legend>ADD NEW ITEM</legend>
<div class="form-group">
<label for=""></label>
<input type="text" class="form-control" name="ItemID" id="" placeholder="Item ID...">
<label for=""></label>
<input type="text" class="form-control" name="ItemName" id="" placeholder="Item Name...">
<label for=""></label>
<input type="text" class="form-control" name="SellPrice" id="" placeholder="Sell Price...">
<label for=""></label>
<input type="text" class="form-control" name="NetPrice" id="" placeholder="Net Price...">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
but this part action="{{url('fastshops/menu')}} is not working ! If I click on the submit button I am having TokenMismatchException ! please help me out.
thanks in advance.
add this
<input type="hidden" name="_token" value="{{Session::token()}}"/> or
<input type="hidden" name="_token" value="{{csrf_token()}}"/>
inside your form.
Laravel 5 automatically check for CSRF on all Post request for all the routes. Read the docs, they are pretty self explainatory

how to make label field and text field to appear on same line in asp.net mvc4?

#using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Form</legend>
<label for="FirstName">First Name:</label>
<input type="text" name="FirstName">
<label for="LastName">Last Name:</label>
<input type="text" name="LastName">
<label for="Period">Date:</label>
<input type="text" name="Period">
<p>
<input type="submit" value=" Send " />
</p>
</fieldset>
</div>
}
Here i want to make all the label field and text field to appear on same line?How could i do that?
Change Parent Div and paragraph display style to inline
<div style="display:inline">
<fieldset>
<legend>Form</legend>
<label for="FirstName">First Name:</label>
<input type="text" name="FirstName">
<label for="LastName">Last Name:</label>
<input type="text" name="LastName">
<label for="Period">Date:</label>
<input type="text" name="Period">
<p style="display:inline">
<input type="submit" value=" Send " />
</p>
</fieldset>
</div>