Open chungchi300 opened 8 years ago
private function filterUniqueWhenEdit($rule,$ruleKey,$validation_data,$oldAttributes)
{
$cleans = explode("|", $rule);
foreach ($cleans as $key => $clean) {
if (str_contains($clean, "unique")) {
if($validation_data[$ruleKey] == $oldAttributes[$ruleKey]){
unset($cleans[$key]);
}
}
}
$cleanResult = implode("|", $cleans);
return $cleanResult;
}
/**
* Saves the model
*
* @param \Illuminate\Http\Request $input
* @param array $fields
* @param array $actionPermissions
* @param int $id
*
* @return mixed //string if error, true if success
*/
public function save(\Illuminate\Http\Request $input, array $fields, array $actionPermissions = null, $id = 0)
{
$model = $this->getDataModel()->find($id);
//fetch the proper model so we don't have to deal with any extra attributes
if (!$model) {
$model = $this->getDataModel();
}
//make sure the user has the proper permissions
if ($model->exists) {
if (!$actionPermissions['update']) {
return "You do not have permission to save this item";
}
} else if (!$actionPermissions['update'] || !$actionPermissions['create']) {
return "You do not have permission to create this item";
}
$editModelAttributes = array();
if ($model->exists) {
$editModelAttributes = $model->getAttributes();
}
//fill the model with our input
$this->fillModel($model, $input, $fields);
//validate the model
$data = $model->getAttributes();
$validation_data = array_merge($data, $this->getRelationshipInputs($input, $fields));
$rules = $this->getModelValidationRules();
$rules = $model->exists ? array_intersect_key($rules, $validation_data) : $rules;
$messages = $this->getModelValidationMessages();
if ($model->exists) {
foreach ($rules as $attribute=>&$rule) {
$rule = $this->filterUniqueWhenEdit($rule,$attribute,$validation_data,$editModelAttributes);
}
}
$validation = $this->validateData($validation_data, $rules, $messages);
//if a string was kicked back, it's an error, so return it
if (is_string($validation)) return $validation;
//save the model
$model->save();
//save the relationships
$this->saveRelationships($input, $model, $fields);
//set/update the data model
$this->setDataModel($model);
return true;
}
My hacking on Config.php is passed all data except unique fields to validate function when edit. 1)Does anyone know how to do it without hacking?
The problem is the maxReuse is not existed in validator->getData() function because the admin program haven't pass the maxReuse when I only edit maxReusePerPerson. 1)How can I solve it?