validating
validating copied to clipboard
Pass variable from controller to $rules
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
]);
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;
}
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.
Nice solution @nicolaslopezj! Didn't think of using setAttribute.. Thanks!
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??
The property is '$validationMessages', I believe.
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 ??
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.