crhayes/laravel-extended-validator

Not able to set custom attribute names

Opened this issue · 2 comments

It would be nice to set custom attribute names to the validator.

For now I have a little workaround, I extend your class and override the passes method, in which I've added a call to a function similar to the one to addConditionalRules that receives the Validator instance as parameter.
I named it extraValidatorOptions and in each validaor I override it and call setAttributeNames on it.

Do you think it's a good idea? Should I try and write a patch for it and do a pull request?

@cdarken Hey, sorry for taking so long to respond. Can you give me an example of how you want to use it?

Hi, no problem. I needed to be able to call methods of the standard Validator class, like setAttributeNames.
This is my workaround: I created a class that extends Crhayes\Validation\ContextualValidator the inside that I've overwritten public function passes() like this:

public function passes()
  {
    $rules = $this->bindReplacements($this->getRulesInContext());

    $validator = Validator::make($this->attributes, $rules, $this->messages);

    $this->extraValidatorOptions($validator);

    $this->addConditionalRules($validator);

    if ($validator->passes()) return true;

    $this->errors = $validator->messages();

    return false;
  }

I've added this line: $this->extraValidatorOptions($validator);
By default that does nothing, but in a class that extends my custom class I redefine it like this:

protected function extraValidatorOptions($validator)
  {
    $validator->setAttributeNames(array(
      'field' => 'Field name',
    ));
  }

I hope it's clear now.