views:

274

answers:

3

There are some "best practice" in Symfony to customize form errors? For exemple, if i would to show "Campo obligatorio" when the field is required.

1)How can i do that better way and independent from what forms call it? 2)How can i customize message 'An object with the same "%namefield" already exist.' ?

Thanks

updated

sorry, but if i try to do 'invalid' how you said me... it print me the same error

$this->setValidator('urlres', new sfValidatorString(array( 'min_length' => 6, ), array( 'min_length' => 'URL must be longer', 'required' => 'Required field', 'invalid' => 'URL exist' )));

prints me: * An object with the same "urlres" already exist.

updated Felix, your solution is fantastic but it prints me this error: "urlres: that url already exists"

Are there some way to delete "field:" ??

Thanks

A: 

Hi

Let me try to help you. You can easily customize standard form errors in configure method of your form class. Here is an example: 1)

<?php
   class myForm extends BaseMyForm
      public function configure(){
         parent::configure();
         $this->setValidator(
            'my_field' => new sfValidatorString(
               array('min_length'=>3, 'max_length'=>32, 'required'=>true),
               array('required' => 'Hey, this field is required!')
            )
         );
      }

2) Just change a message that has a code 'invalid'.

All you need is just find a valid message code to customize particular default messages. More info - Symfony Forms in Action: Chapter 2 - Form Validation

Updated:

And if you don't want to customize error messages in all your form classes, just create your own base validator class:

abstract class myBaseValidator extends sfValidatorBase

and there redefine default 'required' and 'invalid' messages.

Darmen
DArmen, thanks but what i try to say is... probabily in a project you have more than one form from the same entity/object or probabily in all entities/objects you want to show message 'you must enter field'. Is this possible to write 'default errors' in only one place?I will try 'invalid' error and hope that works :)thanks Darmen.
nebur85
I see in another projects that they create a class and inside they define text errors. This way they can do this, but... there are another way?
nebur85
Please look at the updated answer, that should help
Darmen
look at my updated question.thanks
nebur85
@Darmen: YOU ARE DOING IT WRONG - hardcoding detected
develop7
@Develop7, yes, I know, my bad. Just wanted to show schematically
Darmen
Creating a custom base validator to set default `required`, `invalid` messages is really unnecessary (if not even wrong), as you can set the default with e.g. `sfValidatorBase::setDefaultMessage('required', 'Field required');`
Felix Kling
+2  A: 

Maybe this form post helps you:

Put the code

sfValidatorBase::setDefaultMessage('required', 'Field required');

in the "configure" of you application configuration apps/youApp/config/yourAppConfiguration.class.php.

You should be able to set the default value for every error message type this way.


If you want to set certain error messages for certain fields, think about to create a form class that defines all this and let all other forms inherit from this one.
The subclasses then only specify which fields should be displayed (and maybe custom validation logic).

You can find an example how to do this in the Admin Generator chapter of the symfony book.

This is the cleanest approach IMHO.


Edit:

If you want leave fields blank, you have to add the required => false option:

  'email'   => new sfValidatorEmail(array('required' => false))

Regarding the error message: This sounds like the urlres is marked as unique in the database table and the value already exists. Maybe you should check the database schema definition.


Edit 2:

To test both, length and uniqueness, you should use sfValidatorAnd and sfValidatorDoctrineUnique:

$this->setValidator('urlres', new sfValidatorAnd(
                    array(
                      new sfValidatorString(
                            array( 'min_length' => 6, ), 
                            array( 'required' => 'Required field',
                                   'min_length' => 'URL must be at least %min_length% chars long.' )
                      ),
                      new sfValidatorDoctrineUnique(
                            array( 'model' => 'yourModel', 
                                   'column' => 'theColumn', 
                                   'primary_key' => 'thePrimaryKeyColumn',
                                   'throw_global_error' => false),
                            array('invalid' => "That URL already exists")
                      )
                    ));

Also your use of the invalid error code in the string validator is not correct. You set the invalid message to URL exists but how can a string validator know this? It only checks whether the given string meets the min_length, max_length criteria or not.

Btw I assumed that you use Doctrine but I think the same validators are available for Propel.


Edit 3:
Set the option 'throw_global_error' => false. But I am not sure if that works.

You can also have a look at the source code if it helps you.

Felix Kling
Thanks Felix, it works fine and it is a clean method to do what i want.Can u help me with the "An object with the same "urlres" already exist." error? what error type must i catch to customize this error message?thanks a lot.
nebur85
And another question... is possible to validate a field only if it is filled? In my form i could fill email address or phone number... but to validate email adress i need to assign a sfValidatorEmail to email field...if i do that,they raise an 'invalid' error...
nebur85
yes felix, is unique. so i have to read before insert to show a custom error?thanks
nebur85
thanks felix, it works fine :)it prints me this error "urlres: that url already exists", therer are some way to only show the error message without field..?
nebur85
@nebur85: I updated my post.
Felix Kling
A: 

Are there some way to delete "field:" ??

Yes: 'throw_global_error' => true

srus