FrozenNode / Laravel-Administrator

An administrative interface package for Laravel
http://administrator.frozennode.com/
MIT License
1.94k stars 501 forks source link

For validation and custom validator #1021

Open chungchi300 opened 8 years ago

chungchi300 commented 8 years ago
<?php

class PromotionCode extends Eloquent
{
    protected $table = 'promotion_code';
    public $timestamps = false;
    public  static $rules = array
    (

        'price' => 'required',
        'startAt' => 'required|lower_than:endAt',
        'endAt' => 'required',
        'maxReuse' => 'required',
        'maxReusePerPerson' => 'required|lower_than:maxReuse',
        'code' => 'required|unique:promotion_code',
        'name' => 'required'

    );

}

//in my custom validator.php

    $compareFieldName = $parameters[0];
    if(array_key_exists($compareFieldName,$validator->getData())){
        $compareValue = $validator->getData()[$compareFieldName];
        if($value  < $compareValue){
            return true;
        }else{
            return false;
        }
    }else{

        return false;
    }

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?

chungchi300 commented 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?