Bad Request/form not defined on modal form submit to email - flask - forms

I am trying to submit a modal form with various input data to my email through my flask app, but am getting 400 bad request error or "form is not defined" error because I tried using this line in the function:
form = modal1form(request.form)
Not sure whats wrong here - note: modal is located at /dashboard/projects/#modal1
Thanks
# new project function
#app.route('/dashboard/projects/#modal1form/', methods=["GET","POST"])
def new_project():
try:
if request.method == "POST":
if form.validate() == False:
flash('Invalid! All fields are required.')
return redirect(url_for('new_project'))
else:
msg = Message(request.form['name'], sender='request.form["email"]', recipients=['myemail#gmail.com'])
msg.body = """
From: %s %s \n
%s \n \n %s \n \n %s \n %s \n
%s
""" % (request.form['wtype', 'wpack', 'bts', 'fts', 'comments'])
mail.send(msg)
flash('Congratulations! A new project has been submitted.')
gc.collect()
return render_template('dashboardProjects.html')
elif request.method == 'GET':
return render_template("dashboardProjects.html")
except Exception as e:
return(str(e))
modal form
<div id="modal1" class="modal fade">
<form method="post" action="{{ url_for('new_project') }}" role="form" id="modal1form" name="modal1form">
<div class="form-group text-center">
<label for="name" class="control-label text-center"> Name </label>
<input type="text" class="form-control" name="name" id="name" placeholder="Your Name">
</div>
<div class="form-group text-center">
<label for="email" class="text-center"> Email Address </label>
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email">
</div>
<div class="col-md-3 col-md-offset-1">
<div class="form-group"> <br>
<label for="wtype"> Website Type </label> <br>
<h5 class="form-check-inline">
<input class="form-check-input" type="checkbox" name="wtype" id="wtype1" value="Static"> Static
</h5>
<h5 class="form-check-inline">
<input class="form-check-input" type="checkbox" name="wtype" id="wtype2" value="Blog"> Blog
</h5>
</div>
</div>
<br>
<div class="col-md-4 col-md-offset-1">
<div class="form-group">
<label for="wpack"> Website Package </label> <br>
<h5 class="form-check-inline">
<input class="form-check-input" name="wpack" type="radio" id="wpack2" value="Blog"> Basic
</h5>
<h5 class="form-check-inline">
<input class="form-check-input" name="wpack" type="radio" id="wpack3" value="Ecommerce"> Standard <br>
</h5>
</div>
</div>
<li class="dropdown pull-right list-unstyled" aria-expanded="false" style="position: top;"> <a class="dropdown-toggle" data-toggle="dropdown" role="button" style="font-size: 14px;"> <span class="caret"></span> <label for="template"> <i class="fa fa-cube"></i> Template </label> </a>
<ul class="dropdown-menu list-unstyled">
<li><h5 class="text-center"> <i class="fa fa-cubes"></i> Templates</h5></li>
<li><input name="bts" type="radio"/> Basic Template 1</li>
<li><input name="bts" type="radio"/> Basic Template 2</li>
<li role="separator" class="divider"></li>
<li><input name="fts" type="radio"/> Footer 1</li>
</ul>
</li>
</div>
<div class="form-group text-center"> <br>
<label for="comments"> Other Details </label>
<textarea name="comments" class="form-control" id="comments" placeholder="Additional comments, ideas, requests, questions?"></textarea>
<p class="help-block"> </p>
</div>
</form>
</div>
<div class="modal-footer brick">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success" form="modal1form">Submit</button>
</div>

ERROR 1: form is not defined
If you want to use Flask-WTF, you must create a Form, like this:
from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DateRrequired
app = Flask(__name__)
app.config['SECRET_KEY'] = 'a very secret string'
class MyForm(FlaskForm):
name = StringForm('Your Name', validators=[DataRequired()])
submit = SubmitField()
app.route('/', methods=['GET', 'POST'])
def index():
form = MyForm()
if form.validate_on_submit():
name = form.name.data
...
return render_template('index.html', form=form)
Then you need to render the form in HTML template:
<form method="post">
{{ form.name() }}
{{ form.submit() }}
</form>
ERROR 2: 400 Bad request
In Flask, if you get form data by request.form['name'], you must be ensure this input have value, that's why you get the 400 error. Instead, I recommend you to use request.form.get('name', default_value), you can set a default value (as second argument) to avoid 400 error.
More details:
flask.pocoo.org/docs/0.11/patterns/wtforms
Hope it will help you.
Maybe ERROR 3: Bootstrap Modal
You can't open a modal with anchor name, like example.com#MyModal, but other modal plugin can. Check my answer on another question about this.

Related

How to access to RESTController through a controller?

