Yii Form add more element functionality - yii

I have created a Form in Yii, which takes user information, like Name, mobile, email and his location preference like city, zipcode which is multiple like with add more option.
currently i am using jQuery to give add more functionality, and at model i am using array validator. But i want Zipcode to be numeric and required, and City required.
I there any way to achive this in Yii.

Lets say that in your form the variables are named zip1, zip2 and city1, city2 and the form is named infoForm
public function rules()
{
return array(
/*other rules here*/
array('zip1,zip2,city1,city1', 'required', 'on' => 'initform'),
array('zip1,zip2', 'integerOnly', 'on' => 'initform'),
);
}
Check out the wiki

Related

How to make a field required for customers but not for administrators in prestashop?

I made a custom field in prestashop.
I would like to know if there is some way to make this field only required on the frontend and not for administrators.
this is my code in Customer class
array('type' => self::TYPE_STRING, 'required' => true, 'size' => 64),
Thanks in advance
Can't think of a quick way to make this possible exactly as you want it, so here are two suggestions:
1 - Remove the 'required' => true from the definition and create some kind of custom validation like if (Tools::getValue('type ') == "") { return false; } in an override of the front office AuthController (Warning: code is completely untested and should definitely be improved, also according to your particular PS version).
2 - Use frontend validation by giving the frontend form field the required attribute (you should do this even if oyu use 1)

KeystoneJS and naming?

I'm new to Keystone JS and NodeJS.
This is the Part I totally do not understand;
Example 'Post' as defined as 'Post', but there are no 'posts', but when I call/ search for Post, in example (and my practices), it was 'posts'.
Exp:
keystone.set('nav', {
posts: ['posts', 'post-categories'],
enquiries: 'enquiries',
users: 'users',
});
Similar 'PostCategory' => 'post-categories', 'Enquiry'=>'enquiries' etc.
But when I making new Routes=>View for my custom post type, I must use:
locals.data = {
food: []
};
At this, its 'food' not 'foods'.
Keystone automatically uses the plural form of your model names in the Admin Panel, instead of its singular name. It's still referred to by its singular name (Food, PostCategory, Enquiry, etc.) throughout your code, but the admin panel uses the plural forms if referring to multiple documents of a model.
When working with local, you can name the properties of that object anything you want. Doesn't have to be locals.data.food; it can be whatever you want.
Also, the plural form of food is food. So nothing will change when using the plural form of a Food model in your Admin Panel.

Yii: Unique e-mail address in rules validation not working

I built a form that displays a user's existing email, then asks for a new email and a confirmation email to assure that they've typed it in correctly. The field for the new email is 'new_email', the confirmation field is 'new_email_confirm'. The model already has the field 'email' where their existing email address is stored. The scenario used is 'change_email'. Every other rule works and is validating with errors in my model but this one...when I enter in the exact same email/confirm that's already taken in 'email' I don't get an error, it is allowed to save.
In rules, I have added the following:
array(
'new_email,new_email_confirm',
'required',
'message'=>'This is a required field',
'on'=>'change_email',
),
array(
'new_email',
'email',
'message'=>'Invalid email address',
'on'=>'change_email',
),
array(
'new_email',
'compare',
'compareAttribute'=>'new_email_confirm',
'message'=>"Emails don't match",
'on'=>'change_email',
),
array(
'new_email',
'unique',
'className'=>'User',
'attributeName'=>'email',
'on'=>'change_email',
),
What am I doing wrong?
I have two dirty suggestions to do this, since I am not able to find out what the exact problem is.
1- write a custom validation for uniqueness and remove the unique rule:
array('new_email','customValidationRule'),
and write it like below:
public function customValidationRule(){
//after all validation rules
if(!$this->hasErrors()){
$check=User::model()->findByAttributes(array('email'=>$this->new_email)); //or $this->new_email_confirm
if(!is_null($check) || !empty($check)){
$this->addError('new_email','The new email has been already taken by other!');
}
}
}
2- replace new_email with new_email_confirm in your unique rule.

Yii. Change URL based on model parameter value

I would like to use the same model "Work" for handling similar CRUD operations for "Services" and "Markets" pages. There is a column in the Work mysql table called "category". If category is "service", then I would like the Index to show list of Services that are stored in the Work table. Similarly for the "Markets" page.
I would need two urls for the same Model (in the menu and create/ update operations etc.). How can I set this up in the URL Manager?
'services/create/' => 'work/create?&category="services"',
You would do it like this:
array(
//put it first so it has highest priority
'services/create' => 'work/create/category/services',
//other rules follow
)
Then your function in your controller would look like this:
public function actionCreate($category) {
//your code
}

Custom Form With Parts Linked To exisitng Model (ActiveRecord)

i'm madding a form (Yii framework) that does not represent a database table but it contains a part "mapped" to a database table (like a belong_to relation).
So you can consider a contact form in which you can choose to whom send the email with a dropdown list that represent users in database.
And below you can type your email subject and content.
So here the contact form is not stored in the database, so it is not an ActiveRecord instance, but it contains "relation" like an ActiveRecord has.
My question is : How do i build my form class ?
I want to be able to do this in the view :
$activeFormWidget->dropdown($form->user, 'name', User::model()->getUsers());
You can use the CHtml::dropdownList to build dropdown list without a CActiveForm:
<?php
echo CHtml::dropDownList('user', // the input name
'', // initial selected value
CHtml::listData(User::model()->getUsers(), 'id', 'name'), // your data
array()); // htmlOptions
?>