I'm using I'm using laravel 5.5 and laravel collective to build my forms.
During validation, to manage errors and putting back the old values, I'm using this kind of code:
<div class="#if ($errors->has( $question->title )) has-error #endif">
{!! Form::label($question->title, $question->title) !!}
{!! Form::text($question->title, null, ['class' => 'form-control']) !!}
#if ($errors->has($question->title)) <p class="help-block">{{ $errors->first($question->title) }}</p> #endif
</div>
And everything is working well. But I'm facing a problem where my name attributes contains white spaces.
When the page is displayed my name attributes are well writen with white space by example Are you happy?
But the validation return error mentioning Are_you_happy? so the old values are not putted back and the errors messages are not displayed due to the _.
I was thinking of using regex to "sanitize" the values in my forms but the error message will still be The Are_you_happy? field is required. So not that good.
Is there any tricks or better ways to proceed?
Or is it just a bad practice to have whitespace?
More info
This also happen with the dot character ., it's is replaced by an _ during validation.
It's bad practice to use white space,but it's not an error.
you can work around your problem changing the output of the error messages.
From the documentation
Specifying Custom Attributes In Language Files
If you would like the :attribute portion of your validation message to be replaced with a custom attribute name, you may specify the custom name in the attributes array of your resources/lang/xx/validation.php language file:
'attributes' => [
'email' => 'email address',
],
in your case
'attributes' => [
'Are_you_happy' => 'are you happy',
],
Related
So I'm using Livewire 2.10.4 and I have a textarea within a modal, which looks like this:
<div
x-data="{
showDiscontinueModal: #entangle('showModal')
}"
>
<x-modal x-model="showDiscontinueModal" modalDialogClasses="modal-sm-md">
<x-slot name="content">
<x-input.group class="w-full">
<x-input.label>{{ trans('rx.pharmacy_notes') }}</x-input.label>
<x-input.textarea
wire:model="pharmacyNote"
/>
</x-input.group>
</x-slot>
</x-modal>
</div>
The textarea blade component looks like this:
#aware(['error'])
#props(['error' => null])
<textarea
type="text"
x-bind:id="$id('input')"
{{ $attributes->class([
'form-input',
'form-control',
'missing' => $error,
]) }}
></textarea>
But each time the user types something in the textarea input field Livewire hydrates, causing a lot of unnecessary requests and weird behavior in the input (it flickers and erases part of what's being typed)
So far I tried to use wire:model.defer , wire:model.lazy and wire:model.debounce to try to mitigate the number of requests but for some reason this is not affecting the behavior at all. I also tested changing the field to a simple text input and that didn't work either.
try wire:ignore or wire:ignore.self for your textarea parent
I have an error in one of my forms, all the others work but in particular I cannot edit in this one since it does not send the post from a form created with Form :: model (), it took me a while to realize that this was the problem, although I have defined the form as POST the data is sent as GET and it does not find the route since it is defined as POST.
This is my failed code:
{!! Form::model($nota, [
'method' => 'POST',
'url' => ['/admin/notas', $nota->id],
'class' => 'form-horizontal',
'files' => true]) !!}
#include('admin.notas.form', ['formMode' => 'edit'])
{!! Form::close() !!}
But later I realized that if I copy the html generated by this code and paste it raw, there it works!
<form method="POST" action="https://mydomain/admin/notas/{{$nota->id}}" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
#csrf
#include('admin.notas.form', ['formMode' => 'edit'])
</form>
It is worth mentioning that this form allowed to edit correctly beforehand, and that in localhost (WAMP) it works correctly, I already confirmed that my model, controller and routes are fine, but everything fails from the submit and does not reach the route defined as Post because it is sent as Get.
Note:
The problem is gone, I was about to upload the form with raw html to get out of the error and I realized that the error was corrected, I do not know if it was a server error, the strange thing is that other forms did not fail and when submitting the form laravel did not throw me any error.
Good day to all, I am beginner in Laravel and as you know html form by default sends POST requests and wanted to ask if it is ok if I omit POST at the beginning of form tag since I want to send POST request to server The code:
{!! Form::open(['method' => 'ToDoController#store' **I am not specifying type of request**]) !!}
<div class="form-group">
{{Form::label('text', 'Text', ['class' => 'awesome'])}}
{{Form::text('text', '', ['class'=>'form-control' ,'style'=>'width:200px'])}}
</div>
<div class="form-group">
{{Form::label('text', 'Text', ['class' => 'awesome'])}}
{{Form::text('text', '', ['class'=>'form-control' ,'style'=>'width:200px'])}}
</div>
{{Form::submit('Submit')}}
{!! Form::close() !!}
It is OK to omit an attribute if the default is what you intend to carry out. However, you should note the following.
HTML forms by default sends 'GET' requests.
Form Collective which is a package used in Laravel Framework has by default set the method attribute of HTML forms to 'POST'.
So, the reason why your form is sending a 'POST' even when you didn't set the method attribute is because you are using Form Collective else your form would have defaulted to 'GET'.
Meanwhile, from the snippet you pasted:
{!! Form::open(['method' => 'ToDoController#store' **I am not specifying type of request**]) !!}
Change that to:
{!! Form::open(['action' => 'ToDoController#store']) !!}
The method attribute is meant for specifying the kind of request...chiefly 'GET' or 'POST' while the action attribute is meant for specifying the Controller action that will handle the request.
So i have a <textarea> in my form. User can either enter a new line or single line. So when the user views the text it will be showed like how he/she inputs it.
Form
<textarea name="jo_unit" class = 'form-control' required="required" cols = '4'></textarea>
Expected Output
TESTING 1 TESTING 2 TESTING 3
What i get
My code on displaying it
{{ nl2br(#$get['result'][0]->jo_unit_2) }}
Security: This code allows XSS attacks and is not production ready.
use {!! !!}:
{!! nl2br(#$get['result'][0]->jo_unit_2) !!}
Use
{!! nl2br(e(#$get['result'][0]->jo_unit_2)) !!}
{!! !!} creates an escaped output and allows HTML (and XSS attacks).
To make it secure again you also need to use e() see helpers documentation.
I have a variable $country_code that is displaying the correct value in one part of my form but not in a different part. Why is this happening?
This is my code:
{{ Form::open(['action' => ['PinVerificationController#valid'],'id'=>'pin_code_form']) }}
//$country_code shows 1
We sent a text message to {{$country_code}} {{$phone_number}}. You should receive it within a few seconds.<br><br>
{{ Form::label('Pin Code', null, ['class' => 'control-label']) }}
{{ Form::hidden('country_code', $country_code) }}//<------shows 1-US instead of 1
{{ Form::hidden('phone_number', $phone_number) }}
{{ Form::hidden('type', $pin_notification_type) }}
{{ Form::text('pin_code', null,['placeholder' => 'Pin Code'])}}<br><br>
Enter a 4 digit pin you received by phone.
<br>
<br>
{{ Form::submit('Verify',['name'=>'validate'])}}
{{ Form::close() }}
So if I set $country_code to "1" in my controller it'll display We sent a text message to 1 5555555. You should receive it within a few seconds.
But if I do an inspect element on my hidden form it displays 1-US. I've tried php artisan view:clear and php artisan clear-compiled but the problem still persists.
I've also tried hardcoding a value {{ Form::hidden('country_code', 'asdf') }} and i'm not seeing the change. I tried adding a test {{ Form::hidden('country_code1', 'asdf') }} and see the update.
I also renamed country_code to country_code111 for my hidden field and it displayed the correct value of 1. I thought it was a caching issue but like I mentioned I've tried php artisan cache:clear and the problem is still there.
Since you are using Laravel 5.4, I assume you are using Form from the LaravelCollective, since they were removed from baseline Laravel in 5.x.
LaravelCollective Forms will override the value you provide to the input if it exists in the request data, or in old posted data (the old() function). I suspect this is the case for you.
You can see this behavior implementation here.
To solve this problem, you have a few options:
change the name of the request parameter feeding into the page (if you have control over it)
rename your field name to something that doesn't conflict
Don't use Form:: to generate the form and just use classic html/Blade to create the hidden input automatically
Personally, I would recommend #3 because then you have full control over your code.
<input type="hidden" name="country_code" value="{{ $country_code }}"/>