“Validation personnalisée de Laravel” Réponses codées

Validation des e-mails dans Laravel

'email' => 'required|email|unique:users,email',
//@sujay
$uj@y

Si une erreur dans Blade Laravel

 @if ($errors->any())
     @foreach ($errors->all() as $error)
         <div>{{$error}}</div>
     @endforeach
 @endif
Zamir

Validation du formulaire Laravel

use Illuminate\Support\Facades\Validator;

// .... 
  
// 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()
            ]);
        }
  // ....
}
edsonjorgef1

Validation Laravel de règle personnalisée

public function store()
{
    $this->validate(request(), [
        'song' => [function ($attribute, $value, $fail) {
            if ($value <= 10) {
                $fail(':attribute needs more cowbell!');
            }
        }]
    ]);
}
Tiago F2

erreurs de validation de retour de Laravel

@if($errors->any())
   @foreach ($errors->all() as $error)
      <div>{{ $error }}</div>
  @endforeach
@endif
Xfantasia

Validation personnalisée de Laravel

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class GreaterThanTen implements Rule
{
    // Should return true or false depending on whether the attribute value is valid or not.
    public function passes($attribute, $value)
    {
        return $value > 10;
    }

    // This method should return the validation error message that should be used when validation fails
    public function message()
    {
        return 'The :attribute must be greater than 10.';
    }
}
Yawning Yak

Réponses similaires à “Validation personnalisée de Laravel”

Questions similaires à “Validation personnalisée de Laravel”

Plus de réponses similaires à “Validation personnalisée de Laravel” dans PHP

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code