Error Message in Laravel - redirect

Im using redirect::back()->withErrors($exception) but not able to display error message on my page.
Here is my controller
try{
}
catch(ParseException $pex){
$error=var_dump($pex->getMessage());
return Redirect::back()->withInput()->withErros(array('msg'=>$error));
}
and here is my view
{ Form::open(array('action' =>'UsersController#register','class' => 'form-horizontal','id'=> 'registrationForm' ,'name' => 'registrationForm','method' => 'POST')) }}
<div class="body bg-gray">
<!--Kullanicinin girmesi gereken bilgiler-->
#if($errors->any())
<div class="form-group has-error">
<div class="col-sm-12">
<h4>Error:{{$erros->first('code')}}</h4><br/>
<label>{{$errors->first('msg')}}</label>
</div>
</div>
#endif
Any ideas?
Thanks

Related

storing form and rout

I have problem on storing form . All my efforts will be damaged if I could not solve it.
The GET method is not supported for this route. Supported methods: POST.
what happen now :
1.When I press on register which is linked to this rout :
Route::POST('/jobs/apply/{id}/', [JobseekerController::class,'applyforjob'])->name('aplpy1');
there will be two scenarios:
if the user has cv it will register the user directly.(works)
if the user has not cv it will direct the user to create cv view .(works)
This is a part of create cv view :
<form method="POST" action="{{route('storing')}}" enctype=multipart/form-data>
#csrf
<input type="hidden" name=job_id value= {{$s}}>
#if (count($errors) > 0)
<div class="alert alert-danger alert-dismissible fade show">
<strong>Opps!</strong> Something went wrong, please check below errors.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#endif
<div class="card-body">
<div class="container">
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="accordion" class="checkout">
<div class="panel checkout-step">
<div> <span class="checkout-step-number">1</span>
<h4 class="checkout-step-title"> <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" > Personal Information</a></h4>
</div>
<div id="collapseOne" class="collapse in">
<div class="checkout-step-body">
<div class="row mt-3 pr-3">
<div class="col-6 ">
<strong>Full Name:</strong>
{!! Form::text('FName', null, array('placeholder' => 'Full Name','class' =>
'form-control pb-4')) !!}
</div>
<div class="col-6">
<div class="row mt-3 mr-2">
<strong>Nationality:</strong>
<select name="Nationality" class="custom-select">
#foreach(\App\Models\Nationality::all() as $nation)
<option value="{{$nation->name}}">{{$nation->name}}</option>
#endforeach
</select>
</div>
<div class="form-group ">
<button type="submit" name="submit" class="btn btn-primary ">{{ __('APPLAY') }}</button
</div>
<input type="hidden" name=job_id value= {{$s}}>
</form>
2.Now Here is the problem:
If I fill all the fields on the form correctly and click apply it works , but if there is missing field or anything not filled as it should be and then click apply , although there is validation on controller it does not work it gives this error:
The GET method is not supported for this route. Supported methods: POST.
Although I am using Post this my rout that I use to store the form
Route::POST('/jobsapply', [JobseekerController::class,'store'])->name('storing');
This my controller for storing:
public function store(Request $request )
{
$this->validate($request, [
'FName' => 'required|alpha|regex:/^([^"!\*\\]*)$/',
'Nationality' => 'required',
]);
$input = $request->all();
$input = $request->except(['_token']);
$cv = new cv();
if ($this->cvsave($cv, $request )) {
$data = Job::all();
$cv = auth()->user()->cv;
return view ('show',compact('cv' ,'data'))->with('success', 'CV created successfully.');
}
$data = Job::all();
return view('show')->with('Failed!', 'error');
}
public function cvsave(cv $cv, Request $request)
{
$cv->FName = $request->FName;
$cv->Nationality = $request->Nationality;
if ($cv->save()) {
$application = new JobApplication;
$id=$request->job_id;
$jobs = Job::find($id)->get();
$application->job_id =$request->job_id;
$user = User::find(auth()->user()->id);
$application->user_id = auth()->user()->id;
$cv = auth()->user()->cv;
$i= $cv->id;
$application->cv_id = $i;
$application->save();
\Session::flash('success', 'Thank you for applying! Wait for the company to respond.!');
return true;
}
return false;
}
I could not know the reason, I have read lots of stuff but still the same error

