Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel form validation

use IlluminateSupportFacadesValidator;

// .... 
  
// On your Store function 
 public function store(Request $request, $id)
// Validation 
        $validator = Validator::make($request, [
            'name' => 'required',
            'username' => 'required|unique:users,username,NULL,id,deleted_at,NULL',
            'email' => 'nullable|email|unique:users,email,NULL,id,deleted_at,NULL',
            'password' => 'required',
        ]);


// Return the message
        if($validator->fails()){
            return response()->json([
                'error' => true,
                'message' => $validator->errors()
            ]);
        }
  // ....
}


// On your Update function 
public function update(Request $request, $id)
    {
		// Validation
        $validator = Validator::make($input, [
            'name' => 'required',
            'username' => 'required|unique:users,username,' . $id. ',id,deleted_at,NULL',
            'email' => 'nullable|email|unique:users,email,' . $id. ',id,deleted_at,NULL',
            'roles' => 'required'
        ]);

        // Return the message
        if($validator->fails()){
            return response()->json([
                'error' => true,
                'msg' => $validator->errors()
            ]);
        }
  // ....
}
Comment

validate laravel

$validatedData = $request->validate([
    'title' => ['required', 'unique:posts', 'max:255'],
    'body' => ['required'],
]);
Comment

laravel validation

$this->validate([ // 1st array is field rules
  'userid' =>'required|min:3|max:100',
  'username' =>'required|min:3',
  'password' =>'required|max:15|confirmed',
], [ // 2nd array is the rules custom message
  'required' => 'The :attribute field is mandatory.'
], [ // 3rd array is the fields custom name
  'userid' => 'User ID'
]);
Comment

laravel validation

use IlluminateSupportFacadesValidator;


$customMessage = [
  'title.max' => "title is too large",
];
$rules = [
  'id' => 'integer|exists:master_advert_bundles',
  'title' => ['required', 'unique:posts', 'max:255'],
  'body' => ['required']
];
$validate = validation($request->all(), $rules);
$validate = Validator::make($request->all(), $rules, $customMessage);
if ($validate->fails()) {
  return  $validate->messages();
}
Comment

Laravel Validation

    $validated = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);
Comment

validation laravel

/**
 * Store a new blog post.
 *
 * @param  IlluminateHttpRequest  $request
 * @return IlluminateHttpResponse
 */
public function store(Request $request)
{
    $validated = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);
 
    // The blog post is valid...
}
Comment

validated() laravel

$request->validated();
Comment

Laravel validation

public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);
 
        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }
 
        // Retrieve the validated input...
        $validated = $validator->validated();
 
        // Retrieve a portion of the validated input...
        $validated = $validator->safe()->only(['name', 'email']);
        $validated = $validator->safe()->except(['name', 'email']);
 
        // Store the blog post...
    }
Comment

laravel validation

use IlluminateValidationRule;

Validator::make($data, [
    'email' => [
        'required',
        Rule::unique('users')->ignore($user->id),
    ],
]);
Comment

laravel validation

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Validator::extend(...);

    Validator::replacer('foo', function ($message, $attribute, $rule, $parameters) {
        return str_replace(...);
    });
}
Comment

laravel validation

'foo.*.id' => 'distinct'
Comment

laravel form validation

php artisan make:controller ValidationController --plain
Comment

laravel validation

Validator::extendImplicit('foo', function ($attribute, $value, $parameters, $validator) {
    return $value == 'foo';
});
Comment

laravel validation

Rule::unique('users')->ignore($user->id, 'user_id')
Comment

laravel validation

Validator::extend('foo', 'FooValidator@validate');
Comment

laravel validate string

$email = 'fake@email';

$validator = Validator::make(['email' => $email], [
  'email' => 'email'
]);

if ($validator->fails()) {
  return 'Invalid email';
}
Comment

laravel validation

Rule::unique('users')->ignore($user)
Comment

laravel validation

"foo" => "Your input was invalid!",

"accepted" => "The :attribute must be accepted.",

// The rest of the validation error messages...
Comment

laravel validation

$rules = ['name' => 'unique:users,name'];

$input = ['name' => ''];

Validator::make($input, $rules)->passes(); // true
Comment

PREVIOUS NEXT
Code Example
Php :: laravel change foreign key name 
Php :: how to send data from one website to another in laravel 
Php :: get current time in php 
Php :: foreign key laravel migration 
Php :: do while php 
Php :: check for an existing user laravel eloquent 
Php :: import facade URL laravel 
Php :: how to set cookie expire time in php 
Php :: send attachment in mail php 
Php :: string to boolean php 
Php :: number_format reverse php 
Php :: eloquent pluck multiple columns 
Php :: Add Empty Cart Button WooCommerce 
Php :: laravel log build custom channel 
Php :: migrate specific migration file laravel 
Php :: php get max key in associative array 
Php :: laravel sluggable 
Php :: Wordpress admin settings form 
Php :: validation in laravel 
Php :: php global variable function 
Php :: show time laravel 
Php :: prevent xss php 
Php :: add item to array in php 
Php :: Get current taxonomy term ID of the active page 
Php :: email configuration for gmail in laravel 
Php :: laravel upgrade php version 
Php :: get post index wordpress 
Php :: get post by name wordpress 
Php :: php round to the nearest 10 
Php :: laravel create project 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =