I have a form in magento admin panel. In the form i have checkboxes which i can select multiple options or one. The issue is i am unable to put validations for that. Because without selecting any option i can save records. My code is as in below:
$fieldset-> addField('time_ranges', 'checkboxes', array(
'label' => Mage::helper('CheckoutTime')->__('Time Ranges'),
'required' => true,
'class' => 'required-entry',
'name' => 'time_ranges[]',
'values' => array(
array(
'label' => Mage::helper('CheckoutTime')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('CheckoutTime')->__('Investment'),
)
),
));
Can anyone please tell me how to add validations into this form.
Thank You
Try by changing
'name' => 'time_ranges[]'
to
'name' => 'time_ranges'
This way is correct. There were some issues in other places in my coding. That's why it didn't work early. Or else this is the correct way to do that.
Related
I would like to remove the empty or first option of list data value. I have FruitList model and it has a list, so I need to prevent from users to select all.
But now the problem is the empty option that can let user to select all Fruits, so how can I remove.
This is my code
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'header' => 'Buyer',
'name' => 'Buyer',
'value' => 'customer_name',
'filter' => $fruits
),
array(
'header' => 'Fruits',
'name' => 'fruit_id',
'value' => '$data->Buyers->FruitList->Name',
'filter' => $fruits
),
array(
'class'=>'CButtonColumn',
),
),
));
By default filters for CGridView renders dropdown with empty option to allow disabling filtering. But you can overwriting this behavior by providing your own dropdown as a filter:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'header' => 'Buyer',
'name' => 'Buyer',
'value' => 'customer_name',
'filter' => CHtml::activeDropDownList($model, 'customer_name', $fruits),
),
array(
'header' => 'Fruits',
'name' => 'fruit_id',
'value' => '$data->Buyers->FruitList->Name',
'filter' => CHtml::activeDropDownList($model, 'fruit_id', $fruits)
),
array(
'class'=>'CButtonColumn',
),
),
));
Make sure that you set default value for these filters in your model - something like this in your controller:
// ...
$model->fruit_id = FruitList::DEFAULT_ID;
$model->customer_name = FruitList::DEFAULT_ID;
if (isset($_GET['FruitList'])) {
$model->setAttributes($_GET['FruitList']);
}
$dataProvider = $model->search();
// ...
you can set condition in the dataProvider so its return you a result of all not null value.for example
$dataProvider->criteria->addCondition('fruit_id IS NOT NULL ');
I hope its work!
i am new to cakephp and am trying to build an app in it. I have some textfields, i wish to have validation on them. I following cakephp tutorials in cakephp.org and did the following but i cant see the validation messages near the textfields. Following is my code:
ctp:
<?php echo $this->Form->text('Rideoffer.PickFrom',
array('class' => 'address-text',
'value' => $dropFrom)); ?>
model:
public $validate = array(
'PickFrom' => array(
'rule' => 'notEmpty',
'message' => 'Cannot leave this field blank.'
),
//'PickFrom' => 'notEmpty',
'DropAt' => 'notEmpty',
// 'born' => 'date'
);
where am i getting wrong? how do i solve it?
You have used comma in dropat
public $validate = array(
'PickFrom' => array(
'rule' => 'notEmpty',
'message' => 'Cannot leave this field blank.'
),
//'PickFrom' => 'notEmpty',
'DropAt' => 'notEmpty'
// 'born' => 'date'
);
You can use this
Form->text('Rideoffer.PickFrom',
array('class' => 'address-text',
'value' => $dropFrom,
'error'=>'Cannot leave this field blank.')); ?>
I have a model ex. posts that acts as translate and I want to validate the title which is a translated field.
How is this possible. The below does not work.
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
),
));
Change rule name to camelCase: notEmpty and add message
you translate your message in /Locale default.po
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'your translated message',
),
));
whats the big deal?
I've just started a little page using CodeIgniter and wanted to run CodeIgniter's form validation magic tricks. For this, I've set some rules via config/form_validation.php:
$config = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|max_length[64]'
)
);
But in addition to that, I wanted to set some specific rules inside the controller itself.
$this->form_validation->set_rules('name', 'Name', ' is_unique[table.name]');
My problem - the specific set_rules() seems to have reset all previously defined rules.
Is there a way to merge both set of rules? Or did I miss a method for that?
I've had this exact issue before - where I wanted to use one set of rules, but add one extra rule for a specific controller.
Unfortunately you are correct - and the form_validation will overwrite the old rules. You cant even call the variable containing the old rules from the config - because its not stored in an accessible format.
The way I did the workout was to define the rules in a generic config file as arrays - and load the arrays inside the controller, then append a new rule, then set the whole array as the ruleset.
The other option is to just define two different rulesets inside the config file (even though they might be almost identical) - and just call the different rulesets as required.
It is better to defined named array in the config file for each controller and use it as mentioned in the Codeginiter user guide.
$config = array(
'signup' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'PasswordConfirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
),
'email' => array(
array(
'field' => 'emailaddress',
'label' => 'EmailAddress',
'rules' => 'required|valid_email'
),
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'required|alpha'
),
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required'
),
array(
'field' => 'message',
'label' => 'MessageBody',
'rules' => 'required'
)
)
);
Call it like $this->form_validation->run('signup') with the name of the array.
I can't say I have a lot of experience with CI however, as far as I know you should be able to append additional rules. Failing that consider trying:
$config[] = array('name', 'Name', ' is_unique[erfolge.name]');
$this->form_validation->set_rules($config);
or use array_merge if you don't want to modify your standard configuration.
How validate file upload in CakePHP?
When i wrote some like this:var $validate = array(
'file' => array(
'select' => array(
'rule' => array('selectFile'),
'message' => 'There is no file!'),
'type' => array(
'rule' => array('typeFile'),
'message' => 'Bad type!'),
'size' => array(
'rule' => array('sizeFile'),
'message' => 'Bad with size!')));
Works only with the last validate, here 'size'.
Maybe You know solution, for validation for files with many messages?
try adding required=true to each of the 3 rules