I am using Spring.
The method in my RESTController is this,
http://localhost:8080/delivery-api/training/submit. It is able to save hard code values into database.
This is part of my html form, it can be open up using this URL: http://localhost:8080/delivery-web/training/apply.
<form th:object="${applyForm}" class="form-horizontal" role="form" method="post" action="http://localhost:8080/delivery-api/training/submit" >
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1" > Country </label>
<div class="col-sm-9">
<input type="text" id="form-field-1" placeholder="Country"
class="col-xs-10 col-sm-7" th:field="*{country}" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Fee </label>
<div class="col-sm-9">
<input type="text" id="form-field-1" th:field="*{Fee}" class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Start Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{startDate}"
class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1">End Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{endDate}"
class="col-xs-10 col-sm-7" />
</div>
</div>
<br> <br>
<button class="btn btn-info" type="submit">
<i class="ace-icon fa fa-check bigger-110"></i> Submit
</button>
</div>
</div>
</form>
When I click on submit, it can be directed to http://localhost:8080/delivery-api/training/submit and insert the hard coded values into database, but it did not direct to my application-form.html page after that.
My controller
#PostMapping(value = "/apply")
public String apply(#Valid #ModelAttribute("applyForm") DeliveryApplicationForm applyForm, BindingResult errors, Model model) {
model.addAttribute("applyForm", applyForm);
return VIEW_PATH + "application-form";
}
I would like to render html page and also pass data from my form to RESTController method through a controller after I click on the submit button in my html page.
May I know how I can do it?

Angular error:Form submission canceled because the form is not connected

What I am trying to do is -
I am trying to add a form to the existing form but the data not getting stored
what the error coming up -
in the console its showing form connection is missing.. how can I connect it with following code I have?
The code behind the click is something like this:
<button type="submit" class="btn btn-default" routerLink="../viewemployee" [disabled]="empForm.invalid">Add Employee</button>
please refer the link below for the code.. need help to move on from this to solve other tasks.
If it is required to post the code here as well, I will do.Please answer and respond.
Thanx in advance.
How to connect the form in angular routing
createemployee.component.html
<h2>Add Employee:</h2>
<form class="form-horizontal" #empForm="ngForm">
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" minlength="4" maxlength="10" pattern="^[A-Za-z0-9]*[A-Za-z0-9][A-Za-z0-9]\S*$" [(ngModel)]="model.name" placeholder="Enter Your Name"
#name="ngModel" required/>
<div *ngIf="name.invalid && (name.dirty || name.touched)" class="alert alert-danger">
<div *ngIf="name.errors.required">
Name is required.
</div>
<div *ngIf="name.errors.pattern">
No Spaces
</div>
<div *ngIf="name.errors.minlength">
Name must be at least 4 characters long.
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="position">Position:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="position" minlength="4" maxlength="10" pattern="^[a-z]*$" [(ngModel)]="model.position" placeholder="Enter your position"
#position="ngModel" required />
<div *ngIf="position.invalid && (position.dirty || position.touched)" class="alert alert-danger">
<div *ngIf="position.errors.required">
Position is required.
</div>
<div *ngIf="position.errors.pattern">
Only Alphabets are must be entered
</div>
<div *ngIf="position.errors.minlength">
Position must be at least 4 characters long.
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="salary">Salary:</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="salary" pattern="[0-9]*"
minlength="5" maxlength="7" [(ngModel)]="model.salary" placeholder="Enter Salary" #salary="ngModel" required />
<div *ngIf="salary.invalid && (salary.dirty || salary.touched)" class="alert alert-danger">
<div *ngIf="salary.errors.required">
Salary is required.
</div>
<div *ngIf="salary.errors.minlength">
Salary must be in 5 numbers.
</div>
<div *ngIf="salary.errors.maxlength">
Salary must not be exceeded morethan 7 numbers.
</div>
<div *ngIf="salary.errors?.pattern">Only numebers should be typed
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" routerLink="../viewemployee" [disabled]="empForm.invalid">Add Employee</button>
<button type="button" class="btn btn-default" routerLink="../home">Cancel</button>
</div>
</div>
</form>
You could change your component as follows:
Add event handler to the form:
<form (ngSubmit)="continue()">
Handle the routing in code:
continue() {
...
this.router.navigateByUrl("../viewemployee");
}
You need to inject the router:
constructor(private router: Router) {}

Angular Form Validation error : form is undefined

