Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i change a symfony form error message?

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.

like image 400
Benjamin Crouzier Avatar asked Jan 18 '26 20:01

Benjamin Crouzier


1 Answers

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\">\n%errors%  </ul>\n",
    $errorRowFormatInARow      = "    <li>%error%</li>\n",
    $namedErrorRowFormatInARow = "    <li>%name%: %error%</li>\n",
    $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');
like image 105
arsenik Avatar answered Jan 20 '26 10:01

arsenik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!