| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
* Locates and view's forms and adds client validation and pre fills the form |
|---|
| 5 |
* |
|---|
| 6 |
* @package default |
|---|
| 7 |
* @author Joe Simms |
|---|
| 8 |
**/ |
|---|
| 9 |
class sfFormFilter extends sfFilter |
|---|
| 10 |
{ |
|---|
| 11 |
|
|---|
| 12 |
* Execute this filter. |
|---|
| 13 |
* |
|---|
| 14 |
* @param FilterChain A FilterChain instance. |
|---|
| 15 |
* |
|---|
| 16 |
* @return void |
|---|
| 17 |
*/ |
|---|
| 18 |
public function execute ($filterChain) |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
$filterChain->execute(); |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
* Execute this filter. |
|---|
| 26 |
* |
|---|
| 27 |
* @param FilterChain A FilterChain instance. |
|---|
| 28 |
* |
|---|
| 29 |
* @return void |
|---|
| 30 |
*/ |
|---|
| 31 |
public function executeBeforeRendering ($filterChain) |
|---|
| 32 |
{ |
|---|
| 33 |
foreach($this->getForms() as $form) |
|---|
| 34 |
{ |
|---|
| 35 |
|
|---|
| 36 |
if(!$form_name = $this->getAttribute('name')) continue; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
$this->dest[$form_name] = $this->resolveFormsDestination($form->getAttribute('action')); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
if(file_exists(sfConfig::get('sf_mod_dir').'/'.$dest['module'].'/validate/'.$dest['action'].'.yml')) |
|---|
| 43 |
{ |
|---|
| 44 |
|
|---|
| 45 |
// include an array of params |
|---|
| 46 |
// $this->validate[$form_name] = parsed validate.yml file |
|---|
| 47 |
$this->preFillForm($form, ); |
|---|
| 48 |
$this->addFormValidation($form) |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
$filterChain->execute(); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
* Returns an array of forms in the view |
|---|
| 57 |
* |
|---|
| 58 |
* @return void |
|---|
| 59 |
* @author Joe Simms |
|---|
| 60 |
**/ |
|---|
| 61 |
private function getForms() |
|---|
| 62 |
{ |
|---|
| 63 |
return array(); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Pre fill the form with stored values (from request, flash or user's attributes) |
|---|
| 68 |
* |
|---|
| 69 |
* @return void |
|---|
| 70 |
* @author Joe Simms |
|---|
| 71 |
**/ |
|---|
| 72 |
private function preFillForm($form) |
|---|
| 73 |
{ |
|---|
| 74 |
foreach($form as $element) |
|---|
| 75 |
{ |
|---|
| 76 |
if($this->canFillFormElement($element)) |
|---|
| 77 |
{ |
|---|
| 78 |
if($value = $this->Value($form->getAttribute('name'), $element->getAttribute('name'))) |
|---|
| 79 |
{ |
|---|
| 80 |
|
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
* Determines if pre fill can fill this form element based upon loaded params |
|---|
| 88 |
* |
|---|
| 89 |
* @return boolean |
|---|
| 90 |
* @author Joe Simms |
|---|
| 91 |
**/ |
|---|
| 92 |
private function canFillFormElement() |
|---|
| 93 |
{ |
|---|
| 94 |
|
|---|
| 95 |
// checks ignore list etc |
|---|
| 96 |
return true; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
* Adds attributes to form elements providing validation hook for a javascript validation class to interpret |
|---|
| 101 |
* |
|---|
| 102 |
* @return void |
|---|
| 103 |
* @author Joe Simms |
|---|
| 104 |
**/ |
|---|
| 105 |
private function addFormValidation($form) |
|---|
| 106 |
{ |
|---|
| 107 |
foreach($form as $element) |
|---|
| 108 |
{ |
|---|
| 109 |
if($this->canValidateFormElement($element)) |
|---|
| 110 |
{ |
|---|
| 111 |
|
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
* Determines if form element can be validated based upon loaded params |
|---|
| 118 |
* |
|---|
| 119 |
* @return boolean |
|---|
| 120 |
* @author Joe Simms |
|---|
| 121 |
**/ |
|---|
| 122 |
private function canValidateFormElement($element) |
|---|
| 123 |
{ |
|---|
| 124 |
|
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
* Returns the default value used to pre fill the form |
|---|
| 129 |
* |
|---|
| 130 |
* @return void |
|---|
| 131 |
* @author Joe Simms |
|---|
| 132 |
**/ |
|---|
| 133 |
private function getValue($form_name, $elem_name) |
|---|
| 134 |
{ |
|---|
| 135 |
$value = null; |
|---|
| 136 |
switch($lifetime) |
|---|
| 137 |
{ |
|---|
| 138 |
case 'session': |
|---|
| 139 |
$value = sfContext->getUser()->getAttribute($name, '', "form/$form_name"); |
|---|
| 140 |
case 'flash': |
|---|
| 141 |
$value = sfContext->getUser()->getAttribute($name, '', "symfony/flash"); |
|---|
| 142 |
default: |
|---|
| 143 |
$value = sfContext->getRequest()-getParameter($name); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
return $value; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
private function espaceRequestParameter($request, $name) |
|---|
| 150 |
{ |
|---|
| 151 |
$value = $request->getParameter($name); |
|---|
| 152 |
if (isset($this->escapers[$name])) |
|---|
| 153 |
{ |
|---|
| 154 |
foreach ($this->escapers[$name] as $function) |
|---|
| 155 |
{ |
|---|
| 156 |
$value = $function($value); |
|---|
| 157 |
} |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
return $value; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
} |
|---|