0

What I want to do is pretty simple:

1) A single input box with a submit button 2) I want to take whatever input and redirect the user based on the input, so if my site is www.example.com and the user submits 'test' then I want to redirect to www.example.com/test

However, the symfony2 docs are lacking in examples. I have been able to build my form with this:

class VariantType extends AbstractType
{
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
            $builder
            ->add('urlId', 'text', array('label' => 'tiny.cc/'))
            ->add('Go', 'submit')
            ->getForm();
  }  

  public function getName()
  {
    return 'index';
  }

}

This generates things fine, but I realize I have no submission handler. I assume I need a 'newAction function which takes the request as explained in different parts of the documentation:

http://symfony.com/doc/current/book/forms.html

So I tried this....

 public function newAction(Request $request){

    $form->handleRequest($request);

    if($form->isValid()){
      return $this->redirect($this->generateUrl('homepage_id'));
    }


  }

My 'homepage_id' route handles the route with the 'test' input on the end of the url. I think I can just forward to the url I want and my routing system will handle everything, which shows you how basic my needs are. But the above doesn't seem to work. I also have a validation.yml, but I don't think this is my real hurdle. Even if anyone can point to any docs which explain something like this I would appreciate it. (The symfony2 docs are just not helpful: it tells me creating a formbuilder is recommended, but doesn't say how to submit the form, so I go to the section where it talks about submitting the form, and try and put in the relevant function, but still doesn't work. I can't find any example which puts these pieces together.)

2
  • But the above doesn't seem to work - what errors did you get ? Commented Sep 20, 2013 at 8:56
  • no errors, it just doesn't reroute. Well not to the correct path. It goes to www.example.com/# So it appends the '#' Commented Sep 20, 2013 at 20:27

2 Answers 2

1

You can use the Routing placeholders to handle your redirection based on user input.

Define your homepage_id route :

homepage_id:
path:      /{slug}
defaults:  { _controller: YourBundle:YourController:yourAction }

In your action, redirect to the above route like this:

$url = $this->generateUrl('homepage_id',
        array('slug' => $form->getData()->yourFIeldGetter())
    );
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, the problem isn't the route, it's that I want a FORM that takes input for the url that and redirects to that url. It's the form sumbmission that I'm not sure how to get working.
Ok, this does help some. However, it appears, if I place this code in the newAction codeblock and then try and run my debugger (or with print statements), the submission of the form never even goes into this function newAction. I think I'm missing a step, but the docs are all over the place it's hard to find out how to get this newAction to work.
Ok I think I found out the general procedure. Your suggestion did help. Thanks
0

Using a form template, as above, the handling of the form can be done in your controller. (Usually found under something like Controller/DefaultController.php). This is where your actions would be defined. The confusion is that when using a form template means I created a file in the form namespace under a different filename (Form/MyType.php). If you do this, then you can still deal with your form handling in DefaultController.php

http://symfony.com/doc/current/book/forms.html

Then you can do something like this:

if ($isInvalidUrlId) {
  if ($request->getMethod() == 'POST') {
    $form->bind($request);
    if ($form->isValid()) {
      // get form data
      $data = $form->getData();

      //if urlid exists in the database redirect to that url
      $checkurlqb = $em->createQueryBuilder();
      $isInvalidUrlId = self::checkUrlId($data->getUrlId(), $checkurlqb);
      //if is invalid redirect to general page
      if ($isInvalidUrlId) {
        $url = $this->generateUrl('homepage_id');
      }
      // if is not invalid, re
      else {
        $url = $this->generateUrl('homepage_id', array('id' => $data->getUrlId()));
      }

      return $this->redirect($url);
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.