Symfony flash message and data not refreshed after submission of a form

I am using a modal form to update an address. Everything is working properly except that the flash message is not displayed or the view data updated after the form is submitted and therefore the page reloads. For the message to appear and the data to be updated, I have to manually refresh the page.
Here is the call of the modal form in my view :
...
{{ include('_flashMessages.html.twig') }}
...
Edit
...
<div id="profileAddress" tabindex="-1" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Profile address</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ render(controller('App\\Controller\\user\\ProfileController::editAddress', { 'advert': advert.id, 'request': app.request })) }}
</div>
</div>
</div>
</div>
...
Here is the function called to render the form :
/**
* #Route("/user/profile/show/{id}", name="user.profile.show")
*
* #return Response
*/
public function show(User $user): Response
{
$profile = $user->getProfile();
return $this->render('user/profile/show.html.twig', array(
'profile' => $profile,
'bodyId' => 'profileShow'
)
)
;
}
And finally, here is the action that I linked to my form to redirect to the initial view :
{{ form_start(form, { 'action': absolute_url(path('advert.vehicle.create', { 'id': advert.id })), 'attr': { 'id': 'profileAddressForm' } }) }}
...
Anyone have an idea of the cause of the flash message not being displayed and the address data not being updated in the initial view?
Thank you in advance for your help.
EDIT :
I solved 1 problem out of 2: by placing my modal div at the start of the template, the success flash message is now displayed correctly after submitting the form.
It remains now to understand why the data which has been correctly updated via the use of the said form is not refreshed in my view. Indeed, in this one, I have :
...
{% set profileAddress = advert.owner.user.profile.address.street %}
...
<div class="row">
<div class="col-md-10">
{{ form_row ( vehicleForm.situation.useProfileAddess, { 'id':'use_profil_address', 'label': "Use my profile address (" ~ profileAddress ~ ")" } ) }}
</div>
<div class="col">
Edit
</div>
</div>
...
The form updates the address data in the database, but these are not refreshed in the view when the page reloads after the form is submitted.
Resolved by
{% set profileAddress = advert.owner.user.profile.address %}
and
{{ form_row ( vehicleForm.situation.useProfileAddess, { 'id':'use_profil_address', 'label': "Use my profile address (" ~ profileAddress.street ~ ")" } ) }}

How can I validate a date in a form in Phalcon?

