I am currently on internship and I have to make an administrator site. I do it with FrameWork (php) -> Laravel.
I have a little problem and I can't solve it ... Or rather I don't know what to do.
In a form to create a product type, I need the name of the type and its description.
I also need to know what sub-range it is. If it has no sub-range then of the range.
I would like to make a radio button to choose between the two and when I select range, it then shows me the list of ranges.
And of course if I click on sub-range, it shows me a list of subranges.
For now, I display both drop-down lists in my code:
{!! Form::open(array('route' => 'type.store')) !!}
<div class="form-group">
{!! Form::label('name', 'Name', ['class'=>'tai2']) !!}
{!! Form::text('name', '', ['placeholder'=> 'Name' , 'class'=>'form-control contr']) !!}
#if ($errors->has('name'))
<div class="alert alert-danger">
#foreach ($errors->get('name') as $messages)
{{ $messages }}
#endforeach
</div>
#endif
</div>
<div class="form-group">
{!! Form::label('description', 'Description', ['class'=>'tai2']) !!}
{!! Form::textarea('description', '', ['placeholder'=> 'Description' , 'class'=>'form-control contr' , 'style' => 'height:180px;']) !!}
#if ($errors->has('description'))
<div class="alert alert-danger">
#foreach ($errors->get('description') as $messages)
{{ $messages }}
#endforeach
</div>
#endif
</div>
<!-- Radio button required to select a range or sub-range and display only one. -->
<div style="height:100px;">
<div class="lister">
{!! Form::label('range', 'range') !!}
{!! Form::select('range', $ranges, null, array('class'=>'form-control')) !!}
</div>
<div class="lister2" style="float:right">
{!! Form::label('sub-range', 'Sub-range') !!}
{!! Form::select('sub-range', $ranges, null, array('class'=>'form-control')) !!}
</div>
</div>
<button type="submit" class="btn btn-primary center-block">Créer</button>
{!! Form::close() !!}
{!! Form::radio('name', 'value', true) !!}
Related
I'm using Laravel forms. This is my code in view.
<?php
$options = $items->pluck('name', 'id')->toArray();
$options[''] = "Choose...";
?>
{!! Form::select('item_id', $options, array('class' => 'form-control')) !!}
#error('item_id')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
But unfortunately browser does not render css class,
<select name="item_id">
....
</select>
The third parameter for the form is the selected option, so you need to pass the array with the classes as a fourth argument. For example:
{!! Form::select('item_id', $options, null, array('class' => 'form-control')) !!}
how can I create a select box which is filled with values from the db?
The view is published with the $groups variable. In my select box i need $groups->id (hidden, only for storing) and $groups->name. This is currently my form.
{!! Form::open(array('route'=>'store.invitation')) !!}
<div class="form-group">
{{Form::label('username', 'Username')}}
{{Form::text('username', '', ['class' => 'form-control', 'placeholder' => 'Enter Username'])}}
{{Form::label('groupname', 'Gruppe')}}
{{Form::select($groups->name) }}
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Submit',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
Thanks
You should pass an array of the data that you get from the db. Like this:
Form::select('size', array('L' => 'Large', 'S' => 'Small'));
This documentation might be old, but thats how you can create Form::select in blade.
I have used this https://github.com/proengsoft/laravel-jsvalidation for client side validation.
Server side validation is working but client side onFocusout validation is not fire.
In composer file
Laravel 5.4
"proengsoft/laravel-jsvalidation": "^2.0"
In Controller
<?php
protected $validationRules=[
'email' => 'required|unique|max:255',
'name' => 'required',
'password' => 'required',
'userRoleId' => 'required'
];
public function create() {
$model = new Admuser();
$validator = JsValidator::make($this->validationRules);
$userRoleData = Userrole::orderBy('role')->pluck('role', 'userRoleId');
return view('adminlte::portaluser.create')->with([
'validator' => $validator,
'userRoleData' => $userRoleData,
]);
}
?>
And in View file for creating userdata
{!! Form::open(['url' => 'backoffice/portaluser/store', 'class' => 'form-horizontal']) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<fieldset>
<div class="col-sm-8">
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
{!! Form::label('name', 'Name:', ['class' => 'col-lg-2 control-label']) !!}
<div class="col-lg-10">
{!! Form::text('name', $value = null, ['class' => 'form-control', 'placeholder' => 'Name']) !!}
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : ''}}">
{!! Form::label('email', 'Email:', ['class' => 'col-lg-2 control-label']) !!}
<div class="col-lg-10">
{!! Form::email('email', $value = null, ['class' => 'form-control', 'placeholder' => 'Email', ]) !!}
{!! $errors->first('email', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('password') ? 'has-error' : ''}}">
{!! Form::label('password', 'Password:', ['class' => 'col-lg-2 control-label']) !!}
<div class="col-lg-10">
{!! Form::password('password', ['class' => 'form-control', 'placeholder' => 'Password', 'type' => 'password', ]) !!}
{!! $errors->first('password', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('userRoleId') ? 'has-error' : ''}}">
{!! Form::label('userRoleId', 'Select Userrole', ['class' => 'col-lg-2 control-label'] ) !!}
<div class="col-lg-10">
{!! Form::select('userRoleId', $userRoleData, '', ['class' => 'form-control' ]) !!}
{!! $errors->first('userRoleId', '<p class="help-block">:message</p>') !!}
</div>
</div>
<button type="submit" class="submitbtn btn btn-primary">Submit</button>
</div>
</fieldset>
{!! Form::close() !!}
<!-- Scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
<!-- Laravel Javascript Validation -->
<script type="text/javascript" src="{{ asset('/jsvalidation/js/jsvalidation.js')}}"></script>
{!! $validator !!}
I guess Your question is not connected with jsvalidation but the logic how the OnFocusOut working.
Test how the event works:
<!DOCTYPE html>
<html>
<body>
Enter your name: <input type="text" id="fname" onfocusout="myFunction()">
<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
<script>
function myFunction() {
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
The onfocusout event occurs when an element is about to lose focus.
Tip: The onfocusout event is similar to the onblur event. The main difference is that the onblur event does not bubble. Therefore, if you want to find out whether an element or its child loses focus, you should use the onfocusout event.
Tip: Although Firefox does not support the onfocusout event, you can find out whether a child of an element loses focus or not, by using a capturing listener for the onblur event (using the optional useCapture parameter of the addEventListener() method).
Tip: The onfocusout event is the opposite of the onfocusin event.
Be sure that you need onfocusout not onmouseleave event.
I want Form model binding for multiple objects in laracollective's Form package?
Something as following?
Form::model([$user,$vendors], array('route' => array('user.update', $user->id)))
Where can I request this feature?
I assume you're using Laravel-Collective, Unfortunately you cant do something like that. instead you can try something like this :
UPDATE
you can query all your model in your controller and combine them like this :
$user = User::where('id',$user_id)->get();
$vendor = Vendor::where('user_id',$user_id)->get();
//merge two model
$user = $user->merge($vendor);
// return $user;
return view('admin.users.edit', compact('user'))
->withTitle('Edit user');
and in your form call them like this :
{!! Form::model($user[1], ['route' => ['admin.users.update', $user],'method'=>'PUT']) !!}
#include('admin.users._formEdit')
<div>
{!! Form::submit('Save user', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
_formEdit.blade.php
<div class="form-group">
{!! Form::label('first_name', 'First Name : ') !!}
{!! Form::text('user[first_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name', 'Last Name : ') !!}
{!! Form::text('user[last_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('email', 'Email : ') !!}
{!! Form::email('user[email]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('vendor_name', 'vendor_name') !!}
{!! Form::text('vendor_name', null,['class' => 'form-control']) !!}
</div>
OR ANOTHER SOLUTION
create relation between model of your User and Vendor (one-to-one or one-to-many) example
User :
public function vendor(){
return $this->hasOne('App\Vendor','user_id');
}
Vendor:
public function user(){
return $this->belongsTo('App\User','user_id);
}
Build your response query like this :
$user = Vendor::with('user')->find($user_id);
and then in your view template :
{!! Form::model($user, ...) !!}
Vendor: {!! Form::text('vendor_name') !!}
User: {{ Form::text('user[username]') }}
{!! Form::close() !!}
Hello I'm new to laravel 5.2 and going through some lessons.
For some reason form model binding is not working for me.
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController#update', $post->id]]) !!}
I received data in $post because I'm using a workaround like this:
{!! Form::text('title', "$post->title" ,['class'=> 'form-control']) !!}
And that is showing my data.
Controller:
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller{
public function update(Request $request, $id){
$post =Post::findOrfail($id);
$post->update($request->all());
return redirect('/posts');
}
}
create.blade.php view:
#section('content')
<h1>Create Post</h1>
{!! Form::open(['method'=>'POST', 'action'=>'PostController#store']) !!}
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit('Create Post', ['class'=> 'btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#endsection
You shouldn't be quoting the value for null in your input:
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
should be
{!! Form::text('title', null, ['class'=> 'form-control']) !!}
Ok, try adding the body of the form into a partial called posts/partials/form.blade.phpand include it between the form open / model and form close tags.
Example:
posts/partials/form.blade.php
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit($formButtonText, ['class'=> 'btn-primary form-control']) !!}
</div>
posts/create.blade.php
{!! Form::open(['method'=>'POST', 'action'=>'PostController#store']) !!}
#include('posts.partials.form', [
'formSubmitButtonText' => 'Create Post'
])
{!! Form::close() !!}
posts/edit.blade.php
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController#update', $post->id]]) !!}
#include('posts.partials.form', [
'formSubmitButtonText' => 'Update Post'
])
{!! Form::close() !!}