Reputation: 802
How do I custom validate multiple attributes in a Laravel Backpack CRUD controller update?
Lets say I have a ZIP, CITY, DELIVERY_PICKUP and DELIVERY_SHIPPING.
And lets say my wanted rules are:
Its possible to write custom validation rules with multiple attributes in laravel like this
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Delivery implements Rule
{
public $delivery_shipping;
public $delivery_pick_up;
public $zip;
public $city;
public function __construct($delivery_shipping,$delivery_pick_up,$zip,$city)
{
$this->delivery_shipping = $delivery_shipping;
$this->delivery_pick_up = $delivery_pick_up;
$this->zip = $zip;
$this->city = $city;
}
But how do I fit it in Backpacks FormRequest and fill it with the values?
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class AdvertRequest extends FormRequest
{
public function rules()
{
return [
'title' => 'required|min:5|max:255',
'frontend_user_id' => 'required',
'profile_type' => 'required',
'advertisement_type' => 'required',
'marketplace_category_id' => 'required',
'price' => 'numeric|nullable',
'price_mode' => 'required',
'content' => 'required',
'zip' => 'required|numeric',
'city' => 'required|min:1|max:255',
'delivery_shipping' => 'Delivery',
];
}
Upvotes: 0
Views: 1459
Reputation: 433
I think that you can do it without a custom validation.
required_without
validation:'delivery_shipping' => 'required_without:delivery_pickup',
'delivery_pickup' => 'required_without:delivery_shipping',
required_with
valdiation:'zip' => 'required_with:delivery_shipping',
'city' => 'required_with:delivery_shipping',
All together:
rules = [
'delivery_shipping' => 'required_without:delivery_pickup',
'delivery_pickup' => 'required_without:delivery_shipping',
'zip' => 'required_with:delivery_shipping',
'city' => 'required_with:delivery_shipping',
];
Upvotes: 1