I build a webpage using the framework Phalcon. I have a form asking for a text, an integer and a date. I'm using datepicker (https://jqueryui.com/datepicker/) to get the date. When I make the insertion in the database this date is not inserted, only the text and the int.
I think it is problem of the format of the date, so I want to validate it.
I can validate the presence of data in the text fields but I don't find any "validator" to validate it is a date.
This is my form class:
<?php
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Date;
use Phalcon\Forms\Element\Password;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
class NuevakeywordForm extends Form{
public function initialize($entity = null, $options = null){
// Trackeable
$trackeable = new Text('trackeable');
$trackeable->setLabel('Palabra Clave');
$trackeable->setFilters(array('striptags', 'string'));
$trackeable->addValidators(array(
new PresenceOf(array(
'message' => 'Palabra clave requerida'
))
));
$this->add($trackeable);
// IdCliente
$idcliente = new Text('idcliente');
$idcliente->setLabel('idcliente');
$idcliente->addValidators(array(
new PresenceOf(array(
'message' => 'IdCliente is required'
))
));
$this->add($idcliente);
// Fecha Límite
$fecha = new Text('fecha_final');
$fecha->setLabel('Fecha Final');
$fecha->addValidators(array(
new PresenceOf(array(
'message' => 'Por favor pon una fecha límite'
))
));
$this->add($fecha);
}
}
Here is the view:
<div class="page-header">
<h2>Registro de Nueva Keyword</h2>
</div>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<section class="container animated fadeInUp">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div id="login-wrapper">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
Registrar Nueva Keyword
</h3>
</div>
<div class="panel-body">
{{ form('admin/nuevakeyword/', 'id': 'nuevakeywordForm', 'onbeforesubmit': 'return false') }}
<fieldset>
<div class="control-group">
{{ form.label('trackeable', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('trackeable', ['class': 'form-control']) }}
</div>
</div>
<div class="control-group">
{{ form.label('idcliente', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('idcliente', ['class': 'form-control']) }}
</div>
</div>
<div class="control-group">
{{ form.label('fecha_final', ['class': 'control-label']) }}
<div id="datepicker" class="controls">
{{ form.render('fecha_final', ['class': 'form-control']) }}
</div>
</div>
<div class="form-actions">
{{ submit_button('Insertar', 'class': 'btn btn-primary', 'onclick': 'return SignUp.validate();') }}
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
$(function() {
$("#fecha_final").datepicker();
});
</script>
Here is the controller:
public function nuevakeywordAction(){
$auth = $this->session->get('auth');
$permiso = $auth['active'];
if($permiso!='A'){return $this->forward('servicios/index');}
$form = new NuevakeywordForm;
if ($this->request->isPost()) {
$trackeable = $this->request->getPost('trackeable', array('string', 'striptags'));
$idcliente = $this->request->getPost('idcliente');
$fecha = $this->request->getPost('fecha');
$keyword = new Keyword();
$keyword->trackeable = $trackeable;
$keyword->cliente_idcliente = $idcliente;
$keyword->fecha_inicial = new Phalcon\Db\RawValue('now()');
$keyword->fecha_final = $fecha;
if ($keyword->save() == false) {
foreach ($keyword->getMessages() as $message) {
$this->flash->error((string) $message);
}
} else {
$this->tag->setDefault('trackeable', '');
$this->tag->setDefault('idcliente', '');
$this->tag->setDefault('fecha', '');
$this->flash->success('Keyword agregada correctamente');
return $this->forward('admin/verkeywords');
}
}
$this->view->form = $form;
}
I'm using MariaDB (the same as MySQL). I'm trying to find an "addValidator" for the date, but maybe is other the problem. Any help would be welcomed.
Use Phalcon\Validation\Validator\Date
https://docs.phalconphp.com/en/3.0.0/api/Phalcon_Validation_Validator_Date.html

My form is not populating when there is some validation error in the input data. Laravel Collective

Hope yo are all doing great.
I am using Laravel 5.3 and a package LaravelCollective for form-model binding.
I have subjects array that I want to validate and then store in database if there is no error in the input data.
The following snippets show the partial view of form, other views, rules for validations and controller code.
UserProfile.blade.php
<!-- Panel that Adds the Subject - START -->
<div class="col-md-12">
<div class="panel panel-default" id="add_Subject_panel">
<div class="panel-heading">Add Subject(s):</div>
<div class="panel-body">
{!! Form::open( array('method'=>'post', 'url'=>route('user.store.subject', 1)) ) !!}
#include('user.partials.subjects', ['buttonText'=>'Add Subject(s)'])
{!! Form::close() !!}
</div>
</div>
</div>
<!-- Panel that Adds the Subject - END -->
subjects.blade.php
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="subject-list">
<div class="input-group subject-input">
<input type="text" name="name[]" class="form-control" value="" placeholder="Subject" />
<span class="input-group-btn">
<span class="btn btn-default">Primary subject</span>
</span>
</div>
</div>
<div class="text-right">
<br />
<button type="button" class="btn btn-success btn-sm btn-add-subject"><span class="glyphicon glyphicon-plus"></span> Add Subject</button>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4 text-right">
<button type="submit" class="btn btn-primary">
{{ $buttonText }} <i class="fa fa-btn fa-subject"></i>
</button>
</div>
</div>
#push('scripts')
{{-- <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> --}}
<script src="{{ asset('scripts/jquery-2.2.4.min.js') }}" type="text/javascript" charset="utf-8" async defer></script>
<script>
$(function()
{
$(document.body).on('click', '.btn-remove-subject', function(){
$(this).closest('.subject-input').remove();
});
$('.btn-add-subject').click(function()
{
var index = $('.subject-input').length + 1;
$('.subject-list').append(''+
'<div class="input-group subject-input" style="margin-top:20px; margin-bottom:20px;">'+
'<input type="text" name="name['+index+']" class="form-control" value="" placeholder="Subject" />'+
'<span class="input-group-btn">'+
'<button class="btn btn-danger btn-remove-subject" type="button"><span class="glyphicon glyphicon-remove"></span></button>'+
'</span>'+
'</div>'
);
});
});
</script>
#endpush
I want to validate all the subjects passed as an array to the controller using a custom created form request. the following is the code for that form request
SubjectRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SubjectRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
// dd("Rules Area");
foreach($this->request->get('name') as $key => $val)
{
$rules['name.'.$key] = 'required|min:5|max:50';
}
// dd($rules);
return $rules;
}
public function messages()
{
// dd('Message Area. . .');
$messages = [];
foreach($this->request->get('name') as $key => $val)
{
$messages['name.'.$key.'.required'] = ' Subject Name '.$key.'" is required.';
$messages['name.'.$key.'.min'] = ' Subject Name '.$key.'" must be atleast :min characters long.';
$messages['name.'.$key.'.max'] = ' Subject Name '.$key.'" must be less than :max characters.';
}
// dd($messages);
return $messages;
}
}
the following is the code for controller method I am using to store inputted array data to database.
public function storeSubjects(SubjectRequest $request)
{
$data = $request->all();
// save logic
dd($data);
}
Problem:
My form is not populating when there is some validation error in the input data.
After getting validation errors, my input fields are blank.
Problem
In subject.blade.php, your code looks like
<input type="text" name="name[]" class="form-control" value="" placeholder="Subject" />
Your fields are not populating because you have left value attribute blank.
Solution:
Pass the old value to solve your problem.
<input type="text" name="name[]" class="form-control" value="{{old('name[]')}}" placeholder="Subject" />
Use Form Model Binding instead
https://laravelcollective.com/docs/5.3/html#form-model-binding
{{ Form::model($user, [....]) }}
{{ Form::text('firstname', null, [...]) }}
{{ Form::close() }}

