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

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.

Related

Duplicate Validation on Combined Fields in zend form

Hi there I have a table in which combination of three fields is unique. I want to put the check of duplication on this combination. Table looks like
I know how to validate single field, But how to validate the combination is not know. To validate one field I use the following function
public function isValid($data) {
// Options for name field validation
$options = array(
'adapter' => Zend_Db_Table::getDefaultAdapter(),
'table' => 'currencies',
'field' => 'name',
'message'=> ('this currency name already exists in our DB'),
);
// Exclude if a id is given (edit action)
if (isset($data['id'])) {
$options['exclude'] = array('field' => 'id', 'value' => $data['id']);
}
// Validate that name is not already in use
$this->getElement('name')
->addValidator('Db_NoRecordExists', false, $options
);
return parent::isValid($data);
}
Will any body guide me how can I validate duplication on combined fields?
There is no ready to use validator for this, as far as I know. You have either to write your own, or do a check with SQL-query with three conditions (one for each field).
you have to Apply a validation on name element of zend form.
Here is code for add validation on name field.
$name->addValidator(
'Db_NoRecordExists',
true,
array(
'table' => 'currencies',
'field' => 'name',
'messages' => array( "recordFound" => "This Currency Name already exists in our DB") ,
)
);
And you must set required true.

Zend form in a popup (fancybox, lightbox....)

