Mais conteúdo relacionado Semelhante a The new form framework (20) The new form framework4. symfony 1.x
User
sfForm
Business Logic
Controller
Domain Model
5. symfony 1.x
User
sfForm
Business Logic
Controller
Domain Model unit testable!!
6. symfony 1.x
User
function configure()
{
unset($this['foo']);
sfFormDoctrine / sfFormPropel unset($this['bar']);
unset($this['moo']);
unset($this['comeon!']);
...
Domain Model
11. Symfony 2
User
Presentation
Form
Domain Model Business Logic
designed by you™
13. Business Logic Presentation
class Customer Form
{
TextField
public $name = 'Default';
public getGender(); ChoiceField
public setGender($gender);
}
14. $form = new Form('customer', $customer, ...);
$form->add(new TextField('name'));
$form->add(new ChoiceField('gender', array(
'choices' => array('male', 'female'),
)));
15. $form = new Form('customer', $customer, ...);
...
if (isset($_POST['customer']))
{
$form->bind($_POST['customer']);
}
16. Business Logic Presentation
class Customer Form
{
to-one relations FieldGroup
public $address;
}
17. $group = new FieldGroup('address');
$group->add(...);
$group->add(...);
$group->add(...);
$form->add($group);
18. Business Logic Presentation
class Customer Form
{
CollectionField
to-many relations FieldGroup
public $emails;
FieldGroup
}
19. $group = new FieldGroup('emails');
$group->add(...);
$group->add(...);
$form->add(new CollectionField($group));
20. Default Localized
TextField NumberField TimeField
TextareaField IntegerField DateTimeField
CheckboxField PercentField TimezoneField
ChoiceField MoneyField RepeatedField
PasswordField DateField CollectionField
HiddenField BirthdayField FieldGroup
Special
21. ● Field groups
— light-weight subforms
— compose other fields or field groups
?
DateField
ChoiceField ChoiceField
CheckboxField CheckboxField
24. <label for="<?php echo $form['name']->getId() ?>">
Enter a name
</label>
<?php echo $form['name']->renderErrors() ?>
<?php echo $form['name']->render() ?>
27. symfony 1.x
sfForm
sfValidator
Controller
Domain Model
Doctrine_Validator
28. symfony 1.x
sfForm
sfValidator
Controller Duplicate validation
logic
Domain Model
Doctrine_Validator Failed validation
leads to 505 errors
31. Symfony 2
Form
Validator
Domain Model
Validation Metadata
"how shall the domain model be validated?"
33. <class name="Customer">
<property name="name">
<constraint name="MinLength">6</constraint>
</property>
<property name="birthday">
<constraint name="Date" />
</property>
<property name="gender">
<constraint name="Choice">
<value>male</value>
<value>female</value>
</constraint>
</property>
</class>
34. class Customer
{
/** @Validation({ @MinLength(6) }) */
public $name;
/** @Validation({ @Choice({"male", "female"}) }) */
public function getGender();
/** @Validation({ @Valid }) */
public $gender;
}
35. Type Check String Other
AssertTrue AssertType MinLength Min
AssertFalse Date MaxLength Max
Blank DateTime Regex Choice
NotBlank Time Url Valid
Null Email Collection
NotNull File
36. ● Constraints can be put on
— Classes
— Properties
— Methods with a "get" or "is" prefix
40. $form = new Form('customer', $customer,
$this->container->getService('validator'));
42. ● The validation is launched automatically
upon submission
44. Q: What if my form does not translate
1:1 to a domain model?
46. ● Use Case
— Extend our form for a checkbox to accept terms
and conditions
— Should not be stored as field in the Customer
class
47. class Registration {
/** @Validation({ @Valid }) */
public $customer;
/**
* @Validation({
* @AssertTrue(message="Please accept")
* })
*/
public $termsAccepted = false;
public function process() {
// save $customer, send emails etc.
}
}
50. ● The Registration class
— can be reused (XML requests, …)
— can be unit tested
51. Q: What if I don't want to validate all
attributes of an object?
53. ● Use Case
— Administrators should be able to create
Customers with empty names
— Normal users not
54. class Customer
{
/**
* @Validation({
* @NotBlank(groups="User")
* })
*/
public $name;
...
}
57. ● Constraint groups can be sequenced
— first validate group "Fast"
— then, if "Fast" was valid, validate group "Slow"