I am using symfony 1.4.
I have a form like this:
validator:
$this->setValidator('name', new sfValidatorString(array('required' => true)));
view:
<?php echo $form['name']->renderError() ?>
How do i change the default "required" error message to, for example, "This field is required" ?
Edit: Also, how do i get rid of <ul>
and <li>
tags generated by the renderError()
. method ? I just want the text to be displayed, no additional markup.
Note: This is an obvious question, but since I took a long time to find out, i think the question diserves to be asked here.
1.Change the required message:
new sfValidatorString(
array(
'required' => true,
),
array(
'required'=>'You custom required message'
));
2.Create a new formatter class.
3.Redefine the attribute you want.
<?php
class myWidgetFormSchemaFormatter extends sfWidgetFormSchemaFormatter
{
protected
$rowFormat = '',
$helpFormat = '%help%',
$errorRowFormat = '%errors%',
$errorListFormatInARow = " <ul class=\"error_list\">
%errors% </ul>
",
$errorRowFormatInARow = " <li>%error%</li>
",
$namedErrorRowFormatInARow = " <li>%name%: %error%</li>
",
$decoratorFormat = '',
$widgetSchema = null,
$translationCatalogue = null;
4.In the configure method of your symfony form add
$oDecorator = new myWidgetFormSchemaFormatter($this->getWidgetSchema());
$this->getWidgetSchema()->addFormFormatter('myCustom', $oDecorator);
$this->getWidgetSchema()->setFormFormatterName('myCustom');
new sfValidatorString(array('required' => true), array('required'=>'This field is required'))
And you can create a custom WidgetFormSchemaFormatter by extending sfWidgetFormSchemaFormatter to format your form output.