I am developping a web application using Zend and I ran out of ideas for a problem I am having. In just a few words, I am trying to have a contact form in a popup (Fancybox, lightbox, colorbox or whatever...). The whole thing works fine, in the sense that it shows up the contact form in the popup and allows to send emails. However, whenever there are errors (unfilled input or filled wrong), I couldn't get those errors to be displayed on the popup (it actually redirects me back to the form in a normal display (view+layout), to show the errors.
It is perhaps possible but I now thought that perhaps I could more easily bring my error message to a new popup (the contact page, filled unproperly, would lead to a error popup page...). I think this alternative could look cool but am having real trouble doing it. Now my real question is : Can we really make a form on a popup, using Facybox (Lighbox or any other actually ... just want my popup) and Zend? Any Guru outhere??
Thanks a lot
here is the code:
the link for instance:
<a class="popLink" href=" <?php echo $this->url(array('module'=>'default', 'controller'=>'contact', 'action'=>'sendmail')).'?ProID='.$this->proProfil->getProID(); ?>">Contact</a>
the action:
public function sendmailAction()
{
$this->_helper->layout()->setLayout('blank');
$request = $this->getRequest();
$proID = $this->_getParam("ProID");
$professionalsList = new Model_DirPro();
$proName = $professionalsList->getProInfo($proID);
$translate = Zend_Registry::get('translate');
Zend_Validate_Abstract::setDefaultTranslator($translate);
Zend_Form::setDefaultTranslator($translate);
$contactform = new Form_ContactForm();
$contactform->setTranslator($translate);
$contactform->setAttrib('id', 'contact');
$this->view->contactform = $contactform;
$this->view->proName = $proName;
if ($request->isPost()){
if ($contactform->isValid($this->_getAllParams())){
$mailSubject = $contactform->getValue('mailsubject');
if ($contactform->mailattcht->isUploaded()) {
$contactform->mailattcht->receive();
//etc....
the form:
class Form_ContactForm extends Zend_Form
{
public function init ()
{
$this->setName("email");
$this->setMethod('post');
$this->addElement('text', 'mailsubject',
array('filters' => array('StringTrim'),
'validators' => array(), 'required' => true, 'label' => 'Subject:'));
$mailattcht = new Zend_Form_Element_File('mailattcht');
$mailattcht->setLabel('Attach File:')->setDestination(APPLICATION_PATH.'/../public/mails');
$mailattcht->addValidator('Count', false, 1);
$mailattcht->addValidator('Size', false, 8000000);
$mailattcht->addValidator('Extension', false,
'jpg,png,gif,ppt,pptx,doc,docx,xls,xslx,pdf');
$this->addElement($mailattcht, 'mailattcht');
$this->addElement('textarea', 'mailbody',
array('filters' => array('StringTrim'),
'validators' => array(), 'required' => true, 'label' => 'Body:'));
$this->addElement('submit', 'send',
array('required' => false, 'ignore' => true, 'label' => 'Send'));
$this->addElement('hidden', 'return', array(
'value' => Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(),
));
$this->setAttrib('enctype', 'multipart/form-data');
}
}
I would suggest implementing AJAX validation. This would allow for the form to be verified before it is submitted. ZendCasts has a good tutorial on how to accomplish this: http://www.zendcasts.com/ajaxify-your-zend_form-validation-with-jquery/2010/04/
Ajax requests are handled via the contextSwitch action helper. You can to specify the various contexts an action needs to handle (xml or json) in the init method of the controller as follows:
public function init()
{
$this->_helper->contextSwitch()
->addActionContext('send-mail', 'json')
->initContext()
;
}
The request url should contain a "format=json" appended to the query string. This will execute the action and send the response in json format. The default behaviour of JSON context is to extract all the public properties of the view and encode them as JSON. Further details can be found here http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
I found a "probably not the prettiest" working solution, it is to indeed use ajax as mentioned in the previous zendcast for validation to stop the real validation (preventdefault), process the data return the result and if everything's ok restart it.

Arbitrary Form Processing with Drupal

I am writing a module for my organization to cache XML feeds to static files to an arbitrary place on our webserver. I am new at Drupal development, and would like to know if I am approaching this the right way.
Basically I:
Expose a url via the menu hook, where a user can enter in a an output directory on the webserver and press the "dump" button and then have PHP go to drupal and get the feed xml. I don't need help with that functionality, because I actually have a prototype working in Python (outside of Drupal)..
Provide a callback for the form where I can do my logic, using the form parameters.
Here's the menu hook:
function ncbi_cache_files_menu() {
$items = array();
$items['admin/content/ncbi_cache_files'] = array(
'title' => 'NCBI Cache File Module',
'description' => 'Cache Guide static content to files',
'page callback' => 'drupal_get_form',
'page arguments' => array( 'ncbi_cache_files_show_submit'),
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
I generate the form in:
function ncbi_cache_files_show_submit() {
$DEFAULT_OUT = 'http://myorg/foo';
$form[ 'ncbi_cache_files' ] = array(
'#type' => 'textfield',
'#title' => t('Output Directory'),
'#description' => t('Where you want the static files to be dumped.
This should be a directory that www has write access to, and
should be accessible from the foo server'),
'#default_value' => t( $DEFAULT_OUT ),
'#size' => strlen( $DEFAULT_OUT ) + 5,
);
$form['dump'] = array(
'#type' => 'submit',
'#value' => 'Dump',
'#submit' => array( 'ncbi_cache_files_dump'),
);
return system_settings_form( $form );
}
Then the functionality is in the callback:
function ncbi_cache_files_dump( $p, $q) {
//dpm( get_defined_vars() );
$outdir = $p['ncbi_cache_files']['#post']['ncbi_cache_files'];
drupal_set_message('outdir: ' . $outdir );
}
The question: Is this a decent way of processing an arbitrary form in Drupal? I not really need to listen for any drupal hooks, because I am basically just doing some URL and file processing.
What are those arguments that I'm getting in the callback ($q)? That's the form array I guess, with the post values? Is this the best way to get the form parameters to work on?
Thanks for any advice.
You can process forms in two stages, validate and submit.
Validate is for when you want to validate some user provided and raise form errors if some user input was invalid (like an invalid url or email address)
Submit which is the one you use is called if a form passes all of its validations, so at that point if you made a proper validation you will know that the data supplied by the user is okay.
Your submit function should look like this:
function ncbi_cache_files_dump(&$form, &$form_state) {
// $form: an array containing the form data
// $form_state: data about the form, like the data inputted in the form etc.
// code...
}
I think you need 2 separate forms here:
for setting the directory (the one you have now);
for making a dump (another form that would use the configured path).
Also it seems logical to publish the previously saved path as the default value in the settings form (instead of a hard-coded path).
And in general you should check the form input data from the second parameter of the submit callback:
function ncbi_cache_files_dump(&$form, &$form_state) {
$outdir = $form_state['values']['ncbi_cache_files'];
// ...
}

Customize errors symfony

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

Custom Zend Error message for checkboxes

I have a form in a Zend-based site which has a required "Terms and Conditions" checkbox.
I have set a custom message which says "you must agree with terms and conditions".
however, because the checkbox is "presence='required'", it returns
Field 'terms' is required by rule 'terms', but the field is missing
which is this constant defined in the Zend framework:
self::MISSING_MESSAGE => "Field '%field%' is required by rule '%rule%', but the field is missing",
I could edit this constant, but this would change the error reporting for all required checkboxes.
How can I affect the error reporting for this specific case?
If you are using the Zend_Form_Element_Checkbox you can customize the error messages on the Zend_Validate validators.
$form->addElement('checkbox', 'terms', array(
'label'=>'Terms and Services',
'uncheckedValue'=> '',
'checkedValue' => 'I Agree',
'validators' => array(
// array($validator, $breakOnChainFailure, $options)
array('notEmpty', true, array(
'messages' => array(
'isEmpty'=>'You must agree to the terms'
)
))
),
'required'=>true,
);
You want to make sure the unchecked value is "blank" and that the field is "required"
You can override the default message like this:
$options = array(
'missingMessage' => "Field '%field%' is required by rule '%rule%', dawg!"
);
And then:
$input = new Zend_Filter_Input($filters, $validators, $myData);
Or
$input = new Zend_Filter_Input($filters, $validators, $myData);
$input->setOptions($options);
...and finally:
if ($input->hasInvalid() || $input->hasMissing()) {
$messages = $input->getMessages();
}
It's mentioned on the Zend_Filter_Input manual page.
Following #gnarf answer, for those of you who may set Form Fields in a slightly different way (like me), you could also do the following:
$agree_tc_and_privacy = new Zend_Form_Element_Checkbox('agree_tc_and_privacy');
$agree_tc_and_privacy
->setLabel("My T&Cs Agreement text ...")
->addValidator('NotEmpty', false, array('messages' => 'You must and agree...'))
->setRequired(true)
->setOptions(
array(
'uncheckedValue'=> '', //important as explained by gnarf above
'checkedValue' => '1',
)
);
$this->addElement($agree_tc_and_privacy);