{"id":432,"date":"2009-01-22T15:15:40","date_gmt":"2009-01-22T14:15:40","guid":{"rendered":"http:\/\/www.dotmana.com\/index.php\/?p=432"},"modified":"2009-01-22T15:16:31","modified_gmt":"2009-01-22T14:16:31","slug":"mark-required-field-in-a-symfony-form-sfform-with-an-asterisk","status":"publish","type":"post","link":"https:\/\/www.dotmana.com\/weblog\/2009\/01\/mark-required-field-in-a-symfony-form-sfform-with-an-asterisk\/","title":{"rendered":"Mark required field in a Symfony Form (sfForm) with an asterisk"},"content":{"rendered":"<p>How to mark required \/ mandatories fields in a form based on sfForm ? (solution based on <a target=\"_blank\" href=\"http:\/\/dark-it.blogspot.com\/2009\/01\/symfony-mark-fields-as-required.html\">Dark-IT post<\/a>, adapted and tested on Symfony 1.2)<br \/>\nAdd in your lib\/form directory a class name : RegistrationForm.php (you can name it as you want of course)<\/p>\n<blockquote><p>class RegistrationForm extends sfForm<br \/>\n{<br \/>\npublic function configure()<br \/>\n{<br \/>\n\/\/ &#8230;<br \/>\n}<\/p>\n<p>public function render($attributes = array())<br \/>\n{<br \/>\n$formatterObj = $this->widgetSchema->getFormFormatter();<br \/>\nif(!is_null($formatterObj)) {<br \/>\n$formatterObj->setValidatorSchema($this->getValidatorSchema());<br \/>\n}<br \/>\nreturn parent::render($attributes);<br \/>\n}<\/p>\n<p>public function __toString()<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\nreturn $this->renderUsing(&#8216;Registration&#8217;);<br \/>\n}<br \/>\ncatch (Exception $e)<br \/>\n{<br \/>\nself::setToStringException($e);<br \/>\n\/\/ we return a simple Exception message in case the form framework is used out of symfony.<br \/>\nreturn &#8216;Exception: &#8216;.$e->getMessage();<br \/>\n}<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>Add another class in your lib\/form directory : sfWidgetFormSchemaFormatterRegistration.php<\/p>\n<p style=\"margin-left: 40px\">class sfWidgetFormSchemaFormatterRegistration extends sfWidgetFormSchemaFormatter<br \/>\n{<br \/>\nprotected<br \/>\n$rowFormat = &#8220;\\n<\/p>\n<blockquote><p>%label% <span class=\"\\\">%error%<\/span>%field%%help%%hidden_fields%&#8221;,<br \/>\n$requiredTemplate= &#8216;<\/p>\n<p>*&#8217;,<br \/>\n$validatorSchema = null;\/**<br \/>\n* Generates the label name for the given field name.<br \/>\n*<br \/>\n* @param\u00a0 string $name\u00a0 The field name<br \/>\n* @return string The label name<br \/>\n*\/<br \/>\npublic function generateLabelName($name)<br \/>\n{<br \/>\n$label = parent::generateLabelName($name);<\/p>\n<p>$fields = $this->validatorSchema->getFields();<br \/>\nif($fields[$name] != null) {<br \/>\n$field = $fields[$name];<br \/>\nif($field->hasOption(&#8216;required&#8217;) &#038;&#038; $field->getOption(&#8216;required&#8217;)) {<br \/>\n$label .= $this->requiredTemplate;<br \/>\n}<br \/>\n}<\/p>\n<p>return $label;<\/p>\n<p>}<\/p>\n<p>public function setValidatorSchema(sfValidatorSchema $validatorSchema)<br \/>\n{<br \/>\n$this->validatorSchema = $validatorSchema;<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p style=\"margin-left: 40px\">\n<p style=\"margin-left: 0px\">Now, you can use this extended form in your form. Simply change the extends directive with :<\/p>\n<p style=\"margin-left: 40px\">class MyGreatForm extends RegistrationForm<br \/>\n{<br \/>\n\/\/ &#8230;<br \/>\n}<\/p>\n<p style=\"margin-left: 0px\">Of course, you have to update your css, for example :<\/p>\n<blockquote>\n<p style=\"margin-left: 0px\">.requiredFormField<br \/>\n{<br \/>\ncolor: red;<br \/>\n}<\/p><\/blockquote>\n<p style=\"margin-left: 0px\">\n<p style=\"margin-left: 0px\">\n","protected":false},"excerpt":{"rendered":"<p>How to mark required \/ mandatories fields in a form based on sfForm ? (solution based on Dark-IT post, adapted and tested on Symfony 1.2) Add in your lib\/form directory a class name : RegistrationForm.php (you can name it as &hellip; <a href=\"https:\/\/www.dotmana.com\/weblog\/2009\/01\/mark-required-field-in-a-symfony-form-sfform-with-an-asterisk\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-432","post","type-post","status-publish","format-standard","hentry","category-symfony"],"_links":{"self":[{"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts\/432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/comments?post=432"}],"version-history":[{"count":0,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts\/432\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/media?parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/categories?post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/tags?post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}