Assigning the id in html through the Blade concept. laravel 5.1 - html

here is my code ,
I want to change the radio buttons,label which is associated with the radiobutton id="male". I have tried various ways to do it, but i can't make it work. I want to change the text "Male" in the label associated to the radio button in Blade Laravel 5.1
{!! Form::label('gender','Gender') !!} :
{!! Form::label('lblMale','Male') !!}
{!! Form::radio('sex','male' !!}
{!! Form::label('lblFemale', 'Female') !!}
{!! Form::radio('sex', 'female') !!}
its working properly , but i want on clicking on male label male check box should check and clicking on Female label it should select female checkbox in blade

you can do radio button like this in laravel blade
<label for="male">
<input name="male" id="male" value="male" type="radio">
Male
</label>
<label for="female">
<input name="female" id="female" value="female" type="radio">
Female
</label>
For this html you can use this code in blade
{{ Form::label('male', 'Male') }}
{{ Form::radio('sex', 'male', array('id'=>'male','label'=>'female')) }}
{{ Form::label('female', 'Female') }}
{{ Form::radio('sex', 'female', array('id'=>'male','label'=>'female')) }}

Related

Laravel Blade - How to make checkbox stay checked after submit

I want to create a feature to filter data. I use the checkbox to do this.
But I want when the checkbox is selected and then the user submits to filter the data, the previously selected checkbox remains selected.
i tried to do it like this but it didn't work
<input type="checkbox" id="ac" name="ac" value="ac" #if(old('ac')) checked #endif>
my form method to submit this is GET.
old() helper function is only for saved flash session data. Without saved it on flash session you can not retrieve it. An example to save flash data for old() function
request()->flashOnly(['ac']);
or
return redirect('form')->withInput();
If you use GET method and no redirection after submit you can do it using request() helper function. Like this:
<input type="checkbox" id="ac" name="ac" value="ac" #if(request()->ac) checked #endif>
For the submitting form using POST method with multiple checkboxes,
#foreach ($categories as $index => $category)
<div class="form-check form-check-inline">
<input type="checkbox" name="type[]"
value="{{ $category->id }}"
{{ (is_array(old('type')) && in_array($index + 1, old('type'))) ? 'checked' : '' }}
class="form-check-input #error('type') is-invalid #enderror"
id="type_{{$category->id}}">
<label class="form-check-label" for="type_{{$category->id}}">
{{ $category->name }}
</label>
</div>
#endforeach
This worked form me:
<input name="mail" type="hidden" value="0" >
<input name="mail" type="checkbox" value="1"#if(old('mail')) checked #endif>
controller:
$request->validate([
'mail' => 'required',
]);
Contact::create($request->all());

collective form convert html form

This is laravel collective form, This is a dropdown field, its connect with database. How can i convert normal html form???
{!! Form::open(['method'=>'POST', 'action'=> xyz\AddController#store', 'id'=> 'form']) !!}
<div class="form-group col-sm-5">
{!! Form::label('student_id', 'Students:') !!}
{!! Form::select('student_id', [''=>'Choose Options'] + $students , null, ['class'=>'form-control', 'id'=>'student', 'name'=>'student_id'])!!}
</div>
{!! Form::close() !!}
I want to convert format like this
<div class="form-group col-lg-5">
<label for="qty">Traded Quantity</label>
<input type="text" name="qty" id="qty" class="form-control" />
</div>
If students list is coming through database then select tag can be like this,
<select id="student" class="form_control" name="student_id">
<option>Choose Options</option>
#foreach($students as $student)
<option value="{{$student->id}}">{{$student->id}}</option>
#endforeach
</select>

Getting checkboxes ticked based on the value extracted from database for edit form

I have my values stored in database as follows:
I am able to get all the values in my form from database for editing purpose as follows:But I am not able to get checkboxes ticked based on the values that I have stored in my database i.e. if I have MBBS and BDS in my database, i would like to have MBBS and BDS checkboxes ticked in my edit form.
For single value in course column, i was able to achieve single tick as follows:
<div class="form-group">
<label for='degree'>Degree : </label>
<label class="checkbox-inline">
<input type="checkbox" value="MBBS" id="course" name="course"
#if($book->course == "MBBS")
{{"checked" }}
#endif
/>MBBS
</label>
<label class="checkbox-inline">
<input type="checkbox" value="BDS" id="course" name="course"
#if($book->course == "BDS")
{{"checked" }}
#endif
/>BDS
</label>
<label class="checkbox-inline">
<input type="checkbox" value="B.Pharma" id="course" name="course"
#if($book->course == "B.Pharma")
{{"checked" }}
#endif/>B.Pharma
</label>
</div>
How can i access the values in checkboxes of my edit form?
Your course seem to have comma separated value, so you need to explode it and cross check it.
Try this to get a match
#if(in_array("MBBS", explode(",", $book->course)))
{{"checked" }}
#endif

Use <span> inside Form Label in Blade template

In html I can use <span> tag inside form label like this:
<label for="name" class="class-name">Name:<span class="required"></span></label>
Using Laravel Blade, the code for label is like this:
{!! Form::label('name','Name:',['class'=>'class-name']) !!}
How can I use <span> inside form label using blade template?
Try this one code:
{!! Html::decode(Form::label('name','Name: <span class="required"></span>')) !!}
Just set the 4th parameter of Form::label to false which is escape_html = false.
{!! Form::label('name','Name: <span class="required"></span>', [], false) !!}

LARAVEL styled password field with bootstrap

in this form i heve Form::password() and i can not styled or embeded class for that. for example this below class and style for Form::text can work correctly.
{{ Form::text('username', Input::old('username'), array('placeholder'=>'UserName', 'class'=>'form-control' ) ) }}
Generated HTML:
<input id="username" class="form-control" type="text" name="username" placeholder="username">
but for Form::password() do not work:
{{ Form::password('password', null, array('placeholder'=>'Password', 'class'=>'form-control' ) ) }}
Generated HTML:
<input id="password" type="password" value="" name="password">
This will work:
{{ Form::password('password', array('placeholder'=>'Password', 'class'=>'form-control' ) ) }}
You don't need the null as you cannot specify a default value for password fields.
You can also use a custom HTML macro that enables the syntax similar to your first example.
See my answer here:
How to pass value to Password field in Laravel
This problem occurred when the null value in the password.
{!! Form::password('password', old('password'), ['class' => 'form-control ', 'placeholder' => 'Enter Password']) !!}
Please Remove the old('password'):
{!! Form::password('password', ['class' => 'form-control', 'placeholder' => 'Enter Password']) !!}