My blade template contains a form where data can be inserted like name, mail, etc.
One input field is a toggle checkbox where you can check whether you are an intern or not.
Intern => toggle checked and "Yes" is visible (equals in database 1)
Not intern => toggle is not checked and "No" is visible (equals in database 0)
The checking of the box is working but the status intern or extern isn't sent to the database. Bellow, you will find my code. I don't know if this is the correct way to this.
<input checked data-toggle="toggle" data-on="Yes" {{$person->intern_extern == '1' ? 'checked' : ''}}
data-off="No" {{$person->intern_extern == '0' ? 'checked' : ''}} data-onstyle="primary"
data-offstyle="info" type="checkbox"
name="intern_extern">
As discuss with #N69S , I will suggest you a solution that will require less code.
Your yes/no on the client-side equal 1/0 on server-side.
By default, checkbox into form is not send to server-side if it's not checked.
So, using how request work within Laravel, you can create a checkbox like this one:
<input name="intern_extern" type="checkbox" {{$person->intern_extern == 0 ? 'checked' : ''}}>
Into your controller, when updating a person model, do this:
$person->intern_extern = $request->input('intern_extern', 0);
This is what N69S explain about the $request->input():
yes, by default it is null but you can set it like that when you recover the input trait InteractsWithInput # public function input($key = null, $default = null)
In this case, if the checkbox inter_person is checked, the form will post the input with the value true. If not checked, it will not be send and the value will be 0.
In case you are using this to create your model: $model = Model::create($request->all());, you will have to set the default value into your migration $table->boolean('intern_extern')->default(0); to make all this work.
EDIT - Simple suggestion
When I use boolean, I like to name them like "isSomething". In your case, isIntern or isExtern. So when you have to refer to it, it's simple to read it. isIntern (yes or no) or isExtern (yes/no).
Try it.
<input data-toggle="toggle" data-on="Yes" data-off="No" #if(!empty($person) && $person->intern_extern) {{ 'checked' }} #endif data-onstyle="primary" data-offstyle="info" type="checkbox" name="intern_extern">
here #if(!empty($person) && $person->intern_extern) it'll check $person->intern_extern == 1;
You need to have value="Yes" for this to work which is currently absent from your code.
<input
name="intern_extern"
type="checkbox"
value="Yes"
{{ $person->intern_extern == '1' ? 'checked' : '' }}
data-toggle="toggle"
data-on="Yes"
data-off="No" {{$person->intern_extern == '0' ? 'checked' : ''}} data-onstyle="primary"
data-offstyle="info"
>
BUT, if my memory serves me correctly, you cannot have a "No" value sent to the backend when submitting the form unless you're using some JS to handle this.
Why? Because checkboxes can only really represent one value, in your example if it's checked it will show up as intern_extern = Yes in the request, however, if the checkbox is unchecked it will be absent from the request.
Considering this you may want to switch to a different form control: <select> for example to represent Yes/No. Or continue using the checkbox but just handle it appropriately in PHP code, i.e. if it's not in the request then fill out the DB with No/0
A checkbox input is not sent with the form submission if not checked. You can counter it like this:
<input type="hidden" name="intern_extern" value="0"/>
<input checked data-toggle="toggle" data-on="Yes" #if(isset($person->intern_extern) && $person->intern_extern)checked="checked"#endif
data-onstyle="primary" data-offstyle="info" type="checkbox" value="1" name="intern_extern">
if the checkbox is checked, it will override the hidden input value since it has the same name
Related
Is there a way to conditionally pass a disabled attribute to a blade component? For example this question and answer mention how to use the ternary operator to pass in the value of the attribute but the attribute name will be there regardless.
I am specifically trying to use the ternary operator in the blade component tag to add (or not add) the disabled attribute
template code:
<x-button {{!$aircraftType->canIslandBuild($island) ? 'disabled' : ''}}>
Build
</x-button>
button component code:
<button {{ $attributes->merge(['class' => 'inline-flex']) }}>
{{ $slot }}
</button>
The error involves adding {{!$aircraftType->canIslandBuild($island) ? 'disabled' : ''}} to the x-button tag.
The error that I'm getting is: syntax error, unexpected token "endif", expecting end of file
Also if I change this {{!$aircraftType->canIslandBuild($island) ? 'disabled' : ''}} to {{''}} the same error happens so I'm curious if you can render strings from php code inside of the component tag header like you can anywhere else in a blade template. I know there are other ways to pass in data and conditionally add the disabled attribute by modifying the button component code but I would like to know if there is an easy solution here.
Went with matiaslauriti's comment and decided to replace
{{!$aircraftType->canIslandBuild($island) ? 'disabled' : ''}}
with
:disabled="!$aircraftType->canIslandBuild($island)"
without changing anything else. It seems disabled="disabled" is included in the $attributes variable only when the value is true. This means that the button renders a disabled="disabled" only when !$aircraftType->canIslandBuild($island) is true and when it is false, no disabled is rendered on the final html for the button.
I'm using Laravel 5.8, and have several input fields which of course has an old() directive on every value="" tag.
This is my example right now:
<input class="form-control input-md" name="contact_name" type="text" value="#if($edit){{ $ad->contact_name }}#else{{ old('contact_name')}}#endif">
I now that if I use this: {{ old('contact_name', "John")}}
The default value will be "John"
But I want to do a check if there is a user logged in and prefill that input with the User contact name.
My idea is something like this:
value="#if($edit){{ $ad->contact_name }}#else{{ old('contact_name', Auth::user()->name)}}#endif
And it works! But of course, it throws: Trying to get property 'name' when I get an incognito window.
So, how do I evaluate logged in users and prefill this?
You can use the optional helper:
{{ old('contact_name', optional(Auth::user())->name) }}
Having issue with form validation .
i want to submit the form only when form is valid.
but with the empty inputs and clicking on submit button is submitting the form although the inputs are empty.
<form name="equipmentForm" #f="ngForm" (ngSubmit)="f.form.valid && addEquipment()" validate>
Inputs be like this.
<input name="equimentId" class="text-input form-control" type="text" [(ngModel)]="model.equipmentNumber" pattern="^[0-9][0-9]{1,19}$" title="Equipment ID. can be upto 20 digits only.">
I cant post the whole code although.
this
f.form.valid is true from form initialization
wanted to acheive something like this
<div *ngIf="!model.equipmentModel && f.submitted" class="text-danger">
Please enter Equipment Model
</div>
So on submit i want to show this message instead of default browser's.
but this f.form.valid is goddamn true from default.
You should add required attribute to your input tags to, then as #Cobus Kruger mentioned, form will not be submitted untill it is filled.
However you can also give a try to pristine, dirty options, which allow you to check if the user did any changes to the form so in this case your condition may look like this:
<form name="equipmentForm" #f="ngForm" (ngSubmit)="f.form.valid && f.form.dirty ? addEquipment() : ''" validate>
and the input:
<input name="equimentId" class="text-input form-control" type="text" [(ngModel)]="model.equipmentNumber" pattern="^[0-9][0-9]{1,19}$" title="Equipment ID. can be upto 20 digits only." required />
In this case it will check if any changes were applied to the input, and submit the form if both conditions are met.
If you specify the required attribute on the input, then the form will not be submitted unless a value is filled in. But that only covers values that were not supplied and you may want to check for invalid values as well.
The usual way is to disable the submit button unless the form is valid. Like this:
<button type="submit" [disabled]="!f.form.valid">Submit</button>
The Angular documentation about form validation also shows this. Look near the bottom of the "Simple template driven forms" section
In function which you call on submit you can pass form as parameter and then check. In html you will need to pass form instance:
<form name="equipmentForm" #f="ngForm" (ngSubmit)="addEquipment(f)" validate>
In typescript:
addEquipment(form){
if(form.invalid){
return;
}
//If it is valid it will continue to here...
}
Im making a edit form for my app and i was wondering if someone could tell me how to get the data from the database into my text field.
I can locate the record i need to edit based on the users click, and i can display the information if i do the following:
value="{{ $letter->subject }}"
BUT, the problem im having is that when i run it through the validation and there is an error, it comes back with the database information instead of the OLD data.
So my questions is. Is there a way to serve up the database information first and then when it goes through the validatior, validate the information the user has edited?
Currently to validate the text field and bring the data back incase of error, im using
Input::old('subject')
Is there a parameter for that old bit that allows me to put in the DB data?
Cheers,
Hey you could validate and return ->withInput() and then in your actual form, check if there is Input::old() and display it, otherwise display from the db.
example:
<input type="text" name="subject"
value="{{ (Input::old('subject')) ? Input::old('subject') : $letter->subject }}">
Or you could go the other way and define the variable and do a regular if statement, instead of the ternary one! Up to you to decide what you want to use!
All you need is form model binding http://laravel.com/docs/html#form-model-binding:
{{ Form::model($letter, ['route' => ['letters.update', $letter->id], 'method' => 'put']) }}
// your fields like:
{{ Form::text('someName', null, ['class' => 'someHTMLclass' ...]) }}
// no default values like Input::old or $letter->something!
{{ Form::close() }}
This way you form will be populated by the $letter data (passed from the controller for example).
Now, if you have on your countroller:
// in case of invalid data
return Redirect::back()->withInput();
then on the redirect your form will be repopulated with input values first, not the original model data.
Make it more simple and clean
<input type="text" name="subject" value="{{ (Input::old('subject')) ?: $letter->subject }}">
I'm not sure for Laravel 4 but in Laravel 5, function old takes second param, default value if no old data in session.
Please check this answer Best practice to show old value
I'm trying to create an edit page, which brings all of the original values from the database in at first, then let's the form_validation library take over afterwards. I have managed to get everything working as intended but checkboxes and radio buttons.
Here's an example of my form, pretty generic...
<input type="checkbox" name="protocols[]" value="online" <?php echo set_checkbox('protocols[]', 'online');?> />
<input type="checkbox" name="protocols[]" value="network" <?php echo set_checkbox('protocols[]', 'network');?> />
<input type="checkbox" name="protocols[]" value="splitscreen" <?php echo set_checkbox('protocols[]', 'splitscreen');?> />
The database values return as a comma separated string (online,splitscreen).
I also have another 3 field checkbox array to populate, a 9 field checkbox field, and a 3 field radio section to fill.
Any help would be greatly appreciated, thanks.
Remove the brackets from the field name in your call to set_checkbox():
<input type="checkbox" name="protocols[]" value="online" <?php echo set_checkbox('protocols', 'online');?> />
<input type="checkbox" name="protocols[]" value="network" <?php echo set_checkbox('protocols', 'network');?> />
<input type="checkbox" name="protocols[]" value="splitscreen" <?php echo set_checkbox('protocols', 'splitscreen');?> />
The form validation library will take care of checking the box or not, but it responds to the $_POST array only, so you'll have to use the third parameter to get the inputs checked by default:
set_checkbox()
Permits you to display a checkbox in the state it was submitted. The
first parameter must contain the name of the checkbox, the second
parameter must contain its value, and the third (optional) parameter
lets you set an item as the default (use boolean TRUE/FALSE).
Not a great explanation, but here's an example. First get your values from the comma separated string:
The database values return as a comma separated string (online,splitscreen).
// Something like this
$values = explode(',', $my_data); // Now it's an array
Then check if each checkbox's value is in that array:
<?php echo set_checkbox(
'protocols',
'splitscreen',
in_array('splitscreen', $values) // TRUE checks the box, FALSE does not
);?>
I'd do this in a loop for convenience reasons if nothing else. It's also worth looking at form_checkbox() which will make this a good deal easier.
See user guide for details: http://ellislab.com/codeigniter/user_guide/helpers/form_helper.html