How to prevent an empty form submission into CakePHP 2.5.2 - cakephp-2.5

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

Related

$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.

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

validation issue with zend checkbox

everyone, I have create a zend form have a checkbox for license agreement. I want to show an error message when a user submit the form with out checking the agreement check box.
Thanks
Anurodh
There is a bug in zend_framework with the required checkbox. But you can do something like this.
$this->addElement(
'checkbox', 'checbkox_name', array(
'required' => true,
'uncheckedValue' => null
)
);
edit for readability
$acceptAggrement = new Zend_Form_Element_Checkbox('accept_aggrement');
$acceptAggrement->setLabel("Accept Aggrement :");
$acceptAggrement->setUncheckedValue(null);
$acceptAggrement->setRequired(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.

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