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

Pass variable from controller to $rules #70

Open
jonaholsson opened this issue Aug 10, 2014 · 7 comments
Open

Pass variable from controller to $rules #70

jonaholsson opened this issue Aug 10, 2014 · 7 comments
Labels

Comments

@jonaholsson
Copy link

How would one pass a value from my controller to the model and $rules attribute like below?

protected $rules = [
    ....
    'username' => 'required|unique:users,username,NULL,id,tenant_id,'.$this->tenant_id
];

The following works in the controller, but replaces the entire rules set in the model (and is not as flexible)

$myModel->setRules([
    'username' => required|unique:users,username,NULL,id,tenant_id,'.$this->tenant_id
]);
@nicolaslopezj
Copy link

you can do it like this

public function setTenantIdAttribute($value){
    $this->rules['username'] = 'required|unique:users,username,NULL,id,tenant_id,' . $value;
    $this->attributes['entity_id'] = $value;
}

@dwightwatson
Copy link
Owner

I can see the need for there to be a better solution than this, I'll take a look into it for a future version.

@jonaholsson
Copy link
Author

Nice solution @nicolaslopezj! Didn't think of using setAttribute.. Thanks!

@dwightwatson dwightwatson added this to the 0.11.x milestone Sep 15, 2014
@dwightwatson dwightwatson removed this from the 0.11.x milestone Sep 23, 2014
@jonaholsson jonaholsson removed this from the 0.11.x milestone Sep 23, 2014
@urmila-patel
Copy link

How to display Custom Message for Rule

I have added in Model like

 'location' => 'required|min:5',
    'area' => 'required',
    'price' => 'required|numeric'
];

protected $messages = [
    'location.required' => 'A My Location is required',
    'location.min' => 'Location should be longer. Min 3 characters'
];

But Custom Message is not display

Can Help me??

@dwightwatson
Copy link
Owner

The property is '$validationMessages', I believe.

@urmila-patel
Copy link

Thanks For Reply

I have Tried , but cant get Custom Message

protected $validationMessages = [
'location.required' => 'A My Location is required',
'location.min' => 'Location should be longer. Min 3 characters'
];

Any Idea ??

@dwightwatson
Copy link
Owner

Oh sorry, it looks like that feature was removed from the Laravel 5 branch. Please create a new issue for that, as it is different to the topic of this issue.

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

No branches or pull requests

4 participants