Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 555413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:48:44+00:00 2026-05-13T11:48:44+00:00

There are some best practice in Symfony to customize form errors? For exemple, if

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:48:44+00:00Added an answer on May 13, 2026 at 11:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.