Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For validation and custom validator #1021

Open
chungchi300 opened this issue Apr 26, 2016 · 1 comment
Open

For validation and custom validator #1021

chungchi300 opened this issue Apr 26, 2016 · 1 comment

Comments

@chungchi300
Copy link

<?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
Copy link
Author


    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant