validating icon indicating copy to clipboard operation
validating copied to clipboard

Pass variable from controller to $rules

Open jonaholsson opened this issue 10 years ago • 7 comments

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
]);

jonaholsson avatar Aug 10 '14 21:08 jonaholsson

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;
}

nicolaslopezj avatar Aug 10 '14 22:08 nicolaslopezj

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.

dwightwatson avatar Aug 10 '14 23:08 dwightwatson

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

jonaholsson avatar Aug 11 '14 15:08 jonaholsson

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??

urmila-patel avatar Mar 12 '15 12:03 urmila-patel

The property is '$validationMessages', I believe.

dwightwatson avatar Mar 12 '15 12:03 dwightwatson

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 ??

urmila-patel avatar Mar 12 '15 12:03 urmila-patel

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.

dwightwatson avatar Mar 12 '15 23:03 dwightwatson