HTML form array validation in Laravel 5.2 - php

How to validate an array coming from HTML form?
ex:
<input type="text" name="test" value="[value1,value2,value3]">
In Laravel:
$validate = Validator::make(
$request->all(),
['test.*'=> 'max:10']
);
But in PHP code the trick doesn't work

In laravel you could try this:
Controller -
public function someAction(){
$validator = Validator::make($request->all(),
[
'test' => 'required|max:10',
'test2' => 'required|max:2'
]
);
if($validator-fails()){
return Somthing;
}else{
// Pass the form input fields values for use
$test = Input::get('test');
$test2 = Input::get('test2');
}
}
Or try this method given here

Related

Saving data from dynamic form in Laravel

I have been trying to save data from dynamic form in Laravel 5.3. But I cannot save it as array. The error shows
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given...
Form:
<select class="form-control-sm" name="client_id[]">
<input type="text" class="form-control-sm" name="amount[]">
Model:
protected $fillable = ['client_id', 'amount'];
public function client()
{
return $this->belongsTo('App\Client');
}
Controller:
public function store(Request $request)
{
$count = Client::count();
$payment = Payment::create(['amount' => $request->amount,
'client_id' => $request->client_id,
]);
$payment->save();
return redirect()->action('PaymentController#index');
}
Please help on this. Thank you.
you are submitting form with array of text fields and select box, try
below
public function store(Request $request)
{
$count = Client::count();
foreach( $request->client_id as $key=>$val){
$payment = Payment::create(['amount' => $request->amount[$key],
'client_id' => $val,
]);
}
return redirect()->action('PaymentController#index');
}
Ty to create the record like this:
$payment = Payment::create($request->input);
And change you redirect action to this:
View::make('path/to/view/')
or just use just back(); just to test if it works

How to solve Method App\JenisSurat::__toString() must return a string value on Laravel 5?

I want to input data to database. I am using Laravel 5. When I clicked the submit button. I got an error like the image below. Here is controller:`
public function tambahjenissurat(Request $request)
{ $this->validate($request, [
'jenis_surat' => 'required'
]);
$jenis_surat = $request['jenis_surat'];
$jenis_surat = new JenisSurat();
$jenis_surat->jenis_surat = $jenis_surat;
$jenis_surat->save();
return redirect()->route('jenissurat');
}`
Your code is expecting a string but you are passing an object. that could be the problem. try to give different names for object and variable. Something like this should fix the problem. See the lines below EDIT sections
public function tambahjenissurat(Request $request)
{ $this->validate($request, [
'jenis_surat' => 'required'
]);
**EDIT**
$jenis_surat_var = $request['jenis_surat'];
$jenis_surat = new JenisSurat();
**EDIT**
$jenis_surat->jenis_surat = $jenis_surat_var;
$jenis_surat->save();
return redirect()->route('jenissurat');
}

get user input using Request in laravel and pass it for validation

i am new to laravel.Here i have a form where i have to fill up a name field and send it to controllers store() method for validation. Otherwise it will show custom error.But whenever i submit the form with or without input i am getting the following error.
Argument 1 passed to Illuminate\Validation\Factory::make() must be of
the type array, string given, called in
C:\xampp\htdocs\mylaravel\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php
on line 221 and defined
for experiment purpose i am catching the user input using the following format
$data = $request->input('name');
create.blade.php:
<h1>login form</h1>
#if($errors->has())
<div><span> Opps !! </span></br>
<ul>
#foreach ($errors->all() as $error)
<li> {{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{!!Form::open(array('url'=>'user','method'=>'POST', 'files'=>true)) !!}
{!!Form::label('name','Your name')!!}
{!!Form::text('name')!!}
</br>
{!!Form::submit('submit')!!}
{!!Form::close()!!}
store() method in userController.php file:
public function store(Request $request)
{
//
$data = $request->input('name');
$rules = array(
'name' => 'unique:users,name|required|alpha_num'
);
// Create a new validator instance.
$validator = Validator::make($data, $rules);
if($validator->fails()){
$errors=$validator->messages();
return Redirect::route('user.create')->withErrors($validator);
}else{
return Redirect::route('user.index');
}
}
}
According to your error,Validator expects it parameters to be array, but you are passing a string there as $data= $request->input('name') . So, you should pass array in Validator::make() . Below code should work for you.
$validator = Validator::make($request->all(), [
'name' => 'unique:users,name|required|alpha_num'
]);
Here is the doc if you want to explore more .
You need to pass array inside Validator::make.
Right now your are passing string in the form of $data variable.
For example :
$validator = Validator::make(
array('name' => 'Dayle'),
array('name' => 'required|min:5')
);
DOCS : https://laravel.com/docs/4.2/validation
you have pass your params as array in validation,so your code will be
public function store(Request $request)
{
//
$data = $request->all();
$rules = array(
'name' => 'unique:users,name|required|alpha_num'
);
// Create a new validator instance.
$validator = Validator::make($data, $rules);
if($validator->fails()){
$errors=$validator->messages();
return Redirect::route('user.create')->withErrors($validator);
}else{
return Redirect::route('user.index');
}
}
}

Error when trying to extend validator in Laravel

I have been using the following validation for my form in Laravel:
public function isValid($data, $rules)
{
$validation = Validator::make($data, $rules);
if($validation->passes()){
return true;
}
$this->messages = $validation->messages();
return false;
}
The rules passed to it are simple:
$rules = [
'name' => 'required',
'type' => 'required'
];
And $data is the input post data. Now I need to add a custom validation extension to this, specifically to make sure that the value of input field round2 is greater than the value of input field round1. Looking at the docs, I have tried the following syntax which I think should be correct, but I keep getting an error.
$validation->extend('manual_capture', function($attribute, $value, $parameters)
{
return $value > $parameters[0];
});
Then I could call this with $attribute = 'round1', $value = $data['round1'] and $parameters = [$data['round2']].
The error is Method [extend] does not exist. - I'm not sure if my understanding of this whole concept is correct, so can someone tell me how to make it work? The docs only have about 2 paragraphs about this.
Put the following in your route.php
Validator::extend('manual_capture', function($attribute, $value, $parameters)
{
return $value > $parameters[0];
});
Additional documentation here
Then use it like so:
$rules = [ 'foo' => 'manual_capture:30'];

Laravel 3 Validation of a single input form

I am using spectacular Laravel Framework but i have a validation issue that i cannot solve in any way trying to validate a single email field.
My form input is:
{{ Form::email('email', Input::old('email'), array('id' => 'email', 'class' => 'span4', 'placeholder' => Lang::line('newsletter.email')->get($lang), 'novalidate' => 'novalidate')) }}
My controller
public function post_newsletter() {
$email = Input::get('email');
$v = Newsletter::validator($email, Newsletter::$rules);
if($v !== true)
{ ... }
else
{ ... }
}
and my model
class Newsletter extends Eloquent {
/**
* Newsletter validation rules
*/
public static $rules = array(
'email' => 'required|email|unique:newsletters'
);
/**
* Input validation
*/
public static function validator($attributes, $rules) {
$v = Validator::make($attributes, $rules);
return $v->fails() ? $v : true;
}
}
I ve done this so many times with success with much complicated forms but now even if i enter a valid email i get an error message about required fied etc Am I missing something? Maybe is the fact that i try to validate just one single field? I really don't get it why this happens.
I believe this might be because you're not passing in an array of attributes (or an array of data, basically).
Try using the compact function to generate an array like this.
array('email' => $email);
Here is how you should do it.
$v = Newsletter::validator(compact('email'), Newsletter::$rules);

Categories