Answer the question
In order to leave comments, you need to log in
Complex validation in Laravel?
There is a Product model with three methods add($data), edit($id, $data) and validate($data).
Validation occurs at the very beginning of the add and edit methods:
public static function add($data)
{
Product::validate($data);
...
}
public static function add($id, $data)
{
Product::validate($data);
...
}
$rules = array(
...
'url' => 'required|min:5|max:512|unique:products',
...
);
Answer the question
In order to leave comments, you need to log in
$rules = array(
'url' => 'required|unique:products,url,{{$id}}',
);
public function validate($data, $id = null)
{
$rules = $this->rules;
foreach($rules as $keys => $value) {
$validations = explode('|', $value);
foreach($validations as $key => $value) {
$validations[$key] = str_replace('{{$id}}', $id, $value);
}
$implode = implode("|", $validations);
$rules[$keys] = $implode;
}
return \Validator::make($data, $rules);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question