XF 2.0 setting the action on a form

AndrewSimm

Well-known member
I have a page that looks like this "index.php?players/lorenzo-lingard.832/"

On that page I have a form that updates something on the page. What I am trying to do is submit the data and the redirect back. I don't think I will have a problem doing the redirect in the controller, but I can't seem to figure out the action.

PHP:
<xf:form action="{{ link('players') }}" method="get" class="form">
 
Here is my route to view the page which works
Screen Shot 2017-08-11 at 9.47.46 PM.webp

Here is my route which doesn't work
Screen Shot 2017-08-11 at 9.52.43 PM.webp

Here is my method I want "save" to route too

PHP:
    public function actionSave(ParameterBag $params)
    {
        $player_id = $params->player_id;
        echo "test";
    }
 

Attachments

  • Screen Shot 2017-08-11 at 9.49.52 PM.webp
    Screen Shot 2017-08-11 at 9.49.52 PM.webp
    20.4 KB · Views: 10
Are you sure you need two routes? Can't you use an if-statement in your action?

your form action: {{ link('players/create-player') }}

your controller method:
PHP:
public function actionCreatePlayer(ParameterBag $params)
{
    if ($this->isPost())
        // save entry
        // ..
 
Are you sure you need two routes? Can't you use an if-statement in your action?

your form action: {{ link('players/create-player') }}

your controller method:
PHP:
public function actionCreatePlayer(ParameterBag $params)
{
    if ($this->isPost())
        // save entry
        // ..

To be honest I am not sure

I have a page that list players, which works. I have the page of the player, which works. Within the player page you can submit a prediction form.

Here is what the form does.

PHP:
        //Insert prediction
        $college_id = $_GET['colleges'];
        $confidence = $_GET['confidence'];
        $u = \XF::visitor();
        $user_id = $u->user_id;

        if(isset($college_id) AND isset($confidence) AND isset($user_id) AND isset($player_id)) {
            $prediction_insert = \XF::em()->create('CIS\Backend:Prediction');
            $prediction_insert->player_id = $player_id;
            $prediction_insert->user_id = $user_id;
            $prediction_insert->man_college_id = $college_id;
            $prediction_insert->confidence = $confidence;
            $prediction_insert->save();

        }
 
I would not use $_GET. If necessary you can pass the values as hidden fields.

If you also display the predictions on their own page, you should use a separate controller for Predictions
 
I would not use $_GET. If necessary you can pass the values as hidden fields.

If you also display the predictions on their own page, you should use a separate controller for Predictions

They are displayed on their own page, but also on the player page. The player page houses the last 5 and the predictions page houses predictions for everyone.
 
o.k. so I would use the Predictions Controller to save the prediction and after that redirect back to the Players Controller (actionView method).
 
Route prefix predictions

Route format :int<prediction_id,title>

-> with that format you can access a actionSavePrediction method with a route like /predictions/save-prediction, but you can also have a actionView with a route like /predictions/title-of-the-prediction.1 (where the ID is 1).
 
Route prefix predictions

Route format :int<prediction_id,title>

-> with that format you can access a actionSavePrediction method with a route like /predictions/save-prediction, but you can also have a actionView with a route like /predictions/title-of-the-prediction.1 (where the ID is 1).

Ok, so here is more form

<xf:form action="{{ link('predictions/save-prediction') }}" method="get" class="form">

Here is my method
PHP:
    public function actionSavePrediction(ParameterBag $params)
    {
        $player_id = $params->player_id;

                //Insert prediction
        $college_id = $_GET['colleges'];
        $confidence = $_GET['confidence'];
        $u = \XF::visitor();
        $user_id = $u->user_id;

        if(isset($college_id) AND isset($confidence) AND isset($user_id) AND isset($player_id)) {
            $prediction_insert = \XF::em()->create('CIS\Backend:Prediction');
            $prediction_insert->player_id = $player_id;
            $prediction_insert->user_id = $user_id;
            $prediction_insert->man_college_id = $college_id;
            $prediction_insert->confidence = $confidence;
            $prediction_insert->save();

        }

    }

I get

The requested page could not be found. (Code: no_reply, controller: CIS\Backend:Predictions, action: SavePrediction)
 
try this at the end of your method:
PHP:
return $this->redirect($this->buildLink('players/' . $player_id), 'Prediction saved');
 
Now what's wrong with my $_GETs?
When you submit a form in order to save any entry you should use POST.

GET is good for sorting and filtering.

When you use a POST form you can append all parameters you need as hidden fields. Both, GET and POST parameters, on the same request just don't feel clean (o.k., your route is also a GET Parameter, but let's don't care about that).

In your case: Why don't you load the player entity (make sure it exists!), and then load the data / relationships for confidence and college? So you don't have to pass that data to the save method.
 
Ok, that makes sense with post. I am not sure I follow with the entity. The form is submitted from the players profile, so the entity is load there and that is where I grab player_id. the college_id and confidence are selected on the form.

Here is a screenshot to better show what I am doing. This is on the sidebar of the player profile.

Screen Shot 2017-08-12 at 12.08.23 AM.webp
 
the college_id and confidence are selected on the form.
o.k. sorry, I am not too familiar with what you are doing.

When these things are selected by the user, then you should make a POST request and then use the ParameterBag in order to access these values.
 
Top Bottom