laravel 4 full documentation Form::open - php

when I search in Laravel 4 API, ex:
Form::open(array $options = array())
I can't find full list of available options? where to find it?
http://laravel.com/api/4.1/Illuminate/Html/FormBuilder.html#method_open
thanks,

See below link for specific options namely method,action,files,url and route
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#63
Their usage is explained here
http://laravel.com/docs/html#opening-a-form
And you can add any other attributes you use in HTML as options, too.

The options are:
method: POST, GET, PUT, PATCH, DELETE. The last three methods are spoofed with a hidden field.
action: sets action='...' attribute. If there is also a url, route option, they will be translated into the appropriate URL. Otherwise, the action should point to a Controller action route. If not present, the action defaults to current URL.
accept-charset: is forced to UTF-8
files: can be true if file upload is present, appends enctype = 'multipart/form-data' to the form.
toghether with any option you wish to add (like "id", "enctype" or similar).

You should probably check out at laravel official documentation
http://laravel.com/docs/html

Take a look at the source code: https://github.com/laravel/framework/blob/master/src/Illuminate/Html/FormBuilder.php#L95
Some complicated form:
Form::open(['method' => 'put', 'action' => 'awesomeController#putForm', 'id' => 'my-id', 'class' => 'some more css classes', 'files' => 'true', 'data-url' => 'This could be read in JavaScript']);
method, action and files are specific to Laravel, the other values are just attributes and values.

The list of options is the same list of form without use php:
http://www.w3schools.com/tags/tag_form.asp
The difference is that the attributes will be passed in array format:
array('action' => 'user.update', 'enctype' => 'multipart/form-data', 'method' => 'PUT')
The default method is POST, if not especified.
#edit
Seems that i didn't let clear.
Laravel only include some atributes to help, how url(converted to action), route(converted to action), file(converted to enctype='multipart/form-data'), but yet same thing.
file to enctype conversion:
if (isset($options['files']) && $options['files'])
{
$options['enctype'] = 'multipart/form-data';
}

Related

Laminas toRoute as POST?

I work with Laminas and in a ControllerAction function I want to redirect to another url as a post by using $this->redirect()->toRoute('newSite', $noURLParams, $options);
When I use the "query"-Option ($options = [$query => ['postPara1' => 'blabla', 'postPara2' => 'blubblub']]), I will get the "post"-Parameter as GET-Parameter in the URL:
hhttp://localhost/new-site?postPara1=blabla&postPara2=blubblub
So everyone could see the content of the parameters.
Is there a way to get the toRoute() to make a "POST" out of it?
Or do I have to use another function for this?
Thank you for your interest and help. After you said that it doesn't work, I changed the sequence of what, when should be called and now I can do it without an additional redirect.

Check for posted data inside the config file or form_validation.php in CodeIgniter

I want to check for a posted data if present so that I can set a form field as required or not, without having to create a custom rule.
Is it possible to use/load/call CodeIgniter's input library inside the config file (specifically the form_validation.php config file) and get user posted data without having to use the native $_POST variable?
my code goes something like this...
...
array(
'field' => 'dob_day',
'label' => 'Day',
'rules' => (($this->ci->input->post('include_person') === 'yes') ? 'required|' : '') . 'integer|max_length[2]|greater_than[0]|less_than[32]|valid_birth_date'
),
...
Obviously, my approach does not work. hehe. If there's an alternative efficient codeigniter way of doing what i'm thinking, please let me know.
Thanks for all your help! :)
See this:
MANUAL
try this example, in this mode you can at runtime change your rules:
$this->form_validation->set_rules('dob_day', 'Day', 'your new rules');
Okay, I ended up with putting the logic in the controller, and just using inline validation. But to have inline validation work without having to set all the rules, I used the code from this website to allow both inline and config rules to mix.
http://brettic.us/2010/09/13/codeigniter-form-validation-mix-rule/
Cheers!

How to use: sonata_type_model_list

