displaying specific error messages in laravel 4.2 - laravel

hello i have this form and i have validations for it. I have already finished doing the validations for in inside my controller and i can already display the error messages in my view but i wanted the error message to be beside the input area where it came from
here is my code in the view
{{ Form::open(array('url' => 'addParentAccnt')) }}
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username'), array('class' => 'form-control','placeholder' => 'Insert username')) }}
</div>
<div class="form-group">
{{ Form::label('fName', 'First Name') }}
{{ Form::text('fName', Input::old('fName'), array('class' => 'form-control','placeholder' => 'Insert First Name')) }}
</div>
<div class="form-group">
{{ Form::label('lName', 'Last Name') }}
{{ Form::text('lName', Input::old('lName'), array('class' => 'form-control','placeholder' => 'Insert Last Name')) }}
</div> {{ Form::submit('Proceed to Next Step', array('class' => 'btn btn-primary')) }}
{{ Form::close()}}
in the bottom of my view i added this code to display the error messages
#if ($errors->any())
<ul>
{{ implode('', $errors->all('<p style="color:red" class="error">:message</p>')) }}
</ul>
#endif
the code inside my controller is this
$rules = array
(
'username' => 'required|min:10|max:50',
'fName' => 'required|alpha|min:1|max:80',
'lName' => 'required|alpha|min:1|max:80',
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
return Redirect::to('createPa')
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
//do something
}

Change your view as following:
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username'), array('class' => 'form-control','placeholder' => 'Insert username')) }}
{{ $errors->first('username', ':message') }}
</div>
<div class="form-group">
{{ Form::label('fName', 'First Name') }}
{{ Form::text('fName', Input::old('fName'), array('class' => 'form-control','placeholder' => 'Insert First Name')) }}
{{ $errors->first('fName', ':message') }}
</div>

Related

Laravel form model attributes

How to pass attributes like id, class to form model?
This what i try not working and in official Laravel documentation wass not defined.
#if(isset($country))
{{ Form::model($country, ['route' => ['country.show', $country->id]], ['class' => "123"]) }}
#else
{{ Form::open(['route' => 'country.store', 'id'=> 'admin_store_form' ]) }}
#endif
<div class="row mb-2">
<div class="col-sm-1">
{{ Form::label('name', 'Name', ['class' => 'col-sm-2 admin-label col-form-label']) }}
</div>
<div class="col-sm-3">
{{ Form::text('name', old('name'), ['placeholder'=>'Name', 'class' => 'form-control form-control-sm admin-control']) }}
</div>
</div>
{{ Form::close() }}
I define 'class' => "123" but that not work.
The correct way to add a class to Form::model is:
{{ Form::model($country, ['route' => ['country.show', $country->id], 'class' => '123']) }}
You need to add the class key to the same array argument as route.

Refresh datatable after updating row using modal window

