Is there any way to disable form validation?
I have problem with datetime field.
I made in JQuery array with datetime data as follow,
$("[name='{{full_name}}[date][year]']").val(year);
$("[name='{{full_name}}[date][month]']").val(month);
$("[name='{{full_name}}[date][day]']").val(day);
$("[name='{{full_name}}[time][hour]']").val(hour);
$("[name='{{full_name}}[time][minute]']").val(minute);
But when I submit the form, I always get wrong value error messages for datetime field.
Does anyone already faced the same issue?
I made my own datetime type field because I wanted to make my own look of it. Firstly i made hidden inputs, and 2 visible inputs, like this:
<input type="date" value="{{ date_date }}" {{ block('widget_attr') }} class="form-control pull-left" style="width: 30%" placeholder="{{ 'date'|trans({},'app')|capitalize }}" name="TdateDate" />
<input type="time" value="{{ date_time }}" {{ block('widget_attr') }} class="form-control pull-left" style="width: 20%" placeholder="{{ 'time'|trans({},'app')|capitalize }}" name="TdateTime"/>
<input {{ block('widget_attr') }} name="{{full_name}}[date][year]" value="" class="hidden" />
<input {{ block('widget_attr') }} name="{{full_name}}[date][month]" value="" class="hidden"/>
<input {{ block('widget_attr') }} name="{{full_name}}[date][day]" value="" class="hidden" />
<input {{ block('widget_attr') }} name="{{full_name}}[time][hour]" value="" class="hidden"/>
<input {{ block('widget_attr') }} name="{{full_name}}[time][minute]" value="" class="hidden" />
Then with Javascript i put data from this two visible fields (date and time) to this 5 hidden inputs to make Datetime array type and i want to pass it to the form by name generated form my field in one variable... But form does not accept it.
From this 2 fields i bring data by JQuery script, like this:
var year = $("input[name='TdateDate']").val().substr(0, 4);
var month = $("input[name='TdateDate']").val().substr(5, 2);
var day = $("input[name='TdateDate']").val().substr(8, 2) ;
var hour = $("input[name='TdateTime']").val().substr(0, 2);
var minute = $("input[name='TdateTime']").val().substr(3, 2);
This part working fine, i think. I get datetime array passed to my form field but it just cant validate it...
Related
I am using an input for the date. How do we set the Input to display the Date Today? Thank you for your time.
<div class="form-group">
<label>Debarred</label>
<input v-model="form.debarred" type="date" name="debarred"
placeholder="Debarred"
class="form-control" :class="{ 'is-invalid': form.errors.has('debarred') }">
<has-error :form="form" field="debarred"></has-error>
</div>
You can update the model value form.debarred on page load like:
this.form.debarred = new Date().toLocaleDateString('en-CA');
//==> "2020-04-03"
Now, using .toLocaleDateString('en-CA') is need here as <input type="date"> value can only accept a string representing a date in YYYY-MM-DD format, or empty.
DEMO:
const debarred = document.querySelector('[name=debarred]');
debarred.value = new Date().toLocaleDateString('en-CA');
<div class="form-group">
<label>Debarred</label>
<input type="date" name="debarred" placeholder="Debarred" class="form-control">
<has-error :form="form" field="debarred"></has-error>
</div>
I'm trying to load Bootstrap classes with FormFacade in a view but doesn't work.
I tried this steps: https://stackoverflow.com/a/46392838/3444714
But the Form class is generating fields without the effects of Bootstrap.
<div class="form-group">
{{ Form::label('testField', 'Test:') }}
{{ Form::text('testField', null, ['class' => 'formĀ-control']) }}
<input type="text" id="testField" name="testField" class="form-control" />
</div>
Check my attachment, same code, but the field generated by FormFacade is wrong.
Thank you.
I am reading tutorial here
https://symfony.com/doc/current/form/csrf_protection.html
how to add csrf token. It says to use
form_end()
in the template. But this is not working, gives error:
Type error: Too few arguments to function
Symfony\Component\Form\FormRenderer::renderBlock(), 0 passed in
E:\projektai\php
projektai\htdocs\mokomieji\symfony_4_demo\var\cache\dev\twig\bb\bb2248f7be504240fcc2ab43dabf593090ebc4c897ce72b1a979082d62914b47.php
on line 48 and at least 2 expected
Here is answer which shows how to fix but it is only when you have form object built:
Symfony Type error: Too few arguments to function FormRenderer::renderBlock()
How to do this without having form object? Here is login from login documentation page:
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<button type="submit">Login</button>
{{ form_end() }}
You can use the helper twig function csrf_token as described in the doc here, as example:
<input type="hidden" name="_csrf_token"
value="{{ csrf_token('authenticate') }}"
>
More help in this answer.
UPDATE:
Other strategy: pass from controller:
$tokenProvider = $this->container->get('security.csrf.token_manager');
$token = $tokenProvider->getToken('example')->getValue();
Hope this help
You can use {{ form_row(form._token) }} to generate the required CSRF token field to your form render in Symfony 3 (i'm currenlty use this method with Symfony 3.4).
{{ form_end() }} works only if you have something like this:
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
You can create custom token in your controller and then pass it to the view like this:
$csrf = $this->container->get('security.csrf.token_manager');
$token = $csrf->refreshToken('yourkey');
And then create hidden input in your twig with token:
<input type="hidden" name="_token" value="{{ token }}">
To get the right csrf_token you should create the FormView with $form->createView(), then use the token inside it:
<input type="hidden" name="_token" value="{{ form._token.vars.value }}">
All the other solutions rely on generating a static string that does not change, which violates the purpose of the csrf tokens.
I have a model with a field called map_zips and inside that field, I would like to store up to 5 zip codes, comma separated.
I would like to have five different form fields that before getting stored in the database model, I concatenate the values together and store them in the DB in the single column.
How can I create multiple form fields in a view, validate said form fields, perform what needs to be done on said form fields, store them? Beyond that, I think I will also need to split them up when loading up the edit/update page as well.
I guess the simplest way would be to create 5 different columns, one for each zip, but I'd like to learn how to do this to help extend my limited knowledge of Laravel.
In my ReportsController.php my current store method looks like this:
public function store(Request $request) {
$this->validate($request, $this->rules);
$user = Auth::user();
$report = $request->all();
$report['user_id'] = $user->id;
Report::create($report);
return redirect('/reports')->with('status', 'Report created');
}
I don't know if i undertand you question but you can try this:
if you want 5 input fields in your view you can try this, you will see error messages from you validation and a returned message to confirm that the value was stored propperly
#if (count($errors) > 0)
<div class="alert alert-danger">
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
</div>
#endif
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
<form action="{{ route("route_to_store_function") }}" method="post">
<input type="text" name="zip1">
<input type="text" name="zip2">
<input type="text" name="zip3">
<input type="text" name="zip4">
<input type="text" name="zip5">
<button type="submit"></button>
</form>
then in your store function, concatenate the fields, save the report and return with a message to the previous page:
public function store(Request $request) {
$this->validate($request, [
'zip1' => 'required',// you can add more validations here
'zip2' => 'required',
'zip3' => 'required',
'zip4' => 'required',
'zip5' => 'required',
]);
$report = new Report();
$report->map_zips = $request->zip1.",".$request->zip2.",".$request->zip3.",".$request->zip4.",".$request->zip5;
$report->save();
return redirect()->back()->with('message', 'The report has been stored succesfully');
}
Then when you want to edit the report you can try this:
public function edit($id) {
$report = Report::find($id)
$zipCodes = explode(",", $report->map_zips);
return view('edit_report_view', compact("report", "zipCodes"));
}
And in you edit view:
<form action="{{ route("route_to_update") }}" method="post">
<input type="hidden" name="id" value="{{ $report->id }}">
<input type="text" name="zip1" value="{{ $zipCodes[0] }}">
<input type="text" name="zip2" value="{{ $zipCodes[1] }}">
<input type="text" name="zip3" value="{{ $zipCodes[2] }}">
<input type="text" name="zip4" value="{{ $zipCodes[3] }}">
<input type="text" name="zip5" value="{{ $zipCodes[4] }}">
<button type="submit"></button>
</form>
I'm adding a confirm details dialogue to a form before it gets submitted, using a bootstrap modal.
The form inputs look as follows:
<input type="email" ... name="email" id="email" value="{{ submitted.email }}"/>
I'd like to add a line of text to the modal body which reads something like:
"Send to foo#bar.com?"
I tried the these:
<p>Send to {{ form.email.get('value') }}</p>
<p>Send to {{ form.vars.value.email }}</p>
<p>Send to {{ submitted.email }}</p>
The modal is inside the form which has the input i.o.w:
<form method="post" ...>
...
<input type="email" ..../>
...
<button type="button" data-target="thisModal" data-toggle="modal"> Open Modal </button>
<div id="thisModal" class="modal"...>
...
<div class="modal-body>
<p>Send to {{ XXXXXXX }}</p>
</div>
<div class="modal-footer">
<button type="submit"> Send </button>
</div>
</div>
</form>
Using all 3 prior methods it was simply an empty string, is it possible to grab those values in this way, if so any guidance would be appreciated.
You can handle submitted form data in twig in this way:
{{ YOUR_FORM_VARIABLE.FORM_VARIABLE.vars.value }}
Do not forget to handle form request in your controller, after submitting data.
For sample:
// Some controller
public function indexAction(Request $request)
{
// supposing that FormType has only email field
$form = $this->createForm(FormType::class, null, array(
'action' => $this->generateUrl('homepage'),
));
$form->handleRequest($request);
return $this->render('Bundle:Homepage:layout.html.twig', array(
'form' => $form->createView()
));
}
And in template:
// Bundle:Homepage:layout.html.twig
{{ form(form) }}
<b>{{ dump(form.email.vars.value )}}</b>
The modal is being called via JavaScript so it doesn't hit PHP at the point wherein which the data needs to be transferred. Hence it cannot be done using twig, as the data is yet to be passed.