I am trying to validate my data for a student object with Angular Form Validation, but when I want to use or print for example, the serialNumber of a student it gives me this error: Cannot read property 'serialNumber' of undefined.
Here is the code:
<div *ngIf="student">
<div class=container>
<h2>Student {{student.name}} details</h2>
<form name="studentForm" (ngSubmit)="save()">
<!--<div ng-class="{ 'has-error' : studentForm.serialNumber.$invalid && !studentForm.serialNumber.t">-->
<label>Serial number: {{student.serialNumber}} </label>
<input type="text" name="serialNumber" class="form-control" ng-model="student.serialNumber" required>
<div ng-messages="studentForm.serialNumber.$error">
{{studentForm.serialNumber}}
<p ng-message="required">Your name is required!</p>
</div>
<!--</div>-->
<div>
<label>Name: {{student.name}}</label>
<input ng-model="student.name" placeholder="name">
</div>
<div>
<label>Group number: {{student.groupNumber}}</label>
<input ng-model="student.groupNumber" placeholder="groupNumber">
</div>
<button (click)="goBack()">Back</button>
<button (click)="save()">Save</button>
</form>
</div>
</div>
The ng-messages and ng-model attribute directives do not exist in Angular 2+. I would recommend reading into Angular Forms, ReactiveForms, and Template Syntax.
If you would like to dual data-bind the input values, you can do so with the following syntax: [(ngModel)]="student.serialNumber". However, in Angular 2+, there are usually better ways of getting values other than explicitly data-binding.
Angular Form Validation template Driven Model
onSubmit(form : NgForm) {
console.log(form);
}
<form #form="ngForm" (ngSubmit)="onSubmit(form)"
[ngClass]="{'was-validated': form.invalid && (form.dirty || form.touched)}">
<div class="" ngModelGroup="User">
<h2 class="text-center">Registration page</h2>
<br />
<div class="form-group">
<input type="text" class="form-control" placeholder="First Name" name="firstname" required
ngModel #firstname="ngModel">
<span class="help-bpx" *ngIf="firstname.touched && !firstname.valid ">Please enter the
firstname</span>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Last Name" name="lastname" required ngModel
#lastname="ngModel">
<span class="help-bpx" *ngIf="lastname.touched && !lastname.valid ">Please enter the
lastname</span>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" placeholder="Email" name="email" email
required ngModel #email="ngModel">
<span class="help-bpx" *ngIf="email.touched && !email.valid ">Please enter the Email
Value</span>
</div>
<div class="form-group">
<div class="custom-file">
<input type="file" class="custom-file-input" id="customFile" required ngModel name="file" #file="ngModel">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
</div>
<br />
<div class="align-center">
<button type="submit" class="btn btn-primary" [disabled]="!form.valid">Register</button>
</div>
</div>
</form>

submit a form pressing ENTER

I have a submit form, when I click on the button "log in" it works, $_POST['login'] contains something, but when I press ENTER key it is empty.
form
<form class="form-vertical login-form" action="conexio.php" method="post">
<h3 class="form-title">Login to your account</h3>
<div class="alert alert-error hide">
<button class="close" data-dismiss="alert"></button>
<span>Enter any username and password.</span>
</div>
<div class="control-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">Username</label>
<div class="controls">
<div class="input-icon left">
<i class="icon-user"></i>
<input class="m-wrap placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="username"/>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<div class="controls">
<div class="input-icon left">
<i class="icon-lock"></i>
<input class="m-wrap placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="password"/>
</div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn blue pull-right" name="login">
Login <i class="m-icon-swapright m-icon-white"></i>
</button>
</div>
</form>
Try replacing <button> with <input type='submit'>.

Checkbox and caption without line break

I can't place checkbox and "Remember me" in one line using laravel and bootstrap. There is a line break between checkbox and "Remember me" but I don't want that line break.
My code:
{{Form::open(['route'=>'sessions.store','class'=>'form-horizontal'])}}
<div class = 'form-group'>
{{Form::label('email','Email',['class'=>'col-sm-3 control-label'])}}
<div class="col-sm-4">
{{Form::email('email',null,['class'=>'form-control'])}}
{{$errors->first('email')}}
</div>
</div>
<div class = 'form-group'>
{{Form::label('password','Password',['class'=>'col-sm-3 control-label'])}}
<div class="col-sm-4">
{{Form::password('password',['class'=>'form-control'])}}
{{$errors->first('password')}}
</div>
</div>
<div class = 'form-group'>
<div class='col-sm-offset-3 col-sm-4'>
{{Form::checkbox('remember-me',1,null,['class'=>'checkbox'])}}
{{Form::label('remember-me','Remember me')}}
</div>
</div>
<div class = 'form-group'>
<div class="col-sm-offset-3 col-sm-4">
{{Form::submit('Login',['class'=>'btn btn-default'])}}
</div>
</div>
{{Form::close()}}
That's because you're generating this markup for the 'Remember me' checkbox:
<div class="form-group">
<div class="col-sm-offset-3 col-sm-4">
<input class="checkbox" name="remember-me" type="checkbox" value="1">
<label for="remember-me">Remember me</label>
</div>
</div>
While this is the expected markup for the default Twitter Bootstrap checkbox:
<div class="checkbox">
<div class="col-sm-offset-3 col-sm-4">
<label>
<input type="checkbox" name="remember-me" value="1"> Remember me
</label>
</div>
</div>
To fix this, you can use this Blade excerpt in your view:
<div class="checkbox">
<div class="col-sm-offset-3 col-sm-4">
<label>
{{ Form::checkbox('remember-me', 1) }}
</label>
</div>
</div>