Accessing sfWebRequest in Symfony Form Class? - php

I Currently writing a form for user to create a new record that require user's IP address.
I have this ready: $this->getObject()->setIp(ip2long($sf_request->getRequest()->getHttpHeader ('addr','remote')));
But it is obvious that I do not have the $sf_request object in my Form model. How can I access to this and get the user's IP address?
Thank you

Try this:
Access to the $request parameter of the Action method and extract the ip from the http headers. Then, in the view file, try to send the variable as parameter to the form file. I don't know exactly how, but i know for example you can pass an object as a parameter to the form to fill it
Good luck

After a moment think about this. I move the setIP method to my action and override the default processForm method with this:
public function processForm(sfWebRequest $request, sfForm $form){
$form->getObject()->setIp(ip2long($request->getHttpHeader ('addr','remote')));
return parent::processForm($request, $form);
}
It works just fine.

Related

Change variable sent to view Laravel

in __construct() in my controller I sent some data to my view by:
public function __construct(){
// code
View::share('messages',$messages);
// more code
}
In other method I want to change this value (action from user change it). I don't want to copy this part of code to every method in controller because code is not clear in my opinion.
Can I use something like:
View()->get('param');
If you have any ideas so please answer :)
You can retrieve a previously shared variable with shared():
view()->shared('param');

Pass a Parameter to a Controller in Laravel

I would like to pass the id of an object to a controller in laravel. Basically I've a page that displays the information relating to a book. The user has the option to add themes to that book. The line below is the link they click in order to open the create theme form:
Add Theme
and the ThemeController method:
public function create(){
$books = Book::all();
return View::make('theme.create', compact('books'));
}
Currently this brings up a Form with a list of all of my books in a dropdown. I would rather just pass the id of the book the user was on prior to being brought to this form and use that instead. Is there any way I can do that? I've also included my routes.php file:
Route::model('book', 'Book');
//Show pages
Route::get('/books', 'BookController#index');
Route::get('book/create', 'BookController#create');
Route::get('book/edit/{book}', 'BookController#edit');
Route::get('book/delete/{book}', 'BookController#delete');
Route::get('book/view/{book}', 'BookController#view');
////Handle form submissions
Route::post('book/create', 'BookController#handleCreate');
Route::post('book/edit', 'BookController#handleEdit');
Route::post('book/delete', 'BookController#handleDelete');
//Themes
Route::model('theme', 'Theme');
Route::get('/themes', 'ThemeController#index');
Route::get('theme/create', 'ThemeController#create');
Route::get('theme/edit/{book}', 'ThemeController#edit');
Route::get('theme/delete/{book}', 'ThemeController#delete');
Route::get('theme/view/{book}', 'ThemeController#view');
Route::post('theme/create', 'ThemeController#handleCreate');
Route::post('theme/edit', 'ThemeController#handleEdit');
Route::post('theme/delete', 'ThemeController#handleDelete');
You can still use normal $_GET methodology.
On your route:
book/create
You could simply request.
book/create?book_id=xxyy
Then in your controller create function
public function create(){
$bookId = Input::get('book_id');
$books = Book::all()->where('book_id' => $bookId);
return View::make('theme.create', compact('books'));
}
PLEASE NOTE:
I haven't used Laravel in a while, don't trust that elequont query :P.. I was just putting a theoretical "where" in there.
And here http://laravel.com/docs/requests#basic-input, you will note this:
"You do not need to worry about the HTTP verb used for the request, as
input is accessed in the same way for all verbs."
So whether you're using get, put, post or delete, whatever you wrapped in your header, you will be able to find using Input Class.
EDIT
Just needed to add, that you won't need to modify your routes at all. This is just a Simple GET parameter in a GET request. Just like in any other application.

Handling contact form submissions Codeigniter