Laravel 4 Preview Form Submit

Any idea how to solve the following approach.
I have a form and want to display the entered data in a specific formtemplate before store it in the DB. If the entered data looks properly, the user can save the form.So I am searching for a way to display the entered data as a preview in a new window/tab first. With my code below I am not able to preview the form without saving the data in the databse. Also display the preview in a new window or tab is not possible. I guess there is no way to achieve this with php / laravel. I tried some onlick events, but no luck. As it seems like the route is prefered.
Any idea how to solve this?
My form looks like:
{{ Form::open(array('url' => 'backend/menubuilder/'.$id, 'method' => 'PUT'))}}
<section>
<div class="container">
<div class="row">
<div class="inputBox">
<div class="col-xs-12 col-md-6">
<h3>Montag</h3>
<div class="form-group">
{{Form::label('gericht_1_mo','Gericht 1')}}
{{Form::textarea('gericht_1_mo', Auth::user()->gericht_1_mo,array('class' => 'form-control'))}}
</div>
<div class="form-group">
{{Form::label('preis_1_mo','Preis', array('class' => 'col-md-6'))}}
{{Form::text('preis_1_mo', Auth::user()->preis_1_mo, array('class' => 'col-md-6'))}}
</div>
<div class="form-group mrgT55">
{{Form::label('gericht_2_mo','Gericht 2')}}
{{Form::textarea('gericht_2_mo', Auth::user()->gericht_2_mo,array('class' => 'form-control'))}}
</div>
<div class="form-group">
{{Form::label('preis_2_mo','Preis', array('class' => 'col-md-6'))}}
{{Form::text('preis_2_mo', Auth::user()->preis_2_mo, array('class' => 'col-md-6'))}}
</div>
</div>
</div>
</div>
</div>
</div>
{{Form::submit('update')}}
{{-- <input type="submit" name="preview" value="preview"> --}}
{{Form::close()}}
{{ Form::open(array('url' => 'backend/menubuilder/templatesview/'.$id, 'method' => 'POST'))}}
{{-- {{Form::submit('Preview',array('onClick' => 'target_blank'))}} --}}
<input onclick="newTab()" type="submit" name="preview" value="preview" >
{{Form::close()}}
My routes:
Route::get('backend/menubuilder/templates/{id}', 'MenuBuilderController#template');
Route::post('backend/menubuilder/templatesview/{id}', 'MenuBuilderController#preview');
My Controller:
public function preview($id)
{
$user = User::find($id);
$owner = (Auth::id() === (int) $id);
return View::make('backend/menubuilder/templatesview/tempone')->withUser($user)->withOwner($owner);
}
public function template($id)
{
$user = User::find($id);
$owner = (Auth::id() === (int) $id);
return View::make('backend/menubuilder/templates/tempone')->withUser($user)->withOwner($owner);
}