how i can add tag required on this flask code :
{{ form.youtube_href(type='url', class='form-control') }}
actual output is :
<input class="form-control" id="youtube_href" name="youtube_href" value="" type="url">
need this output bat give error :
<input class="form-control" id="youtube_href" name="youtube_href" value="" type="url" required>
im tried this bat give error :
{{ form.youtube_href(type='url', class='form-control', 'required') }}
To those who simply want to add the
required
attribute to their html input, this can be accomplished by following the comment mentioned by Raja Simon above. Simply call your field name in your template withrequired='required'
Example:The Above code will result in fields like so:
As of WTForms 2.2 (June 2nd, 2018), fields now render the
required
attribute if they have a validator that sets therequired
flag, such asDataRequired
andInputRequired
. If for some reason you don't want to render the attribute, you can passrequired=False
. Or if you want to disable all browser validation, you can set thenovalidate
attribute in theform
tag. In general you should prefer to leave browser validation enabled, because it prevents a request/response for simple validation, which is desirable.You are passing a positional argument after keyword arguments, which is a syntax error. Instead, pass
required=True
, which will set a bare attribute on the tag. Check the flags on a field to see if aRequired
validator was set:field.flags.required
is a boolean. Create aURLField
rather than passing the type manually.