Customize errors symfony - forms

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

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.

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.

Are there some way to delete "field:"
??
Yes: throw_global_error => true

Related

How to get choice options from another entity in Symfony

I want to load two choice list, the second one load only some values based on the first choice. But my problem comes first... how to load the EntityType values in the first list from a class that is not directly related to the current class (the form type class).
->add(
'cliente',
EntityType::class,
array(
'class' => 'AppBundle:Cliente',
'choice_label' => 'nombre',
)
)
But there is no one 'cliente' field in this entity, so it throws the message you know...
Neither the property "cliente" nor one of the methods "getCliente()",
"cliente()", "isCliente()", "hasCliente()", "__get()" exist and have
public access in class "AppBundle\Entity\Envio".
Please, do you know how to solve this issue? Any help is welcome!
According to your error your form is for the entity Envio. If you want to create an EntityType choicelist based on the Cliente entity, you'll need a doctrine relation in your Envio class:
class Envio
{
/*
* #ORM\ManyToOne(targetEntity="Cliente")
*/
protected $cliente;
The error has no relation to your question about having 2 choicelists and changing the choices of your second list based on the first choice. You're probably best off using javascript for that and you'll have many choices from AJAX to limiting the choices on the fly depending on the value or innerText of the .
For that error you need to make the field as 'mapped' => false, so:
->add(
'cliente',
EntityType::class,
array(
'class' => 'AppBundle:Cliente',
'choice_label' => 'nombre',
'mapped' => false
)
)
Then for getting the property in the controller you must do:
$cliente = $form->get('cliente')->getData();
Hope this help you.

How to change the default order of the sub panels in the opportunities module

I have a need to change the default order of the sub panels in the opportunities module. I have been looking for a solution for a while and have not found a solution that works.
I am using SugarCRM CE 6.5.13
Thanks in advance.
Your question is actually unclear. Are you asking for the order of a subpanel meaning how to place one subpanel above another. Or is your question concerning the order of the data inside a subpanel?
A fast response to both is:
in {MainModule}/metadata/subpaneldefs.php there should be the sort order declared for the subpanel like this:
$layout_defs[{MainModule}] = array(
// list of what Subpanels to show in the DetailView
'subpanel_setup' => array(
'{TheSubpanel}' => array(
'order' => 1, // influences the place of the subpanel relative to the other subpanels
'module' => '{TheSubpanel}',
'subpanel_name' => 'default', // attention please check the subpanel module to make sure there is not another setting overriding this setting
'sort_order' => 'desc',
'sort_by' => 'date_entered',
...
check also the file defined above that contains the subpanels fields. In this case it can be found in {TheSubpanel}/metadata/subpanels/default.php
$module_name = '{TheSubpanel}';
$subpanel_layout = array(
'top_buttons' => array(
'where' => '',
'sort_order' => 'desc',
'sort_by' => 'date_entered',
Please consider that after a change you need to run 'rebuild & repair' and if you manually clicked on a sort field then you should clear your cookie cache and log in again too.
There are other questions on stack overflow concering this like how-to-change-default-sort-in-custom-subpanel-sugarcrm
If you do not want code level customization then you can just drag and drop the sequence of subpanels on detail view of record. The sequence of subpanel will be saved.

How to prevent an empty form submission into CakePHP 2.5.2

I am new to CakePHP, how can i use javascript to prevent a form being submitted empty, i mean with all fields empty ?
The user just hit the submit button
I am using CakePHP 2.5.2
You don't need Javascript for such minor blank validation stuff. Just go through the validation section.
Check out this example below:
Model: User.php has the following validation code for email field.
class User extends AppModel {
public $validate = array(
'email' => array(
'required' => true,
'allowEmpty' => false,
'message' => 'Email cannot be empty.'
)
);
Setting required to true, and allowEmpty to false does the job for you. Also, the "message" field acts as the icing on the cake which indicates the error message to be shown when the validation fails.
Peace! xD

CakePHP Show Validation Message on Hidden Field

In my form I've created a hidden field:
echo $this->Form->hidden('editor_rating', array('value' => 0));
Which outputs:
In my model I've created a validation rule:
'editor_rating' => array(
'rule' => array('comparison', 'greater or equal', 1),
'message' => 'Please choose a valid Editor Rating'
)
When I submit the form the hidden field has an error class added, but no visible change and no error message:
<input id="ListingEditorRating" class="form-error" type="hidden" value="0" name="data[Listing][editor_rating]">
How can I make this error message visible or even attach it to a different div?
FormHelper::error
For use cases where Form->input or Form->inputs are not used you can render errors explicitly:
if ($this->Form->isFieldError('gender')) {
echo $this->Form->error('gender');
}
OK, so it doesn't look like there is any built in method to handle what I need which is understandable, so I'm handling it manually by checking the validationErrors for the field.
Here is a cleaner example than the editor_rating field I used perviously:
(artist_picker uses jQuery autocomplete to fetch a list of matching artists. We want to display the artist name in the input, but need to submit the artist_id to the DB, hence updating the hidden field)
echo $this->Form->hidden('artist_id', array('div' => false));
echo $this->Form->input('artist_picker', array(
'label'=> false,
'div'=> (isset($this->validationErrors['Listing']['artist_id']) ? 'span4 error' : 'span4'), // Turn on error class if errors
'class' => (isset($this->validationErrors['Listing']['artist_id']) ? 'span12 form-error' : 'span12'), // Turn on form-error class if errors
'after' => (isset($this->validationErrors['Listing']['artist_id']) ? '<div class="error-message">'.$this->validationErrors['Listing']['artist_id'][0].'</div>' : ''),
'type'=>'text') // Show error message if errors
);

Custom error message for input validators (using the array syntax)

ZF 1.11.2
I've tried most of the syntaxes. They didn't click.
$validators = array('product_name' => array('alnum'));
//...
$input = new Zend_Filter_Input($filters, $validators, $_POST);
How in the world do you set a custom error message for alnum with the syntax above? Using 'messages' => array('Not alnum!!')? Yeah, well... How? I must've tried 100 nested arrays.
Use the built in translator.
For example, configure the translator in your config file to use a simple array
; Translations
resources.translate.data = APPLICATION_PATH "/lang"
resources.translate.adapter = "Array"
resources.translate.options.scan = "directory"
resources.translate.options.disableNotices = "1"
This tells the Translate application resource plugin that you want to
keep your translations under APPLICATION_PATH/lang
use the Array adapter (simplest)
scan the translation directory for languages / locales
ignore errors about unknown translations (ie user preferes en_AU but you don't have a specific translation file for that language)
Now, create folders for any languages you want to support. At a minimum, you'll want application/lang/en. For example
application
lang
en
en_AU
en_US
In each language folder, create a translate.php file. This file will contain (and return) an array of key / value pairs for each translation. You can find the keys for each validator message in the validator class. Here's an example for the Alnum validator
<?php
// application/lang/en/translate.php
return array(
Zend_Validate_Alnum::NOT_ALNUM => 'Not alnum!!',
Zend_Validate_Alnum::INVALID => 'Not valid!!'
);
For all Zend validators, you can also use the %value% placeholder in your message, eg
Zend_Validate_Alnum::NOT_ALNUM => "'%value%' is not alpha-numeric"
If you are simply trying to change the validation messages for a form element, I have always done it like this (inside a class that extends Zend_Form):
$this->addElement('text', 'myTextField', array(
'label' => 'The Label',
'description' => 'The description for the field...',
'filters' => array(
'StringTrim',
// etc
),
'validators' => array(
array('NotEmpty', true, array(
'messages' => 'This field is required',
)),
array('AnotherValidator', true, array(
'messages' => 'Bad value',
)),
// etc
),
));
Are you saying that this didn't work? Or are you using your validator in a more general context, in which case #Phil Brown's (awesome!) answer will do the job.
Disabling the translator on the element will disable the translation of all the validator messages. It is not possible to use a translator on the form or element and overwrite just one validator message. When the element is validated the translator is injected to every validator. The validator will use the translator if it is set. Thereby the custom error message won't be used.
Zend_Validate_Abstract::_createMessage()
// $message is your custom error message
$message = $this->_messageTemplates[$messageKey];
if (null !== ($translator = $this->getTranslator())) {
// your custom error message gets overwritten because the messageKey can be translated
if ($translator->isTranslated($messageKey)) {
$message = $translator->translate($messageKey);
} else {
$message = $translator->translate($message);
}
}
I think it is only possible to use a custom error message by disable the translator on the element.
$element->setDisableTranslator(true)
Use setMessage and disable translator if you have one.
$alnum = new Zend_Validate_Alnum();
$alnum->setDisableTranslator(true);
$alnum->setMessage(
'Not alnum!!',
Zend_Validate_Alnum::NOT_ALNUM
);
$validators = array('product_name' => array($alnum));
If you use your validator on a form element, you have to disable the translator on the element.