I'm using Jquery Datatable for a small project along with Laravel 5.6,
one of the datatable columns allows to edit a specific row , (by the way, I'm not using datatable editor plugin which has to be adquired).
the modal window contains the update form with the jquery logic, when i click update i don't see the updated row changes on datatable.
modal window to update datatable row
modal window code:
<div class="row padded">
<div class="col-md-12">
<div class="form-group">
</div>
{!! Form::open([ 'route' =>['srh.prestamo_pago.update', $prestamo_pago->id] ,'method' => 'PUT', 'id' => 'frmEdit_pago']) !!}
<div class="form-group">
{{ Form::label('Nro Cuota', 'Nro Cuota') }}
{{ Form::text('nro_cuota', $prestamo_pago->nro_cuota, array('class' =>'form-control', 'required' =>'', 'max-length' => '50', 'id' => 'nro_cuota')) }}
</div>
<div class="form-group">
{{ Form::label('Monto Cuota', 'Monto cuota') }}
{{ Form::text('monto_cuota', $prestamo_pago->monto_cuota, array('class' =>'form-control', 'required' =>'', 'max-length' => '50','id' => 'monto_cuota', "readonly" )) }}
</div>
<div class="form-group">
{{ Form::label('Fecha Pago', 'Fecha Pago') }}
#if(isset($prestamo_pago->fecha_pago))
{{ Form::text('fecha_pago', date("d/m/Y", strtotime($prestamo_pago->fecha_pago)) , array('class' =>'form-control datepicker1', 'required' =>'', 'max-length' => '50','id' => 'fecha_pago')) }}
#else
{{ Form::text('fecha_pago', null , array('class' =>'form-control datepicker1', 'required' =>'', 'max-length' => '50','id' => 'fecha_pago')) }}
#endif
</div>
<div class="form-group">
{{ Form::label('Fecha Vencimiento', 'Fecha Vencimiento') }}
{{ Form::text('fecha_vencimiento', date("d/m/Y", strtotime($prestamo_pago->fecha_vencimiento)), array('class' =>'form-control', 'required' =>'', 'max-length' => '50','id' => 'fecha_vencimiento', 'readonly'=>'')) }}
</div>
<div class="form-group">
{{ Form::label('Seleccione', 'Seleccione Estado') }}
{{ Form::select('estado_cuota', array( 'CANCELADO' =>'CANCELADO', 'PENDIENTE' =>'PENDIENTE'), $prestamo_pago->estado_cuota, array('class' =>'form-control', 'required' =>'', 'id' => 'estado_cuota'))}}
</div>
<div class="form-group">
{{ Form::label('Tipo Asiento', 'Tipo Asiento') }}
<input name="tipo_asiento" type="text" value="{{ $prestamo_pago->tipo_asiento_ingreso }}" id="tipo_asiento" class="form-control" readonly="readonly">
</div>
<div class="form-group">
{{ Form::label('Tipo Obligacion', 'Tipo Obligacion') }}
<input name="tipo_obligacion" type="text" value="{{$prestamo_pago->tipo_obligacion_ingreso}}" id="tipo_obligacion" class="form-control" readonly="readonly">
</div>
<div class="form-group">
{{ Form::label('Codigo Cuenta', 'Codigo Cuenta ') }}
{{ Form::text('cod_cuenta', $prestamo_pago->cod_cuenta, array('class' =>'form-control', 'required' =>'', 'max-length' => '50','id' => 'cod_cuenta','readonly'=>'')) }}
</div>
<div class="form-group">
{{ Form::label('Observación')}}
{{ Form::textarea('glosa', null,
array('class' => 'form-control',
'id' => 'glosa',
'size' => '30x3',
'required' => '',
'placeholder' => 'Ingrese Observación')) }}
</div>
<div class="form-group">
{{ Form::hidden('id_cuota', $prestamo_pago->id, array('id' => 'id_cuota')) }}
{{ Form::hidden('estado_final_cuotas','', array('id' => 'estado_final_cuotas')) }} // SI SE HAN CANCELADO TODAS LAS CUOTAS
{{ Form::hidden('prestamo_solicitud_id', $prestamo_pago->prestamo_solicitud_id, array('id' => 'prestamo_solicitud_id')) }}
{{ Form::hidden('nro_obligacion', '', array('id' => 'nro_obligacion')) }}
{{ Form::hidden('nro_asiento', '', array('id' => 'nro_asiento')) }}
{{ Form::hidden('contracuenta','', array('id' => 'contracuenta')) }}
{{ Form::submit('Actualizar Pago', array('class' => 'btn btn-primary btn-block', 'style' => 'margin-top:20px')) }}
</div>
{!! Form::close() !!}
</div>
</div>
<script type="text/javascript">
$(function(){
// FORM DATA AND HIDDEN FORM FIELDS
$('#frmEdit_pago').on('submit', function(e){
e.preventDefault();
var id = $('#id_cuota').val();
var tipo_asiento = $('#tipo_asiento').val();
var tipo_obligacion = $('#tipo_obligacion').val();
var cuenta_ingreso = $('#cod_cuenta').val();
var nro_cuota = $('#nro_cuota').val();
var prestamo_solicitud_id = $('#prestamo_solicitud_id').val();
var estado_cuota = $('#estado_cuota').val();
//pass search params and get data from database , then load hidden form fields
$.get('prestamo_pago/get_datos_contables',
{
'tipo_obligacion':tipo_obligacion,
'tipo_asiento': tipo_asiento,
'cuenta_ingreso' : cuenta_ingreso,
'nro_cuota' : nro_cuota,
'prestamo_solicitud_id' : prestamo_solicitud_id,
},
function(data) {
console.log(data);
$('#nro_obligacion').val(data.nro_obligacion);
$('#nro_asiento').val(data.nro_asiento);
$('#contracuenta').val(data.contracuenta.contracuenta_gasto);
var frm = $('#frmEdit_pago')[0];
var send = $(frm).serializeArray();
$.post(frm.action, send,
function(data) {
console.log(data);
//*****************************************************************
Here i want to refresh datatable to display updated row, I get datatable1.rows.data is not a function error, i think it doesn't get the instance of the main window.
datatable1.clear().draw();
datatable1.rows.data(data.cuotas_prestamo).draw();
//******************************************************************
toastr.success('Se ha guardado la información');
$('#modal-pago').modal('hide');
},'json'
).fail(funFail);
},'json'
).fail(funFail);
});
});
PS.- the fact that I'm getting submit code in the modal window and not on the index.php that holds datatatable, is because I put hidden fields in the form and it has to be displayed for that to happen. maybe there's a way to pass datatable instance to the modal ?

laravel 5.2 Restfull Api with admin panel stop working suddenly

I developed admin panel to add,edit,delete users in my website with Restfull Api. edit and delete work fine but add not.
when I add user it may work or this error may appear
The localhost page isn’t working
localhost is currently unable to handle this request.
This is my routes
Route::resource('admin/users','AdminUser');
This is store function in Admin user resource
public function store(Request $request)
{
//rules
$rules = array(
'name' => 'required',
'email' => 'required|email',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required|min:6'
);
/*
validate the data user
**/
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return view('admin.User.create_user')
->withErrors($validator)
->withInput(['page' => 'home']);
}
/*
Store the data user in the database
**/
$user = new User;
$user->name =$request->input('name');
$user->email = $request->input('email');
$user->password=bcrypt($request->input('password'));
$user->role='user';
$user->save(); //error here
//redirect
return redirect('admin/users')->with('message', 'Successfully added user!');
}
And this is create_user.blade.php
#extends('layouts.layout')
#section('content')
<section id="advertisement">
<div class="container">
<img src="{{asset('images/shop/advertisement.jpg')}}" alt="" />
</div>
</section>
<section>
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="left-sidebar">
#include('shared.sidebaradmin')
</div>
</div>
<div class="features_items"><!--features_items-->
<h2 class="title text-center">Add New Product</h2>
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('url' => 'admin/users')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name',null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::email('email',null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password_confirmation', 'Confirm Password') }}
{{ Form::password('password_confirmation', array('class' => 'form-control')) }}
</div>
{{ Form::submit('Add a new user !', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div><!--features_items-->
</div>
</div>
</section>
#endsection

What am I doing wrong with my Form Model Binding?

I have method in a controller returning Employer based an id which it does...
public function talentProfile()
{
$user = Auth::user()->CandidateID;
$test_user = User::find($user);
foreach( $test_user->workHistory as $history ) {
echo $history;
}
return View::make('account.TalentProfile')->with( [ 'history' => $history, 'user' => $test_user] );
}
In my view I have a form I am trying to bind these result to. Looks like this....
#foreach( $history as $workHistory )
{{ Form::model($workHistory, array('route' => 'work-history-create', 'class' => 'form-horizontal work-history', 'files' => true) ) }}
<!-- Text input-->
<div class="form-group">
{{ Form::label('CompanyName', 'Company Name', array('class' => 'control-label') ) }}
{{ Form::text('CandidateID', null, array('class' => 'form-control', 'placeholder' => 'Company Name', 'title' => 'Company Name', 'required') ) }}
</div>
<!-- Text input-->
<div class="form-group">
{{ Form::label('JobTitle', 'Job Title', array('class' => 'control-label') ) }}
{{ Form::text('JobTitle', null, array('class' => 'form-control', 'placeholder' => 'Job Title', 'title' => 'JobTitle', 'required') ) }}
</div>
<!-- Text input-->
<div class="form-group">
{{ Form::label('Duties', 'Duties', array('class' => 'control-label') ) }}
{{ Form::text('Duties', null, array('class' => 'form-control', 'placeholder' => 'Duties', 'title' => 'Duties', 'required') ) }}
</div>
<div class="form-group">
{{ Form::label('Salary', 'Salary', array('class' => 'control-label') ) }}
{{ Form::number('Salary', null, array('class' => 'form-control', 'placeholder' => 'Salary', 'title' => 'Salary', 'required') ) }}
</div>
<div class="form-group">
{{ Form::label('NumDirectReports', 'Lackies', array('class' => 'control-label') ) }}
{{ Form::number('NumDirectReports', null, array('class' => 'form-control', 'placeholder' => 'Lackies', 'title' => 'Lackies', 'required') ) }}
</div>
<!-- Month/Year select -->
<div class="control-group">
{{ Form::label('EmploymentStart', 'Start Date', array('class' => 'control-label') ) }}
<div class="span3">
{{ Form::selectMonth('EmploymentStartMonth', null, array('class' => 'col-md-13', 'title' => 'employmentStartMonth') ) }}
</div>
<div class="span2">
{{ Form::selectYear('EmploymentStartYear', 1980, 2055, null, array('class' => 'col-lg-3', 'title' => 'employmentStartYear') ) }}
</div>
<!-- Month/Year select -->
<div id="fromPosition">
{{ Form::label('EmploymentEnd', 'End Date', array('class' => 'control-label') ) }}
<div class="span3">
{{ Form::selectMonth('EmploymentEndMonth', null, array('class' => 'col-md-13', 'title' => 'employmentEndMonth') ) }}
</div>
<div class="span2">
{{ Form::selectYear('EmploymentEndYear', 1980, 2055, null, array('class' => 'col-lg-3', 'title' => 'employmentEndYear') ) }}
</div>
</div>
<div class="span2">
{{ Form::checkbox('current-position', null, false, array('id' => 'current-position') ) }} This is my current position
</div>
</div>
#endforeach
My problem is the form is empty. I can dump $history right before the form and see my results. I don't know how to get those results into my form. In this scenario there are two results in $history so I need to have two separate forms displayed and populated with the binded data.
Hope this makes sense. Would love some help here. I'm struggling to understand why this doesn't work.
Thank you,
Your example is missing {{ Form::close() }}. Could it be that simple?
EDIT:
You are opening a new form with each loop in your example, but you never close any of them, as far as I can tell. If you only mean to open one form for the whole thing, you will not be able to use model binding, since you can only bind one model per form that way.

laravel blade form show me an exception error

First of all sorry for my bad english.
I have a laravel form using blade template engine that show me an exception error. If i remove the code and use the php form with echo statmente everything fine and the page show up.
where is the problem? Here is my code
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => 'news')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::title('name', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', Input::old('description'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Create the Nerd!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
There is no such thing as Form::title() in laravel.
Try this
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => 'news')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', Input::old('description'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Create the Nerd!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
Make sure you figure these errors yourself as these are typing mistakes.

Resources