I have an email field in my model registration form and other fields like name, country, age.
and I have to do an ajax validation onChange event only for email field .
I was set my view to enable ajax validation.
my problem is the ajax validation applied for all field not only for email, I need to do this action only for one field onChange event.
this my view
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'Login-Form',
'enableClientValidation'=>true,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
echo $form->errorSummary($loginForm);
echo $form->textFieldRow($loginForm, 'email');
echo $form->textFieldRow($loginForm, 'name');
echo $form->textFieldRow($loginForm, 'age');
echo $form->textFieldRow($loginForm, 'country');
and this is my model
public function rules()
{
return array(
array('email, country, name', 'required'),
array('email', 'checkEmail')
);
}
// custom function to check email
public function checkEmail($attribute, $params)
{
$this->addError($attribute,strtr('email exsit before',$params));
}
how can enable ajax validation only for email and the other filed (country, name, age) on client side without ajax validation.
Please help I send a lot of time to do that.
Thanks
Set the form clientOption on change like this:
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'Login-Form',
'enableClientValidation'=>true,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
So that on change it can trigger the validation
This can be done by setting the 4th parameter to false on validate trigger.
<?php echo $form->error($model,'email', array(), false); ?>
In your model set this
public function rules()
{
return array(
array('email, country, name', 'required'),
array('email', 'unique'), //check if email already exist
array('email', 'email'),
);
}
in your controller
<?php
class SiteController extends CController {
.... // Other functions
public function actionLogin(){ // Your corresponding action
$model= new LoginForm(); // Your actual model name
$_POST['ajax'] === 'Login-form' make sure that login form is the same as your form name in view
if (isset($_POST['ajax']) && $_POST['ajax'] === 'Login-form') {
echo CActiveForm::validate($model,);
Yii::app()->end();
}
... // Remaining action logic
in your view
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'Login-Form',
'enableClientValidation'=>false,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>false,
'validateOnChange'=>false,
'validateOnType' => true,
To perform validation via ajax you first need to call a helper function in your controller in addition to you enabling the enableAjaxValidation in your form view
In your Controller ( I am assuming the SiteController since this a login form )
<?php
class SiteController extends CController {
.... // Other functions
public function actionLogin(){ // Your corresponding action
$model= new LoginForm(); // Your actual model name
if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
echo CActiveForm::validate($model,array('email'));
Yii::app()->end();
}
... // Remaining action logic
see Validate and enableAjaxValidation api docs for full details how this works
Related
I have a field set as required in the model, yet I have seen users saving it as an empty string (ie. ''). When I tested it, I do receive the "Cannot be blank" message properly, so don't know how to prevent this in the future. Do I have to specify all scenarios in the rule (eg, 'insert', 'update')? By the way, I tried updating the field, and it doesn't let me save it empty (I even tries spaces).
These are the rules applied on the field (model):
public function rules()
{
return array(
array('field', 'required'),
array('field', 'length', 'max'=>4096),
array('field', 'safe', 'on'=>'search'),
);
}
For #RiggsFolly :) the controller action:
public function actionUpdate($id)
{
$model = Model::model()->findByPk($id);
$formData = Yii::app()->request->getPost('Model');
if ($formData)
{
$model->attributes = $formData;
$model->save();
}
$this->render('update',array(
'model'=>$model
));
}
... and the view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'form'
)); ?>
<?php echo $form->textArea($model,'text',array( 'rows'=>5 ')); ?>
<?php $this->endWidget(); ?>
Can you imagine any scenario this field could be saving an empty string in the database?
I have created a form which I need to validate using model and controller .Here is my form
index.ctp
<?php echo $this->Form->create('Contact',array('url'=>array('controller'=>'contacts','action'=>'add')));
echo $this->Form->text('name');
Model : Contact.php
class Contact extends AppModel
{
var $name = 'Contact';
var $useTable = false;
public $validate = array(
'name' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => false,
'message' => 'Letters and numbers only'
),
'between' => array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters'
)
)
);
}
Controller : ContactsController.php
public function add()
{
$this->Contact->validates();
$this->request->data['Country']['country_name']=$this->request->data['Contact']['country'];
$this->Country->saveall($this->request->data);
$this->redirect('/Contacts/index/');
}
I am trying to do the validation by googling but it seems difficult to me so if anyone could describe the process it would be a great help .My cakephp version is 2.3.8. I just need to validate this name field , as when I click in submit it will show this message in the form.
Your controller code should be like this
The process of validation in CakePHP is like
1) as you have defined validation rules in CakePHP model public `$validates = array();`
2) when ever you do a save on particular model directly or through any association
a callback method beforeValidate for that model gets called to validate the data which is being saved.
3) once the data is validated then beforeSave callback is called after this save method is called.
4) we can also validate the form input fields in controller using $this->Model->validates() but then while saving we have to disable the beforeValidate callback by doing
$this->Model->save($data,array('validate'=>false));
Otherwise you will end validating the same data twice
your controller code should be somewhat like this.
public function add() {
// here we are checking that the request is post method
if ($this->request->is('post')) {
$this->request->data['Country']['country_name']
= $this->request->data['Contact']['country'];
// here we are saving data
if ($this->Contact->saveAll($this->request->data)) {
//here we are setting a flash message for user
$this->Session->setFlash('your record has been added','success');
$this->redirect(array('controller'=>'contacts','action' => 'index'));
} else {
//here we are setting a flash message for user for error if input are not
//validated as expected
$this->Session->setFlash('sorry we could add your record','error');
}
}
}
For more information you can always refer to http://book.cakephp.org/2.0/en/models/callback-methods.html
I'm trying to pass a temporary data from my form to my model. My form has a hidden data in it, but it does not get any values from my model.
My form is
<?php echo $form->hiddenField($model, 'fieldName'); ?>
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
I threw in the data for my hidden field using JQuery, and then I proceeed to validate my form.
public function rules()
{
return array(
array('name, email, subject, phone, body', 'required','message' => '{attribute} Required'),
array('resume', 'file', 'types'=>'txt,pdf,doc,docx', 'maxSize'=>2097152, 'tooLarge'=>'File has to be smaller than 2MB','allowEmpty'=>false),
array('email', 'email'),
);
}
Whenever the validation hits an exception, it returns all of the $_POST data I inputted except the data I sent from the hidden form.
How do I get the $_POST so it gets back to the hidden form?
First:
The attribute must be declared as public in the model.
public $fieldName;
Add in rules() of Model the field with a safe mode array('fieldName','safe') :
public function rules()
{
return array(
array('name, email, subject, phone, body', 'required','message' => '{attribute} Required'),
array('resume', 'file', 'types'=>'txt,pdf,doc,docx', 'maxSize'=>2097152, 'tooLarge'=>'File has to be smaller than 2MB','allowEmpty'=>false),
array('email', 'email'),
array('fieldName','safe'), // Add the custom field to 'safe' mode.
);
}
Is 'fieldName' part of the model/table? If not and it is just a custom property declared in the model, then try setting it as 'safe'
I'm new to cakePHP and I've made a simple form following some tutorial. On this html form I've used validation. Now the problem is that the validation is working but the message is not displaying what I want it to display. I tried the code below.
Model
public $validate = array(
'title' => array(
'title_required' => array(
'rule' => 'notEmpty',
'message' => 'This is required field'
),
'title_unique' => array(
'rule' => 'isUnique',
'message' => 'This should be unique title'
)
)
);
Controller
public function add() {
if ($this->request->data) {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Post has been added successfully');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Error occured, Please try agan later!');
}
}
}
View
<h2>Add New Post</h2>
<?php
echo $this->Form->create('Post', array('action'=>'add'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Create Post');
?>
The validation error which I've seen is not the message I mentioned in my controller.
That's built-in browser validation.
Since 2.3 the HTML5 required attribute will also be added to the input based on validation rules.
Your title has the notEmpty rule, so Cake is outputting
<input type="text" required="required" ..
and your browser is triggering that message.
Edit: to override this behaviour, you can do:
$this->Form->input('title', array('required'=>false));
or
$this->Form->submit('Submit', array('formnovalidate' => true));
When you submit the form, your model validation will fire.
From your code what i can see is that you havent included helpers.
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
Just add to your controllers and try..
Your Form-create() options are invalid, first argument is the model-name, second is for options:
<h2>Add New Post</h2>
<?php
echo $this->Form->create('Post', array('action'=>'add'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Create Post');
?>
If the form-helper does not know which 'model' it is creating a form for, I won't check for field validation in the right place, hence, it won't output the validation errors for 'title'
[update] solution above didn't solve the problem. OP has modified the question
Some ideas:
Be sure to enable 'debug' (App/Config/core.php set Configure::write('debug', 2); Otherwise CakePHP may be using a 'cached' version of your model.
If you've named your Model incorrectly, Cake may be automatically generating a model for you, in which case your own model is never actually used, try this for debugging to see if we even 'get' to your model:
Add this to your model;
public function beforeValidate($options = array())
{
debug($this->data); exit();
}
I am only trying to create a non-AJAX registration form.
When I submit through a CForm, PHP says error() is being called on a non-object. I checked the source file where the error occurred and (using var_dump(get_class($parent->getActiveFormWidget()));) found that getActiveFormWidget() returns a CFormInputElement, which does not have error() defined.
I thought this had to do with CForm::activeForm, but it defaulted to CActiveForm.
I did try 'enableAjaxValidation'=>false.
What am I not understanding? I suspect I misinterpreted something along the way.
CFormInputElement::renderError():
public function renderError()
{
$parent=$this->getParent();
// $parent->getActiveFormWidget() returns object that does not define error()
return $parent->getActiveFormWidget()->error($parent->getModel(), $this->name, $this->errorOptions, $this->enableAjaxValidation, $this->enableClientValidation);
}
Model:
class RegisterFormModel extends CFormModel {
public $email;
public $password;
public $password_confirm;
public function rules()
{
return array(
array('email, password, password_confirm', 'required'),
array('password','compare','compareAttribute'=>'password_confirm'),
array('password, password_confirm','length','min'=>'6','max'=>'20'),
array('email', 'email'),
array('email', 'length', 'max'=>256)
);
}
public function attributeLabels()
{
return array(
'email'=>'Email',
'password'=>'Password',
'password_confirm'=>'Confirm Password',
);
}
}
View:
<div class="form">
<?php echo $form; ?>
</div><!-- form -->
Controller Action:
class RegisterAction extends CAction
{
public function run()
{
$register_model = new RegisterFormModel;
$controller = $this->getController();
$form = new CForm(array(
'title'=>'Register',
'enableAjaxValidation'=>false,
'elements'=>array(
'email'=>array(
'type'=>'text',
'maxlength'=>256,
),
'password'=>array(
'type'=>'password',
'minlength'=>6,
'maxlength'=>32,
),
'password_confirm'=>array(
'type'=>'password',
'minlength'=>6,
'maxlength'=>32,
),
),
'buttons'=>array(
'register'=>array(
'type'=>'submit',
'label'=>'Register',
),
),
), $register_model);
if($form->submitted('register', true) && $form->validate())
{
// ...
}
else
{
$controller->render('register', array('model'=>$register_model, 'form'=>$form));
}
}
}
Well, I have never seen using CForm as you show us.
I recommend you to use the active form widget,
http://www.yiiframework.com/wiki/195/implementing-a-registration-process-using-the-yii-user-management-module/
and you need to define all those fields in your CFormModel. This way you will be able to provide proper validation for them.
I know that the answer is really late :)
But for the sake of anyone else who may have a similar error.
<?php
// change from: echo $form;
echo $form->render();
?>
I was rendering the elements separately so this is how I did it:
<?php
// without the renderBegin() and renderEnd() it may give the no object error
echo $form->renderBegin();
echo $form->renderElements();
echo $form->renderEnd();
?>