jeudi 27 avril 2017

Parameters added to form request disappear when request is sent via JSON but work from Form

A strange one here which has me stumped.

I have a form request with a custom validator. Upon successful validation I add some items to the request as follows:

public function __construct(ValidationFactory $validationFactory) {

$validationFactory->extend('spatial_reference', function ($attribute, $value, $params, $validator) {
  $spatialReferences = calculateSpatialReferences($value);\\ returns an array with the various spatial references
  if( $spatialReferences == false ){
    return false;
  }
  $this->request->add($spatialReferences); //Works if request comes from html form
  $this->request->remove('spatial_reference');//Works if request comes from html form

  return true;
},
'Spatial reference is not in a valid format.');

}

public function store(SomeRequest $request)
{
    dd($request->all()); \\ contains the spatial references but not if the request was sent via json
}

However here is the strange part. When post is sent from a web form the following lines work fine and request->all() shows the attributes are indeed added to the request when accessed from within the store method in the controller.

$this->request->add($spatialReferences); //Works if request comes from html form
$this->request->remove('spatial_reference');//Works if request comes from html form

But!! if the request came in via JSON then the above does not work but the following does:

request()->request->add($spatialReferences); //Works if request comes from html form
request()->request->remove('spatial_reference');//Works if request comes from html form

And to make matters worse the same is true vice versa. request()->request->add() works for JSON requests but fails if the request comes from a form.

If anyone has any ideas on why this is happening I would appreciate it. Its so strange, they are both CustomRequest classes and both are created but the request->add method is performing differently based on whether the request originated in a form or json.

Any ideas?

Thanks.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2p7kyKF
via IFTTT

Aucun commentaire:

Enregistrer un commentaire