php - Laravel 5 how to run mutator before validation -


i green @ laravel (first project) bear me if i'm making novice mistakes. i'm trying create project while running through laracasts, i'm using suggestions. i'm using laravel 5.4 , php 7.1.4

i have checkbox on form boolean field. if checkbox not checked when form submitted returns null. not want null values boolean's have validator ensuring accepts true/false values. in order work had create mutator change value false if null.

i have 2 models, item , itemnote. i'm trying create itemnote item model. on item page there place add itemnote runs through itemnotecontroller, call method in item add itemnote. issue can't mutator run in itemnote model validation fails because boolean field (calendar_item) null.

i @ first trying create itemnote relationship item, according stack overflow laravel 5 mutators work when create record , not when update record answer mutator not run when creating via relationship $this->notes()->create($request->all()). have use model $this->notes->create($request->all()) notice absence of parenthesis after notes. i've tried can possibly think of try create object via model , still can't mutator run.

here relationship declarations in models:

item

public function notes() { return $this->hasmany(itemnote::class); } 

itemnote

public function item() { return $this->belongsto(item::class); } 

mutator in itemnote calendar_item

protected function setcalendaritemattribute($value) { $this->attributes['calendar_item'] = isset($value) ? $value : false; } 

the validation rules in itemnote

public static $validationrules = array('note_date' => 'required|date',                                            'resolve_date' => 'nullable|date',                                            'notes' => 'required|string',                                            'cost' => 'nullable|numeric',                                            'calendar_item' => 'required|boolean',                                            'attachment_path' => 'nullable|string|max:200'); 

this action in itemnotecontroller runs when adding itemnote item page

public function store(item $item) {     $this->validate(request(), itemnote::$validationrules);      $item->addnote(new itemnote(request(['item_note_category_id', 'note_date', 'resolve_date',                                          'notes', 'cost', 'calendar_item', 'attachment_path'])));      return back(); } 

here function addnote in item model

public function addnote(itemnote $note) {     $this->item_note->save($note); } 

here different things have tried in addnote, fail run mutator. create statements have field assignments listed out removed them here brevity.

$this->notes->save($note); $this->notes()->save($note); $this->item_note->save($note); $this->notes->create $this->item_notes->create $this->item_notes()->create $this->item_note->create $this->item_note()->create $this->itemnote->create $this->itemnote()->create itemnote::create 

all of above work, although think $this->item_notes->create shouldn't work @ because relationship name notes doesn't complain makes me think may not getting code , it's failing on validate statement in controller. how mutators run before validation? or there better way clean data before validation?

i tried putting item_id field in validation rules fails because item_id not assigned until create object via relationship. i'd require haven't figured out how assigned in request.

any appreciated. sorry long post.

your mutators on model. whereas you're using validatesrequests controller trait validate request input data. mutators being invoked after you've run validation.

i therefore see have 2 options.

a. modify html ensure receive boolean value. example, use hidden input default value. value submitted, if checkbox isn't checked.

<input name="example" type="hidden" value="0"> <input name="example" type="checkbox" value="1"> 

b. hydrate model, invoke mutators, run validation.

$itemnote = new itemnote($request->all()); $request->merge($itemnote->toarray());  $this->validate($request, itemnote::$validationrules); // ... 

edit: here links model validation. might useful , give own ideas.

https://gist.github.com/robsimpkins/2fe0de2483d51f1e4446ec90be7d96ae https://gist.github.com/robsimpkins/6a2f20853a2d01260ab299ebcebf9673 https://gist.github.com/robsimpkins/7df10abce1cdc07593d8c872a5461425 https://gist.github.com/robsimpkins/c4aa0517a80b794a2d347912100bd6d9


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -