CakePHP Show Validation Message on Hidden Field - forms

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
);

Related

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

How to submit the value of a disabled form field?

This page actually a preview where user can't change anything,that he has given before.I have tried bellow code,
echo $this->Form->input('exchange_type', array(
'disabled' => 'disabled',
'empty' => '--Please Select--',
'options' => array(
'6' => 'POINT_TO_PRODUCT',
'7' => 'POINT_TO_GIFT',
'2' => 'POINT_TO_GAME'
)
));
Here field has disabled but it's sending null value to database.I am trying to send actual value that user has been selected.How can I do this ?
That's how HTML works, values of disabled elements are not being sent.
What you can do is using a hidden field, that's what the form helper automatically does when using for example checkboxes, in order to ensure that there's always a value being sent, as unchecked checkboxes do not submit any value, just like disabled inputs.
The hidden field should have the same name as the actual field, and it should be placed before the actual field, that way the hidden value will only be sent in case the following element is disabled.
echo $this->Form->hidden('exchange_type');
echo $this->Form->input('exchange_type', array(
'disabled' => true,
// ...
));
That would pick up the previously POSTed value for both the hidden input and the select input, and the hidden input would be submittable.
See also Cookbook > Helpers > FormHelper > FormHelper::hidden()

$this->request->data EMPTY - When I have disabled Fields CakePHP 2

I have a form which has some disabled fields, when the form is submitted both $this->request->data and $_POST is empty, removing the disabled fields and it is fine again. I would have though it would still pass though the non-disabled fields. I've even tried to remove the disabled field attribute when the submit button is pushed but this still returns an empty array.
Is there something cake related that might be causing this?
Thanks
// SNIPPET FROM THE VIEW CODE:
$this->Form->create('Card', array('class' => 'GeneralValidate'));
$this->Form->input('Card.property_id', array('type'=>'select', 'empty'=>true , 'class' => 'required adminOnlyField', 'div' => array('class' => 'required')));
$this->Form->input('Card.building_id', array('type'=>'select', 'empty'=>true, 'id' => 'BuildingSelector', 'class' => 'adminOnlyField', 'label' => 'Building (If Applicable)'));
$this->Form->input('Prospect.waiting_list_details', array('value' => $prospect['Prospect']['waiting_list_details']));
$this->Form->input('SaleDetail.property_sold', array('class' => 'checkbox', 'checked' => $ps_checked));
$this->Form->input('SaleDetail.date_conditions_met', array('type'=>'text', 'class' => 'text date_picker adminOnlyField', 'value' => $this->Date->format($saledetail['SaleDetail']['date_conditions_met'])));
$this->Form->button('Save & Continue', array('type'=>'submit', 'label' => 'Save', 'name' => 'quicksave' , 'class' => 'submit long clear_ready_only'));
// JS FROM THE VIEW
$(function () {
var $adminOnly = $('.adminOnlyField');
$adminOnly.prop('disabled',true).prop('readonly',true);
$adminOnly.attr("onclick","return false");
$adminOnly.attr("onkeydown","return false");
$adminOnly.removeClass('required');
$adminOnly.removeClass('date_picker');
$('.clear_ready_only').click(function(e)
{
e.preventDefault();
$adminOnly.prop('disabled',false).prop('readonly',false);
$adminOnly.attr("onclick","return true");
$adminOnly.attr("onkeydown","return true");
$('#CardModifysaleForm').submit();
});
});
That's the way HTML works, disabled don't get posted. CakePHP can't change what is sent from the browser. If you still want the value you can set it as a hidden element.
Update
Some problems I see:
Missing Form::end() in view (always a good idea to insert it).
You never said your form was submitted from JS, first test with a simple form POST then JS.
Your JS code is set to submit a form by ID CardModifysaleForm. There's no such ID in your supplied view code and you're not setting your form to that ID from the snippet you supply.
I ended up removing the disabled option from this, leaving the ready only and added some addition CSS stylings so it looked disabled to the user. This is not the exact answer to the question but works as a different approach.

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.

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