• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 setting the action on a form

AndrewSimm

Active member
#1
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">
 
#5
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
        // ..
 

AndrewSimm

Active member
#6
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();

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

AndrewSimm

Active member
#8
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.
 
#11
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).
 
#12
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)
 
#14
#18
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.
 
#19
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.png