I have a cakephp form that has validation. The validation itself works BUT when an error shows up after clicking submit, it just produces some text.
Why am I getting no colour. eg Its meant to display errors in red.
Controller
<div class="users form">
<?php echo $this->Form->create('Ticket'); ?>
<fieldset>
<legend><?php echo __('Purchase'); ?></legend>
<?php
echo $this->Form->input('first_name');
echo $this->Form->input('last_name');
echo $this->Form->input('email');
echo $this->Form->input('phone');
echo $this->Form->input('date', array('options'=> $dates));
echo $this->Form->input('quantity', array('options' => $maxAmount, 'default' => '1'));
?>
</fieldset>
<?php
echo $this->Form->end(__('Purchase'));
?>
</div>
Model
public $validate = array(
'first_name' => array(
'rule' => '/^[a-zA-Z]{1,}$/i',
'message' => 'Alphabets only',
'required' => true
),
'last_name' => array(
'rule' => '/^[a-zA-Z]{1,}$/i',
'message' => 'Alphabet only',
'required' => true
),
'phone' => array(
'rule' => 'numeric',
'message' => 'numbers only please',
'required' => true
),
'email' => array(
'rule' => 'email',
'message' => 'Your email is not valid',
'required' => true
),
'quantity' => array(
'rule' => 'numeric',
'message' => 'numbers only please',
'required' => true
)
);
Did you include a stylesheet in your default.ctp? If you removed the default CakePHP stylesheet from your default.ctp layout, the default colours will no longer be there.
You need to either include the CakePHP stylesheet again in your layout (here you can see how it was in the original default.ctp: https://github.com/cakephp/cakephp/blob/master/app/View/Layouts/default.ctp#L33)
Or create your own CSS styles in your stylesheet. You can use the styles from the default CakePHP stylesheet as an example;
https://github.com/cakephp/cakephp/blob/master/app/webroot/css/cake.generic.css#L371
There is nothing wrong with your code. That is just how CakePHP is handling the error reporting. The red stuff is reserved for major errors like missing view, or a missing function, or cant connect to the database. Basically stuff that would generate a status code that is in the range of 400.
I did some searching to answer your question better, but i stumbled on this page.
CakePHP 2.0 - How to make custom error pages?
Its all about what status code CakePHP will generate when u do something wrong.
Validation errors will I think throw even an OK (200) but wont write anything to the database. Happened a couple a times to me.
Related
Following up on my previous question, I am supposedly passing the submitted post data as query strings like so:
echo $this->Form->create('Donor',array(
'url' => array_merge(array('action' =>'find'), $this->params['pass'])
));
But when I try the following within my controller's find action :
$this->Paginator->settings['conditions'] = $this->Donor->parseCriteria($this->Prg->parsedParams());
The $this->Prg->parsedParams() only consists of the criteria:
here's the var_dump
array (size=1)
'criteria' => string 'blood_group_id' (length=14)
And here is my view code :
<?php
echo $this->Form->create('Donor',array(
'url' => array_merge(array('action' =>'find'), $this->params['pass'])
));
echo $this->Form->input('criteria',array(
'label'=>'Search Criteria',
'options' => array(
'id'=> 'By ID',
'name' => 'By Name',
'blood_group_id' => 'By Blood Type',
'type' => 'By Donor Type',
'age' => 'By Age',
'gender' => 'By Gender'
)
));
?>
<?php echo $this->Form->input('query', array( 'id' => 'query', 'name' => 'query', 'label' => false, 'placeholder' => 'Search')); ?>
<?php echo $this->Form->end(__('Search'));?>
I believe that I should be receiving all submitted data and not only the criteria's value.. I do not know what is wrong, and frankly this is taking so much time to put together. I cant seem to figure out how to work with this plugin. Is it just me ? Please, if anyone could find the time to help out, I will be grateful!
The Answer was almost under my nose.. I inattentively was naming the 'query' input twice,
<?php echo $this->Form->input('query', array( 'id' => 'query', 'name' => 'query','label' => false, 'placeholder' => 'Search')); ?>
I removed the 'name' key, and everything worked fine! That was one stupid mistake, which wasted about an hour for me, so im hoping this would help someone who comes across something like this! Thanks
I have a form on my homepage from an element in my Leads plugin. I have this in my Lead model:
public $validate = array(
'last_name' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Your custom message here',
'allowEmpty' => false,
),
),
'phone' => array(
'rocket' => array(
'rule' => array('phone', null, 'us')
),
),
);
My app is validating correctly because it's not posting to my database unless I fill in required fields. The problem is that the because my url isn't /leads/add it's not showing my custom error messages on my fields when I submit the form.
I think it has something to do with the fact that I'm using an element. Anyone have any ideas or had trouble with this before?
The rule should be notEmpty instead of notempty. What is working right now is the allowEmpty set to false which makes sure the field is not empty using !empty($field) || is_numeric($field).
Just starting out playing with CakePHP so please bear with me if this is an easy question.
I'm using CakePHP 2.4.0 and I want to reuse a specific form in multiple views. Therefore, I add this form to an view-element and include this element in each view.
This is the element.ctp file:
<?php
echo $this->Form->create('Lead', array('type' => 'post',
'url' => array('controller' => 'Lead', 'action' => 'index'),
'novalidate' => true));
echo $this->Form->input('name', array('label' => 'Achternaam'));
echo $this->Form->input('email', array('label' => 'Email'));
echo $this->Form->input('phone', array('label' => 'Telefoon'));
echo $this->Form->submit('submit', array('name' => 'submit'));
?>
This element is included in multiple views in the following way:
This is the home.ctp (a view) file:
<?php echo $this->element('element'); ?>
This is the LeadController.php file that receives the post operation from the form:
<?php
class LeadController extends AppController{
public function index(){
$this->autoRender = false;
if(!empty($_POST))
$this->Lead->save($this->request->data);
$this->redirect('/Pages/home');
}
}
?>
This is the Lead.php file that hodls all the validation checks.
class Lead extends AppModel{
public $useTable = 'Leads';
public $validate = array(
'email' => array(
'required' => array(
'rule' => array('notEmpty'),
'required' => true,
'message' => 'need email'
),
'validEmailRule' => array(
'rule' => array('email'),
'required' => true,
'message' => 'invalid emial'
)
),
'name' => array(
'required' => array(
'rule' => array('notEmpty'),
'required' => true,
'message' => 'need name'
)
),....
When the form in the view-element is completely valid, than the data from the form is successfully added to the database. But when the form is invalid, than the errors aren't returned to the view. If I write the following in the LeadController to the logfile than they are shown their.
$this->Lead->invalidFields();
If I add the code from LeadController/index to PagesController/home and change the form submit url, than the errors are shown in the view at each form-element.
What do you have to do to show the errors from a form in the view at each form-element, when the form is placed in a separate view-element and reused in multiple pages?
This has probably something to do with the fact that I use a redirect but I think their is a better way than saving the errors temporarily in a session.
This has probably something to do with the fact that I use a
redirect...
It's actually COMPLETELY to do with the fact that you use a redirect. By redirecting, you lose the validation errors.
There are as many ways to deal with this as your imagination can think of (and each has it's own merits depending on YOUR situation), but it is already widely asked/answered on the web:
CakePHP preserving validation errors after redirecting
http://bakery.cakephp.org/articles/binarycrafts/2010/01/20/persistentvalidation-keeping-your-validation-data-after-redirects-2
CakePHP: Keep validation data upon redirect
https://groups.google.com/forum/#!topic/cake-php/NsfckwSfY5c
I have a model which has a primary key of 'staff_code'
I have customer validation rules as below:
public $validate = array(
'staff_code' => array(
'unique' => array(
'rule' => 'isUnique',
'message' => 'This Staff Code already exists',
'last' => true
),
'sc_required' => array(
'rule' => 'notEmpty',
'message' => 'The Staff Code must be specified',
))
);
When the field is not unique the validation error does not display
Has anyone overcome this error, I think it is related to the primary key being inputted by the user.
I have to keep the schema as it is as it is used by another piece of software and would be a real nightmare to change the way this works.
Not sure if need but my view is :
<?php
echo $this->Form->create('Staff');
echo $this->Form->inputs(array(
'staff_code' => array('type' => 'text', 'autocomplete' => 'off'),
'login_name' => array('type' => 'text', 'autocomplete' => 'off'),
'person_name' => array('type' => 'text', 'autocomplete' => 'off'),
'password',
'_allowed_to_do_po',
'is_active'
));
echo $this->Form->input('role', array('options' =>
array('user' => 'user', 'admin' => 'admin'), 'empty' => false, 'default'=>'user'));
echo $this->Form->end(array(
'label' => 'Save',
'class' => 'btn'
));
?>
...and my custom save function from the Staff controller:
function emSave($data) {
if (!$this->find('first', array('conditions' => array('staff_code' => $data['Staff']['staff_code'])))) {
if ($this->save($data)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
NB: The error messages work fine for my other fields that are no primary keys.
The problem is, I think, that you are doing a validation lookup in your controller before you are calling save. Save automatically calls your validation, so you don't need to look it up first, that's the whole point of the Unique rule, it does that for you.
You might also want to add 'required' => true to your validation if you want the field to be valid.
However, having a user input a primary key field in your database is bad practice in my opinion. It should be automatically generated and invisible to the user.
I'm using cakephp 2.0 and I'm having problem using Cake's validation thing, which works wonderful when used for it alone.
var $validate = array(
'loginname' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 3),
'required' => true,
'allowEmpty' => false,
'on' => 'create',
),
'alphaNumericRule' => array(
'rule' => 'alphaNumeric',
),
'uniqueRule' => array(
'rule' => 'isUnique',
),
),
'password' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 5),
'required' => true,
'allowEmpty' => false,
),
// on so on ...
Due to Internalization I put the error messages into the view like this:
echo $this->Form->input('display_name', array(
// some other options ...
'error' => array(
'betweenRule' => __('Your Dispay Name must contain at least 3 characters and should be maximal 20 characters long.'),
'uniqueRule' => __('This Display Name is already in use!'),
),
);
Now I want jQuery to validate the form on client-side as well with some little - sexy message box to return validation errors if something goes wrong.
The Ajax Request works fine but the problem here is: I only get returned the name of the validation rule, not the message.
sure: Because I didn't specified any messages in the model.
But how can I get those messages I declared in the view?
Is there any solution without having to type everything twice (PHP and JS)?