php - Laravel - the same validator works in one controller method but doesn't work in another -
i have 2 simmilar forms, 1 adding news site, edit news:
{{ form::open(array('action' => 'mycontroller@verifyadminaddnews', 'files' => true)) }} {{ form::text('title', form::old('title'), ['required' => 'required']) }}<br><br> {{ form::textarea('subtitle', form::old('subtitle'), ['required' => 'required', 'style' => 'height:60px;']) }} <br><br> {{ form::textarea('text', form::old('text'), ['required' => 'required']) }} <br><br> {{ form::file('image', '') }} @if(isset($errormessage)) <div class="error-message"> {{ $errormessage }} </div> @endif {{ form::submit('pridať novinku', ['class' => 'form-control']) }} {{ form::close() }}
and:
{{ form::open(array('action' => 'mycontroller@verifyadmineditnews', 'class'=>'bg-grey width')) }} {{ form::text('title',$item->title, ['required' => 'required']) }}<br><br> {{ form::textarea('subtitle', $item->subtitle, ['required' => 'required', 'style' => 'height:60px;']) }} {{ form::textarea('text', $item->text, ['required' => 'required']) }} <br><br> {{ form::file('image', '') }} @if(isset($errormessage)) <div class="error-message"> {{ $errormessage }}</div> @endif {{ form::submit('upraviť novinku', ['class' => 'form-control']) }} {{ form::close() }}
and in controller 2 methods adding , editting news:
public function verifyadminaddnews(){ if (is_object(db::table('news')->orderby('id', 'desc')->first())) { $newid = db::table('news')->orderby('id', 'desc')->first()->id + 1; } else { $newid = 0; } // validate if file image $input = array('image' => input::file('image')); $rules = array('image' => 'image'); $validator = validator::make($input, $rules); if ($validator->fails()) { input::flash(); return view::make('adminnadd', ['errormessage' => 'chyba! vybratý súbor nie je obrázok.'] ); } else { if (input::file('image')==null) { db::insert('insert news (id, title, subtitle, text, imageurl) values (?, ?, ?, ?, ?)', array($newid, input::get('title'), input::get('subtitle'), input::get('text'), 'none')); } else { $destination = 'uploadedimages'; $filename = 'image'.$newid; input::file('image')->move($destination, $filename); db::insert('insert news (id, title, subtitle, text, imageurl) values (?, ?, ?, ?, ?)', array($newid, input::get('title'), input::get('subtitle'), input::get('text'), $filename)); } return view::make('adminnall',['items'=>db::table('news')->get()]); } } public function verifyadmineditnews() { $id = session::get('editnewsid'); // validate if file image $input = array('image' => input::file('image')); $rules = array('image' => 'image'); $validator = validator::make($input, $rules); if ($validator->fails()) { input::flash(); return view::make('adminnedit', ['errormessage' => 'chyba! vybratý súbor nie je obrázok.'] ); } else { if (input::file('image')==null) { db::table('news')->where('id', $id)->update(array('title' => input::get('title'), 'subtitle' => input::get('subtitle'), 'text'=>input::get('text'))); } else { $destination = 'uploadedimages'; $filename = 'image'.$id; input::file('image')->move($destination, $filename); db::table('news')->where('id', $id)->update(array('title' => input::get('title'), 'subtitle' => input::get('subtitle'), 'text'=>input::get('text'), 'imageurl' => $filename )); } return view::make('adminnall',['items'=>db::table('news')->get()]); } }
these methods have same validator validating if selected file image. in verifyadminaddnews works right, returning error message view form, when file not image. in verifyadmineditnews validator not fails when file not image , not return error message. how possible?
oh, forgot add 'files' => 'true' second form.
Comments
Post a Comment