The laravel required_if validation doesn't seem to work when you have radio buttons.
I have the following rules:
'method' => 'required|in:Email,Url',
'email' => 'required_if:method,Email'|'email',
'url' => 'required_if:method,Url'|'url',
In my form I have the following:
{!! Form::radio('method', 'Email', true ) !!}
{!! Form::radio('method', 'Url', false ) !!}
{!! Form::text('email', null, ['maxlength' => '255', 'class' => 'form-control']) !!}
{!! Form::text('url', null, ['maxlength' => '1000', 'class' => 'form-control']) !!}
But the validation doesn't seem to fire?
My mistake - added extra apostrophes to the following:
'email' => 'required_if:method,Email'|'email',
'url' => 'required_if:method,Url'|'url',
Should be:
'email' => 'required_if:method,Email|email',
'url' => 'required_if:method,Url|url',
Related
I have 2 Form::select's inside an id-else statement in my view in Laravel 5.5. How do I get the id from these? Tried to put the ID in there on several location but none works..
this is part of the view:
<div class="o-grid__col u-12/12#sm">
{!! Form::label('', __('profile.contactSalutation').'*') !!}
#if( Session::get('urlLang') == "en" )
{!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
#else
{!! Form::select(__('contact_contactSalutation'), array('Frau' => 'Frau', 'Herr' => 'Herr'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
#endif
</div>
This is the definition of Form::select:
{!! Form::select(NAME, OPTIONS, DEFAULT_SELECTED, ['id' => 'test', 'class' => 'any-class', ... so on with the extras in this array]) !!}
in your case will be:
{!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'), null, array('id' => 'YOUR ID HERE', 'class' => 'c-dropdown c-dropdown__simple u-mb-x6', 'required' => 'required')) !!}
notice the null (so nothing pre-selected) as the third argument.
Try this
$choices = ['Frau'=> 'Frau', 'Herr'=> 'Herr'];
{!! Form::select(__('contact_contactSalutation'), $choices, null, ['id' => 'my_id', 'class'=> 'c-dropdown c-dropdown__simple u-mb-x6', 'required']) !!}
or //if you want to select a default value
{!! Form::select(__('contact_contactSalutation'), $choices, $result->contact ?? null, ['id' => 'my_id', 'class'=> 'c-dropdown c-dropdown__simple u-mb-x6', 'required']) !!}
$result->contact is your DB column name
3rd argument is used for a selected value you can use coalesce Operator either from a saved value or null
Routes
Route::get('/editRoute/{id?}',['uses'=>'RouteController#edit' , 'as' =>'route.editRoute']);
Edit View blade
{!! Form::select('driver_id', ['$driver_id' => '---Select Driver---']+$drivers, null, ['class' => 'form-control']) !!}
Edit Controller
$routes = Route::find($id);
return view('route.editRoute', compact('routes'));
You can change that in one of two ways:
Add placeholder:
{!! Form::select('driver_id', $drivers, null, ['class' => 'form-control', 'placeholder'=>'---Select Driver---']) !!}
Change current select list:
{!! Form::select('driver_id', ['' => '---Select Driver---']+$drivers, null, ['class' => 'form-control']) !!}
Both option will work but first is my preferred :)
I want to show multiple selected values in laravel5.3 form
{!! Form::select('category[]', $categories['all_cat']['categories'], null,['multiple' => 'multiple'], ['class' => 'form-control', 'id' => 'category']) !!}
Try with this:
{!! Form::select('category[]', $categories['all_cat']['categories'], null, ['multiple' => true, 'class' => 'form-control', 'id' => 'category']) !!}
How can I prevent the numbers which was given as input in the form, not to go as negative value?
Here is my code:
{!! Form::input('number', 'mobile', null, array('id' => 'mobile', 'class' => 'input-lg form-control TabOnEnter', 'placeholder' => 'Eg: 9876543210', 'tabindex' => 15)) !!}
Use the min attribute like this:
<input type="number" min="0">
In Laravel:
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => ....]) !!}
Use type="number" and min=0:
<input type="number" min="0">
Or:
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => ....]) !!}
You can also try Form::number:
{!! Form::number('number', 'mobile', ['min' =>0, 'id' => ....]) !!}
You can use server side validation provided by Laravel Validator class.
https://laravel.com/docs/5.3/validation#rule-min
Problem in dependent dropdowns when editing in my yii application.
While editing, the drop downs are not automatically selected.
In my view,
array('class' => 'CButtonColumn',
'header' => 'Manage',
'template' => '{update} {view} {delete}',
'htmlOptions' => array('width' => '20%'),
'buttons' => array(
'update' => array(
'label' => '',
'imageUrl' => '',
'options' => array('class' => 'glyphicon glyphicon-pencil'),
),
'view' => array(
'label' => '',
'imageUrl' => '',
'options' => array('class' => 'glyphicon glyphicon-eye-open'),
),
'delete' => array(
'label' => '',
'imageUrl' => '',
'options' => array('class' => 'glyphicon glyphicon-remove'),
),
),
),
<div class="form-group">
<label for="reg_input" class="req">Course</label>
<?php
$course = CHtml::listData(Course::model()->findAll(), 'courseid', 'course_name');
echo CHtml::activeDropDownList($model, 'courseid', $course, array(
'empty' => 'Select Course', 'class' => "form-control",
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('Assignment/Fetchbatch'),
'update' => '#' . CHtml::activeId($model, 'batchid'))));
?>
<?php echo $form->error($model, 'courseid', array('class' => 'school_val_error')); ?>
</div>
<div class="form-group">
<label for="reg_input" class="req">Batch</label>
<?php
$batch = CHtml::listData(Batch::model()->findAll(), 'batchid', 'batch_name');
echo $form->dropDownList($model, 'batchid', $batch, array('prompt' => 'Select Batch',
'class' => "form-control",
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('Assignment/Fetchsubject'),
'update' => '#' . CHtml::activeId($model, 'subjectid'))));
echo $form->error($model, 'batchid', array('class' => 'school_val_error'));
?>
</div>
Second dropdown get the data, to change of first dropdown. At this condition the dropdown will not be selected automatically. Because when editing, that value is not there. So I fixed this problem, my code is above this.