Du hast ein Registrierungsformular mit einem Feld account_type und zwei eingebetteten Formularen Company und Address.
Du willst das eingebettete Company Formular entfernen, wenn das Feld account_type sfGuardUserProfile::TYPE_PRIVATE entspricht, andernfalls willst du das eingebettete Address Formular entfernen.
Die Lösung ist die bind Methode in deinem Formular zu überschreiben.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| public function bind(array $taintedValues = null, array $taintedFiles = null)
{
// unset request param and validator
if($taintedValues['account_type'] == sfGuardUserProfile::TYPE_PRIVATE)
{
unset(
$taintedValues['Company'],
$this->validatorSchema['Company']
);
}
else
{
unset(
$taintedValues['Address'],
$this->validatorSchema['Address']
);
}
parent::bind($taintedValues, $taintedFiles);
} |
Erstelle die Datei lib/gfFormHerlp.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| <?php
/**
* Some usefull form helper
*
* @package symfony
* @subpackage helper
* @author Gordon Franke <gfranke@nevalon.de>
* @link http://www.nevalon.de
*/
class gfFormHelper
{
/**
* Add * to required field labels
*
* @param sfForm $form
* @param string $symbol
* @param string $title
*
* @return void
*/
public static function addRequiredToLabel(sfForm $form, $symbol = '*', $title = 'This field is mandatory.')
{
$widgetSchema = $form->getWidgetSchema();
$validatorSchema = $form->getValidatorSchema();
foreach($form->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
{
$label = $form->getFormFieldSchema()->offsetGet($key)->renderLabelName();
if(isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true) {
$label .= '<sup title="' . $widgetSchema->getFormFormatter()->translate($title) . '">' . $symbol . '</sup>';
}
$widgetSchema->setLabel($key, $label);
}
}
} |
Füge folgendes am ende deiner Formular in der setup oder besser wenn du Unterformulare hast in der configure Methode ein
1
| gfFormHelper::addRequiredToLabel($this); |
Benutze dies in deinem Testcode um das User Objekt zu erstellen
1
2
3
4
5
6
7
8
| $_SERVER['session_id'] = 'test';
$dispatcher = new sfEventDispatcher();
$sessionPath = sfToolkit::getTmpDir().'/sessions_'.rand(11111, 99999);
$storage = new sfSessionTestStorage(array('session_path' => $sessionPath));
$myUser = new myUser($dispatcher, $storage);
$myUser->setAttribute('user_id', 1, 'sfGuardSecurityUser'); |
Erster Schritt zend framework implementieren
[cref 30 Wie kann ich Zend Framework Komponenten in meinem Symfony Projekt nutzen?]
Zweiter Schritt feed Modul erstellen und template Ordner löschen
./symfony generate:module frontend feed
rm apps/frontend/modules/feed/templates/ -Rf
Dritter Schritt index Funktion ersetzen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| public function executeIndex(sfWebRequest $request)
{
ProjectConfiguration::registerZend();
$feedArray = array(
'title' => ...,
'link' => $this->generateUrl('default_index', array('module' => 'feed')),
'description' => ..,
'language' => ...,
'charset' => sfConfig::get('sf_charset'),
'pubDate' => time(),
'entries' => array()
);
/* get the data from the db or outher source */
foreach($articles as $article)
{
$feedArray['entries'][] = array(
'title' => $article['title'],
'link' => $this->generateUrl('article_detail', $article),
'guid' => $article['id'],
'description' => $article['description']
);
}
$feed = Zend_Feed::importArray($feedArray, 'rss');
$feed->send();
throw new sfStopException();
} |
das wars für weitere Information siehe
Zend_Feed
UPDATE: Ich habe die Klasse aktualisiert und einen neuen Parameter hinzugefügt include_level
Aktiviere das logging für die prod Umgebung in der settings.yml
prod:
.settings:
logging_enabled: on
füge dies zu deiner factories.yml hinzu
prod:
logger:
param:
level: debug
loggers:
sf_web_debug:
class: sfNoLogger
sf_file_debug:
param:
level: warning
sc_mail:
class: gfMailLogger
param:
level: warning
include_level: debug
emails: ["test@test.de"]
erstelle gfMailLogger.class.php in deinem lib Ordner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
| <?php
/**
* gfMailLogger logs messages and send it via email.
*
* @package symfony
* @subpackage log
* @author Gordon Franke <gfranke@nevalon.de>
* @link http://www.nevalon.de
*/
class gfMailLogger extends sfLogger
{
protected
$emails = array(),
$subject,
$body,
$include_level,
$log = false,
$type = 'symfony',
$format = '%time% %type% [%priority%] %message%%EOL%',
$timeFormat = '%b %d %H:%M:%S';
/**
* Initializes this logger.
*
* Available options:
*
* - emails: The emails to be send the log messages
* - subject: The subject for the email
* - include_level: Use this to get more detailed information
* - format: The log line format (default to %time% %type% [%priority%] %message%%EOL%)
* - time_format: The log time strftime format (default to %b %d %H:%M:%S)
*
* @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
* @param array $options An array of options.
*
* @return Boolean true, if initialization completes successfully, otherwise false.
*/
public function initialize(sfEventDispatcher $dispatcher, $options = array())
{
if (!isset($options['emails']))
{
throw new sfConfigurationException('You must provide a "emails" parameter for this logger.');
}
$this->emails = $options['emails'];
if (isset($options['format']))
{
$this->format = $options['format'];
}
if (isset($options['time_format']))
{
$this->timeFormat = $options['time_format'];
}
if (isset($options['type']))
{
$this->type = $options['type'];
}
if (isset($options['subject']))
{
$this->subject = $options['subject'];
}
else
{
$this->subject = sfConfig::get('sf_app') . ':: Log';
}
parent::initialize($dispatcher, $options);
if (isset($options['include_level']))
{
if (!is_int($options['include_level']))
{
$options['include_level'] = constant('sfLogger::'.strtoupper($options['include_level']));
}
$this->include_level = $options['include_level'];
}
else
{
$this->include_level = $this->getLogLevel();
}
}
/**
* Logs a message.
*
* @param string $message Message
* @param string $priority Message priority
*/
public function log($message, $priority = self::INFO)
{
if ($this->include_level < $priority)
{
return false;
}
if($this->getLogLevel() >= $priority)
{
$this->log = true;
}
return $this->doLog($message, $priority);
}
/**
* Logs a message.
*
* @param string $message Message
* @param string $priority Message priority
*/
protected function doLog($message, $priority)
{
$this->body .= strtr($this->format, array(
'%type%' => $this->type,
'%message%' => $message,
'%time%' => strftime($this->timeFormat),
'%priority%' => $this->getPriority($priority),
'%EOL%' => PHP_EOL,
));
}
/**
* Returns the priority string to use in log messages.
*
* @param string $priority The priority constant
*
* @return string The priority to use in log messages
*/
protected function getPriority($priority)
{
return sfLogger::getPriorityName($priority);
}
/**
* Executes the shutdown method.
*/
public function shutdown()
{
if($this->log)
{
foreach($this->emails as $email)
{
mail($email, $this->subject, $this->body);
}
}
}
} |
Ich möchte die user id des aktuell eingelogten zum formular hinzufügen. Ich könnte ein hidden feld benutzen, aber das ist nicht sicher. Also rufe ich die updateObject($values) methode zwischen isValid() und save() auf.
1
2
3
4
5
6
7
8
9
10
11
| protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()));
if ($form->isValid())
{
$form->updateObject(array('user_id' => $this->getUser()->getAttribute('user_id', null, 'sfGuardSecurityUser')));
$article = $form->save();
$this->redirect($this->generateUrl('article_detail', $article));
}
} |
Letzte Kommentare