I'm trying to run this code:
if(!$this->isChild()) {
$formMapper->add('post', 'sonata_type_model', array(), array('edit' => 'list'));
From this tutorial: http://sonata-project.org/bundles/doctrine-orm-admin/2-1/doc/tutorial/creating_your_first_admin_class/defining_admin_class.html
I'm aware that you have to use sonata_type_model_list as of 2.1
sonata_type_model_list : this type replaces the option edit = list provided as a 4th argument on the sonata_type_model
The problem is that I have absolutely no idea how to do that. I have found ZERO examples anywhere after a whole day of google searches. All I want to do is replace the edit=>list with sonata_type_model_list.
Can you please tell me how to do that in the code above?
This is how I used it in my code. However it's not working in all browsers. When I select the taget entity the form value in the parent view doesn't get updated (FireFox and IE).
$formMapper->
...
->add('image', 'sonata_type_model_list',
array(
'compound' => true,
'by_reference' => true
)
)
...
I also find it very hard to find some tutorials/examples on how to use this type. Best thing you can do is to go through their source code. Which is awful time consuming.
One way I found out how to configure these form types is to provide a wrong argument.
e.g. 'my_compound' => true,
This will result in an error telling you that 'my_compound' isn't a valid parameter and also will show you a list of valid parameters.
Hope this helps!

In CakePHP, Why using HTML->link instead of manually writing an anchor text?

Isn't it much slower concerning development time ?
What are the advantages of of HTML->link ?
Thanks !
It's just a question of whether you want to generate your own URLs and hard-code them, or if you want Cake to do the work for you. For simple urls leading to the homepage of your site using cake may seem slower, but it's actually useful for dynamic urls, for example:
Say you're printing a table of items and you have a link for each item that deletes that item. You can easily create this using:
<?php
echo $this->Html->link(
'Delete',
array('controller' => 'recipes', 'action' => 'delete', $id),
array(),
"Are you sure you wish to delete this recipe?"
);
Notice how using an array specifying the controller and action as a URL allows you to be agnostic of any custom routes. This can have its advantages.
The corresponding way to do it without the HTML helper would be:
Delete
It can also be really useful for constructing URL query strings automatically. For example, you can do this in array format:
<?php
echo $this->Html->link('View image', array(
'controller' => 'images',
'action' => 'view',
1,
'?' => array('height' => 400, 'width' => 500))
);
That then outputs this line of HTML:
View image
It could be a pain to generate that URL manually.
In summary, while it may seem awkward for simple links, the HTML helper definitely has its uses. For further uses, consult the cakePHP book on the HTML helper's link function.

Do query strings need to be manually sent to POST requests

This is a CakePHP / General PHP question.
In my application I use a query string like /login?continue=/admin/posts
This query string is used to redirect users to the URL in the query, but it doesn't work so it seems as though the app can't see the string...
This has got me wondering as basically when you arrive at the page with the string it's a GET request where as when you login, it becomes a POST or XML request (if using AJAX). Do I need to add the query string manually to the form for the POST to see it?
Either in the form action or a hidden input? Or am I barking up the wrong tree?
I'm currently grabbing the query like so:
if(isset($this->params['url']['continue']))
{
$pathtoredirect = $this->params['url']['continue'];
}
else
{
$pathtoredirect = $this->Auth->redirect();
}
But that's within the POST request so perhaps the query is lost... and adding it to a hidden input would not solve the problem with the current code so I would either change the code to look at the hidden field or pass the query with the action on the form?
e.g. <form action="/login?continue=/admin/posts" method="post">
Am I correct in thinking this? And would anyone be able to offer solutions or pros and cons of the two methods I mention?
In short I'm asking how to add the query string to my form action value
It currently looks like:
php echo $this->Form->create('User',
array(
'id' => 'loginform',
'type' => 'post',
'url' => array
(
'admin'=>false,
'controller' => 'users',
'action' => 'login'
)
)
);
So how would I add the query string to the form?
Thanks
When receiving a POST request you can receive both POST and GET variables through the superglobals $_POST and $_GET.
You can either send your paramater in $_GET by including it in the form's action attribute or send it in $_POST by creating an <input type="hidden"> tag within the form
A slash (/) is a reserved character. Encode it with %2F.
http://blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
The solution was to do this:
<?php echo $this->Form->create('User',
array('id' => 'loginform', 'type' => 'post',
'url' => array('admin'=>false,'controller'=>'users','action'=>'login','url'=>$this->params['url']['continue']))); ?>
as I have a route already setup to handle the additional URL parameter:
Router::connect('/auth/login', array('controller'=>'users','action'=>'login'));
Router::connect('/auth/login?continue=:url',
array('controller'=>'users','action'=>'login'),
array(
'url' => '[A-Za-z0-9/\._-]+',
'pass' => array('url')
));
If anyone sees any issues with the way I do this though, please feel free to comment.

Categories