|
April 25, 2011 – 12:05 pm |
Medhad |
|
|
|
|
There are several frameworks out there in the world of php. Symfony is one of them. It is able to stand out of the crowd due to its superior features, built in security, and great community. Symfony incorporated best practices such as ‘convention over configuration’ and ‘evade code duplication’ which helps writing and organizing code more easily, boosting productivity. That’s one is the reasons why we love to work with Symfony here at Blueliner.
One of Symfony’s most appreciated features is its form library. Today I will write a short tutorial on how to create and validate a ‘choice form’ using its form library.
At first let’s see the screenshots of what we are going to implement:

Figure 1: Form Fields before Submission

Figure 2: Form Fields w/ blank Submission

Figure 3: Filling out Required Fields

Figure 4: After Successful Submission
Now let’s create a form inside ‘project_root/lib/form/’ directory called ‘myChoiceForm.php’ and write this code inside.
<?php
class myChoiceForm extends BaseForm {
public function configure() {
//key value pair for the choice form
$choices = array(‘Soccer’ => ‘Football’, ‘Cricket’ => ‘Cricket’, ‘BasketBall’ => ‘Basketball’, ‘Tennis’ => ‘Tennis’, ‘Chess’ => ‘Chess’);
//setting up the widget
$this->setWidgets(array(
‘my_name’ => new sfWidgetFormInputText(),
‘my_sport’ => new sfWidgetFormChoice(array(
‘expanded’ => true,
‘multiple’ => true,
‘choices’ => $choices,
)),
));
//setting up the validator
$this->setValidators(array(
‘my_name’ => new sfValidatorString(array(‘required’ => true)),
‘my_sport’ => new sfValidatorChoice(array(‘choices’ => array_keys($choices), ‘multiple’ => true))
));
//this is to allow any extra form fields while validation
$this->validatorSchema->setOption(‘allow_extra_fields’, true);
//this is to define what name format the form will generate in the view
$this->widgetSchema->setNameFormat(‘myChoiceForm[%s]‘);
}}
?>
Note that inside ‘$this->setValidators()’ I am defining the validation rules. Here ‘sfValidatorString(array(‘required’ => true))’ means this form field is required to be filled with some value. Similarly note the ‘new sfValidatorChoice(array(‘choices’ => array_keys($choices), ‘multiple’ => true))’ part, here by providing “’choices’ => array_keys($choices)” parameter I’m telling the validation class to validate against these particular keys from the choice form. Also the “’multiple’ => true” is important! If you do not provide this parameter then validation class will assume that user can only select 1 value and will show an error if user selects multiple entries.
Now write a method to show this form in action and pass it to the view:
‘project_root/apps/app_name/modules/module_name/actions/actions.class.php’:
<?php
public function executeShowMyForm(sfWebRequest $request) {
$form = new myChoiceForm();
if ($request->getPostParameter(‘myChoiceForm’) != NULL) {
$form->bind($request->getPostParameter(‘myChoiceForm’));
if ($form->isValid()) {
//setting up a different template
$this->setTemplate(‘showMessage’);
//passing the form to the view
$this->form = $form;
} else {
//passing the form to the view
$this->form = $form;
}
} else {
$this->form = $form;
}
}
?>
Now render this form inside the corresponding template:
‘project_root/apps/your_app/modules/module_name/templates/showMyFormSuccess.php’:
<form id=”form5″ method=”post” action=”<?php echo url_for(‘test/showMyForm’) ?>” >
<?php// echo $form; ?>
lt;span style=”color: #EE0000″><?php echo $form->renderGlobalErrors() ?></span>
<?php echo $form->renderHiddenFields() ?>
<div ><span class=”lebel”>Name :</span></div>
<div >
<span style=”color: #EE0000″> <?php echo $form["my_name"]->renderError() ?> </span>
<?php echo $form["my_name"]->render(array(“size” => “40″)) ?>
</div>
<br />
<div ><span class=”lebel”>My Sports :</span></div>
<div>
<span style=”color: #EE0000″> <?php echo $form["my_sport"]->renderError() ?> </span>
<?php echo $form["my_sport"]->render(array(“size” => “40″)) ?>
</div>
<input type=”submit” value=”Submit” />
</form>
Also create a template to show your submitted message:
‘project_root/apps/your_app/modules/module_name/templates/showMessageSuccess.php’:
<?php
echo “You have submitted the form successfully !<br />”;
echo ‘Your name is:’ . $form->getValue(‘my_name’).’<br />’;
echo ‘Your favourite sports are: ‘ . implode(‘,’,$form->getValue(‘my_sport’));
?>
Now you have seen how to implement a form in Symfony, try it yourself!
Posted by
Medhad
Posted in
blog, web development |
No Comments »
Tags: 7 pillars, Blueliner, coding, creating forms, digital marketing tips, html, internet marketing agency nyc, symfony