I've inherited a website built using Codeigniter (v2.1.4). The client has asked for a change, and I'm not sure of the best way to achieve it.
I have the following method in the Main controller that powers a new vans page.
public function new_vans($slug = null){
$this->load->view('inc/header_view');
if($slug === NULL){
//If no slug is provided, show all new vans
$this->load->view('new_vans_view');
}else{
//If there is a slug, just show the selected van, or redirect if nothing returned
$data['new_van'] = $this->Database->getSingle('new_vans', array('slug' => $slug));
if(!empty($data['new_van'])){
$this->load->view('new_van_details_view',$data);
}else{
redirect('/new-vans');
}
}
$this->load->view('inc/footer_view');
}
The client has asked for a contact form to be added to a couple of pages including this one, and my question is, should I create a new method that just handles the contact form submissions? If so, how would I handle sending validation errors back to the page? The contact forms will all have the same fields, so I would guess creating a new method is the way to go?
Partial Views(forms)
Partial views are good for forms, they can be re-used
like your client has requested.
Returning views as data
There is a third optional parameter lets you change the behavior
of the function so that it returns data as a
string rather than sending it to your browser.
This can be useful if you want to process the data in some way.
If you set the parameter to true (boolean) it will return data.
The default behavior is false, which sends it to your browser.
Remember to assign it to a variable if you want the data returned:
$string = $this->load->view('myfile', '', true);
Master layouts
To create a Master layout so you can wrap your views
create a new file inside your views directory
views/master/layout.php
<body>
<?php $this->load->view($view); ?>
</body>
Controller
class someController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->template = 'master/layout';
}
public function index()
{
return $this->load->view($this->template, array(
'view' => 'somecontrollerview',
'contact_form' => $this->load->view('partials/forms/contact', array(), true)
));
}
}
somecontrollerview
Echo out the contact form(string)
<?php echo $contact_form; ?>
Contact Controller
Create a new Controller to handle your form validation
The client has asked for a contact form to be added to a couple of pages including this one, and my question is, should I create a new method that just handles the contact form submissions?
create a new controller and new methods
If so, how would I handle sending validation errors back to the page?
look through the codeigniter documentation for form validation. basically if they have an error you are going to show them a view with the form again. it does not matter which page they came "from".
The contact forms will all have the same fields, so I would guess creating a new method is the way to go?
you need to validate the form fields, hopefully capture the contact info to a database, send an email confirmation to the customer, and send an email to the sales person unless its being done directly from the database, and then show a view with a thank you.
each one of those steps is a separate method.
optionally you can show the email address on the thank you page saying 'we have sent you a copy to the email address: something#gmail.com -- that way if the customer messed up the email address they can go back and correct it.

How can I get the form data from the FOSUserBundle registration form?

I am using the FOSUserBundle and have overwritten the RegistrationController. When the form is submitted and valid, I want to get the email address the user entered in the registration form.
But I don't see any way to get it. As taken from the Symfony2 forms documentation, you can get form data like this:
$this->get('request')->request->get('name');
But the RegistrationController does not know the get() method (as it's not inherited from the Symfony2 controller entity). So I could go like this:
// Note the ...->container->...
$this->container->get('request')->request->get('name');
But this returns NULL. Now I try to get it from the $form.
// Does contain a lot of stuff, but not the entered email address
$form->get('email');
// Does also contain a lot of stuff, but not the desired content
$request->get('email');
$request->request('email');
// Throws error message: No method getData()
$request->getData();
Any idea?
It's really, really simple. You create a form with related entity. In FOSUserBundle you should have a RegistrationFormHandler, and in process method you've got:
$user = $this->createUser();
$this->form->setData($user);
if ('POST' === $this->request->getMethod()) {
$this->form->bind($this->request);
if ($this->form->isValid()) /**(...)**/
After the line $this->form->bind($this->request) every value in $user object is overwritten by data from form. So you can use $user->getEmail().
On the other hand you are able to get data directly from request, but not by the property name, but by form name. In FOSUserBundle registration form it is called fos_user_registration - you can find it in FOS/UserBundle/Form/Type/RegistrationFormType.php in getName method.
You get it by:
$registrationArray = $request->get('fos_user_registration');
$email = $registrationArray['email'];
If you were to use Controller as a Service (which should work with this), you could pass the RequestStack (sf >=2.4) in the constructor and do $this->request_stack->getCurrentRequest()->get();
My guess is that you are trying to get the POST data. You are trying to put the data in a form object I presume. I would recommend you to take a look at: http://symfony.com/doc/current/book/forms.html if you have a custom form.
As regarding to your question, the form is probably containing a name. If you want to have direct access to it instead of doing things in your form, you need to fetch it multilevel if directly via the true at $deep, get('registration_form_name[email]', null, true); You can also do $email = $request->get('registration_form_name')['email']; (if you have php 5.4+)

How do I add a field to a Symfony2 form on preBind event?

I need to modify a form on preBind (using Symfony 2.2).
Here is a segment of my preBind function in my EventSubscriber:
public function preBind(FormEvent $event)
{
$form = $event->getForm();
$form->get('locationType')->setData('default');
}
However, when I submit the form, this value is not being saved. Am I missing a step? Do I need to call setData on the FormEvent object itself in order to propagate the new data?
Something that would really help me figure this out is to see the protected function customizeForm($form, $positions) completed in the example at the bottom of this page:
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html
As cheesemacfly suggested, I ended up changing this data within the controller rather than trying to do it within the event subscriber.

Categories