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

Not able to set custom attribute names #11

Open
cdarken opened this issue Aug 1, 2014 · 2 comments
Open

Not able to set custom attribute names #11

cdarken opened this issue Aug 1, 2014 · 2 comments

Comments

@cdarken
Copy link

cdarken commented Aug 1, 2014

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?

@crhayes
Copy link
Owner

crhayes commented Aug 28, 2014

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

@cdarken
Copy link
Author

cdarken commented Aug 29, 2014

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.

melihovv added a commit to melihovv/laravel-extended-validator that referenced this issue Aug 28, 2016
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

Successfully merging a pull request may close this issue.

2 participants