| 1 |
What's new in symfony 1.3? |
|---|
| 2 |
========================== |
|---|
| 3 |
|
|---|
| 4 |
This tutorial is a quick technical introduction for symfony 1.3. |
|---|
| 5 |
It is for developers who have already worked with symfony 1.2 |
|---|
| 6 |
and who want to quickly learn new features of symfony 1.3. |
|---|
| 7 |
|
|---|
| 8 |
First, please note that symfony 1.3 is compatible with PHP 5.2.4 or later. |
|---|
| 9 |
|
|---|
| 10 |
If you want to upgrade from 1.2, please read the |
|---|
| 11 |
[UPGRADE](http://www.symfony-project.org/installation/1_3/upgrade) file |
|---|
| 12 |
found in the symfony distribution. |
|---|
| 13 |
You will have there all the information needed to safely upgrade |
|---|
| 14 |
your projects to symfony 1.3. |
|---|
| 15 |
|
|---|
| 16 |
Security |
|---|
| 17 |
-------- |
|---|
| 18 |
|
|---|
| 19 |
When a new application is created with the `generate:app` task, the security |
|---|
| 20 |
settings are now enabled by default: |
|---|
| 21 |
|
|---|
| 22 |
* `escaping_strategy`: The value is now `on` by default (can be disabled |
|---|
| 23 |
with the `--escaping-strategy` option). |
|---|
| 24 |
|
|---|
| 25 |
* `csrf_secret`: A random password is generated by default, and thus, the |
|---|
| 26 |
CSRF protection is enabled by default (can be disabled with the |
|---|
| 27 |
`--csrf-secret` option). It is highly recommended that you change the |
|---|
| 28 |
default generated password, by editing the `settings.yml` configuration |
|---|
| 29 |
file, or by using the `--csrf-secret` option. |
|---|
| 30 |
|
|---|
| 31 |
Widgets |
|---|
| 32 |
------- |
|---|
| 33 |
|
|---|
| 34 |
### Default Labels |
|---|
| 35 |
|
|---|
| 36 |
When a label is auto-generated from the field name, `_id` suffixes are now |
|---|
| 37 |
removed: |
|---|
| 38 |
|
|---|
| 39 |
* `first_name` => First name (as before) |
|---|
| 40 |
* `author_id` => Author (was "Author id" before) |
|---|
| 41 |
|
|---|
| 42 |
Validators |
|---|
| 43 |
---------- |
|---|
| 44 |
|
|---|
| 45 |
### `sfValidatorRegex` |
|---|
| 46 |
|
|---|
| 47 |
The `sfValidatorRegex` has a new `must_match` option. If set to `false`, the |
|---|
| 48 |
regex must not match for the validator to pass. |
|---|
| 49 |
|
|---|
| 50 |
### `sfValidatorSchemaCompare` |
|---|
| 51 |
|
|---|
| 52 |
The `sfValidatorSchemaCompare` class has two new comparators: |
|---|
| 53 |
|
|---|
| 54 |
* `IDENTICAL`, which is equivalent to `===`; |
|---|
| 55 |
* `NOT_IDENTICAL`, which is equivalent to `!==`; |
|---|
| 56 |
|
|---|
| 57 |
sfToolkit |
|---|
| 58 |
--------- |
|---|
| 59 |
|
|---|
| 60 |
The `getTmpDir()` method has been deprecated and is no longer used in the |
|---|
| 61 |
symfony core classes. You can replace usage of this method by the built-in |
|---|
| 62 |
`sys_get_temp_dir()` PHP function. The `getTmpDir()` is now just a proxy to |
|---|
| 63 |
this method. |
|---|
| 64 |
|
|---|
| 65 |
This method will be removed in the symfony 1.4. |
|---|
| 66 |
|
|---|
| 67 |
Forms |
|---|
| 68 |
----- |
|---|
| 69 |
|
|---|
| 70 |
### `sfForm::useFields()` |
|---|
| 71 |
|
|---|
| 72 |
The new `sfForm::useFields()` method removes all fields from a form except the |
|---|
| 73 |
ones given as an argument. It is sometimes easier to explicitly give the |
|---|
| 74 |
fields you want to keep in a form, instead of unsetting all unneeded fields. |
|---|
| 75 |
For instance, when adding new fields to a base form, they won't automagically |
|---|
| 76 |
appears in your form until explicitly added (think of a model form where you |
|---|
| 77 |
add a new column to the related table). |
|---|
| 78 |
|
|---|
| 79 |
[php] |
|---|
| 80 |
class ArticleForm extends BaseArticleForm |
|---|
| 81 |
{ |
|---|
| 82 |
public function configure() |
|---|
| 83 |
{ |
|---|
| 84 |
$this->useFields(array('title', 'content')); |
|---|
| 85 |
} |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
By default, the array of fields is also used to change the fields order. You |
|---|
| 89 |
can pass `false` as the second argument to `useFields()` to disable the |
|---|
| 90 |
automatic reordering. |
|---|
| 91 |
|
|---|
| 92 |
Validators |
|---|
| 93 |
---------- |
|---|
| 94 |
|
|---|
| 95 |
### Default Error Messages |
|---|
| 96 |
|
|---|
| 97 |
You can now define default error messages globally by using the |
|---|
| 98 |
`sfForm::setDefaultMessage()` method: |
|---|
| 99 |
|
|---|
| 100 |
[php] |
|---|
| 101 |
sfValidatorBase::setDefaultMessage('required', 'This field is required.'); |
|---|
| 102 |
|
|---|
| 103 |
The previous code will override the default 'Required.' message for all |
|---|
| 104 |
validators. Note that the default messages must be defined before any |
|---|
| 105 |
validator is created (the configuration class is a good place). |
|---|
| 106 |
|
|---|
| 107 |
>**NOTE** |
|---|
| 108 |
>The `setRequiredMessage()` and `setInvalidMessage()` methods are |
|---|
| 109 |
>deprecated and call the new `setDefaultMessage()` method. |
|---|
| 110 |
|
|---|
| 111 |
When symfony displays an error, the error message to use is determined as |
|---|
| 112 |
follows: |
|---|
| 113 |
|
|---|
| 114 |
* Symfony looks for a message passed when the validator was created (via the |
|---|
| 115 |
second argument of the validator constructor); |
|---|
| 116 |
|
|---|
| 117 |
* If it is not defined, it looks for a default message defined with |
|---|
| 118 |
the `setDefaultMessage()` method; |
|---|
| 119 |
|
|---|
| 120 |
* If it is not defined, it falls back to the default message defined by the |
|---|
| 121 |
validator itself (when the message has been added with the `addMessage()` |
|---|
| 122 |
method). |
|---|
| 123 |
|
|---|
| 124 |
Autoloaders |
|---|
| 125 |
----------- |
|---|
| 126 |
|
|---|
| 127 |
### `sfAutoloadAgain` (EXPERIMENTAL) |
|---|
| 128 |
|
|---|
| 129 |
A special autoloader has been added that is just for use in debug mode. The |
|---|
| 130 |
new `sfAutoloadAgain` class will reload the standard symfony autoloader and |
|---|
| 131 |
search the filesystem for the class in question. The net effect is that you no |
|---|
| 132 |
longer have to run `symfony cc` after adding a new class to a project. |
|---|
| 133 |
|
|---|
| 134 |
Tests |
|---|
| 135 |
----- |
|---|
| 136 |
|
|---|
| 137 |
### Functional Tests |
|---|
| 138 |
|
|---|
| 139 |
When a request generates an exception, the `debug()` method of the response |
|---|
| 140 |
tester now outputs a readable text representation of the exception, instead of |
|---|
| 141 |
the normal HTML output. It makes debugging much more easier. |
|---|
| 142 |
|
|---|
| 143 |
`sfTesterResponse` has a new `matches()` method that runs a regex on the whole |
|---|
| 144 |
response content. It is of great help on non XML-like responses, where |
|---|
| 145 |
`checkElement()` is not useable. It also replaces the less-powerful |
|---|
| 146 |
`contains()` method: |
|---|
| 147 |
|
|---|
| 148 |
[php] |
|---|
| 149 |
$browser->with('response')->begin()-> |
|---|
| 150 |
matches('/I have \d+ apples/')-> // it takes a regex as an argument |
|---|
| 151 |
matches('!/I have \d+ apples/')-> // a ! at the beginning means that the regex must not match |
|---|
| 152 |
matches('!/I have \d+ apples/i')-> // you can also add regex modifiers |
|---|
| 153 |
end(); |
|---|
| 154 |
|
|---|
| 155 |
Tasks |
|---|
| 156 |
----- |
|---|
| 157 |
|
|---|
| 158 |
### `sfTask::askAndValidate()` |
|---|
| 159 |
|
|---|
| 160 |
There is a new `sfTask::askAndValidate()` method to ask a question to the user |
|---|
| 161 |
and validates its input: |
|---|
| 162 |
|
|---|
| 163 |
[php] |
|---|
| 164 |
$anwser = $this->askAndValidate('What is you email?', new sfValidatorEmail()); |
|---|
| 165 |
|
|---|
| 166 |
The method also accepts an array of options (see the API doc for more |
|---|
| 167 |
information). |
|---|
| 168 |
|
|---|
| 169 |
Exceptions |
|---|
| 170 |
---------- |
|---|
| 171 |
|
|---|
| 172 |
### Autoloading |
|---|
| 173 |
|
|---|
| 174 |
When an exception is thrown during autoloading, symfony now catch them and |
|---|
| 175 |
outputs an error to the user. That should solve most "White screen of death" |
|---|
| 176 |
pages. |
|---|
| 177 |
|
|---|
| 178 |
Propel |
|---|
| 179 |
------ |
|---|
| 180 |
|
|---|
| 181 |
### `propel:insert-sql` |
|---|
| 182 |
|
|---|
| 183 |
Before `propel:insert-sql` removes all data from a database, it asks for a |
|---|
| 184 |
confirmation. As this task can remove data from several databases, it now also |
|---|
| 185 |
displays the name of the connections of the related databases. |
|---|