php - Symfony 1.4 style form field with errors -
i want change background color of field when error occurs.
in java struts, can this:
<s:textfield name="parameter" cssclass="normal_css_class" csserrorclass="class_on_error" csserrorstyle="style_on error"/>
i want able perform above. tag renders field csserrorclass when field parameter has errors. no more additional javascript required.
currently have following (very dirty) code in template:
<?php if($form['bill_to']->haserror()): ?> <?php echo $form['bill_to']->render(array('style' => 'background-color: red')) ?> <?php else: ?> <?php echo $form['bill_to']->render() ?> <?php endif; ?> <?php echo $form['bill_to']->rendererror() ?>
the above code works there way implement have call:
<?php echo $form['bill_to']->render() ?>
and perform setting of styles? i'm thinking of overriding render() method not sure if right approach.
you can extend sfwidgetformschemaformatter class this:
class sfwidgetformschemaformattercustom extends sfwidgetformschemaformatter { protected $rowformat = "<div class=\"%row_class%\">%label% %field% %hidden_fields% %help%</div>", $helpformat = "%help%", $errorrowformat = "", $errorlistformatinarow = "\n%errors%\n", $errorrowformatinarow = "<span class=\"error\">%error%</span>\n", $namederrorrowformatinarow = "%error%\n", $decoratorformat = "%content%"; public function formatrow($label, $field, $errors = array(), $help = '', $hiddenfields = null) { $row = parent::formatrow( $label, $field, $errors, $help, $hiddenfields ); return strtr($row, array( '%row_class%' => (count($errors) > 0) ? ' error' : '', )); } }// decorator class
and apply form inside it's configure() method this:
class myform extends sfform { public function configure() { // .... $formatter = new sfwidgetformschemaformattercustom($this->widgetschema); $this->widgetschema->addformformatter('custom', $formatter); $this->widgetschema->setformformattername('custom'); } }
Comments
Post a Comment