hi to all i am using cakephp framework for my project. in on of my form i take a checkbox on click on this two other text box are shown.
by using cakephp validates method i validate the form data but i want that when the checkbox is not checked then it avoid the validation for that text box. it check only when the checkbox is checked.
so plz help me.
thanks in advance
You can use your models beforeValidate servicecall for that and add extra validation criteria for this model.
For example:
function beforeValidate($options = array())
{
if(!empty($this->data['Model']['fieldOne']))
$this->validate['fieldTwo'] = array(/*normal validation rules*/);
return true; // Needed or validation fails
}
You can use custom validation methods:
var $validate = array(
'checkbox1' => 'checkboxRule'
);
// If checkbox1 is checked, requires checkbox2 to be checked as well
function checkboxRule() {
if (!empty($this->data[$this->alias]['checkbox1'])) {
return !empty($this->data[$this->alias]['checkbox2']);
}
return true;
}
You can also invalidate other fields, like checkbox2, at the same time by calling $this->invalidate('checkbox2') in your custom method.
Also, you can unset the validation in your controller like this:
unset($this->Model->validate);
Related
For CakePHP2, I used following function for resetting associations.
public function unbindModelAll($reset = true) {
foreach(array('hasOne','hasMany','belongsTo','hasAndBelongsToMany') as $relation){
$this->unbindModel(array($relation => array_keys($this->$relation)), $reset);
}
}
How can I reset them for CakePHP3?
For CakePHP 2.x you can easily clear bindings using empty contain on the model
$this->Model->contain();
For CakePHP 3.x you can try, I didn't test it:
$this->Model->find()->contain();
In Drupal 7, I have this code.
It defines the hard-coded string my_module_custom_route_form as the form ID and use drupal_get_form to initialize a form. And there is one submit button.
Visit this route /custom_route, the output is
initialize form
If you submit the form, the output is
initialize form
submitted
The problem is this: The submission of this form will call my_module_custom_route_form again.
How can I prevent it from being called again on submission? If you know why Drupal does this, I would also like to know.
function my_module_menu() {
$items = array();
$items['custom_route'] = array(
"page arguments" => array("my_module_custom_route_form"),
"page callback" => "drupal_get_form",
"access callback" => TRUE,
);
return $items;
}
function my_module_custom_route_form($form, &$form_state){
print_r("initialize form");
$form = array();
$form["button"] = array(
"#type" => "submit",
"#value" => "submit"
);
return $form;
}
function my_module_custom_route_form_submit($form, &$form_state){
print_r("<br />submitted");
exit;
}
I think what you need is this:
Set $form_state['redirect'] = false in the submit handler.
See https://drupal.stackexchange.com/questions/26347/prevent-redirect-after-form-submit
Hope that helps!
I know that this is an old question, but the question is still relevant - I ran into the same problem. In my case it is definitely a problem, since the form in the block claims a queue item for processing, but when the form builder is called a second time, of course the item is blocked in the queue.
Clicking the button on a non-ajaxified form causes the page to reload. In the case of the block, this calls the block_view function, which rebuilds the form from 0.
I solved this by ajaxifying the submit button. Then the submission is called without re-viewing the block (and rebuilding the form, as is the case here). There is a nice ajax_example in the examples module, for those who are new to ajax forms.
I'm a noob in CakePHP and I've been trying to do some complex validations here:
I have the following models:
- Fonts (name, file);
- Settings(value1,value2,value3,type_id,script_id);
- Types(name)
Whenever I create a Font I also create a default setting associated to it. Also, this setting has a type associated. After the Font is created I can associate more settings to it (Font hasMany Settings), but I need to make sure that two settings of the same type are not added to that font. I don't know how to handle this case. Any help is appreciated. Thanks.
I'd use a simple beforeSave validation
//in setting.php model
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['font_id']) && isset($this->data[$this->alias]['type_id']) {
$otherSettings = $this->find('all', array('conditions'=>
array('type_id'=>$this->data[$this->alias]['type_id'],
'font_id'=>$this->data[$this->alias]['font_id']);
//check if it's insert or update
$updated_id = null;
if ($this->id)
$updated_id = $this->id;
if (isset($this->data[$this->alias][$this->primaryKey]))
$updated_id = $this->data[$this->alias][$this->primaryKey];
if (count($otherSettings) > 0) {
if ($updated_id == null)
return false; //it's not an update and we found other records, so fail
foreach ($otherSettings as $similarSetting)
if ($updated_id != $similarSetting['Setting']['id'])
return false; //found a similar record with other id, fail
}
}
return true; //don't forget this, it won't save otherwise
}
That will prevent inserting new settings to the same font with the same type. Have in mind that this validation will return false if the validation is incorrect, but you have to handle how you want to alert the user of the error. You can throw exceptions from the beforeSave and catch them in the controller to display a flash message to the user. Or you could just not save those settings and let the user figure it out (bad practice).
You could also create a similar function in the model like checkPreviousSettings with a similar logic as the one I wrote above, to check if the settings about to be saved are valid, if not display a message to the user before attempting a save.
The option I prefer is the exception handling error, in that case you'd have to replace the return false with
throw new Exception('Setting of the same type already associated to the font');
and catch it in the controller.
Actually, the better approach is to not even display the settings with the same type and font to the user, so he doesn't even have the option of choosing. But this behind-the-scenes validation would also be needed.
Was checking out this tutorial "http://book.cakephp.org/2.0/en/models/data-validation.html" only seem to explain validating on form fields.
So what If I want to validate params or input data.
If I have this controller, where I want to validate a this "campaignId"?:
public function agency($campaignId = null){
if ($this->request->is('get')){
$campaignId;
...
}
CakePHP Model Data Validation is validation of the data, regardless of where it comes from.
If you create an array of data, eg:
$article = array(
'title' => 'Pizza is the Best Food on Earth',
'byline' => 'Dave from StackOverflow',
'content' => 'Pizza is awesome. I love it.'
);
Then try to save it:
$this->Article->save($article);
The Model's validation will kick in and validate (or invalidate) the data.
The most common way data is entered is via a form, but - beyond that, validation is not directly tied to using a form(s).
I'd like to change the text of the default '-Any-' that Drupal 7 views uses for an exposed dropdown filter.
Based on an answer in this thread,
How to change the label of the default value (-Any-) of an exposed filter in Drupal Views?
I have created a module called any_exposed with a hook form alter:
function any_exposed_form_alter(&$form, &$form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-vendors-page') {
$form['field_vendor_type_tid']['#options']['ALL'] = t('Everything'); } }
But all that does is add another option for 'Everything' in the dropdown, it does not overwrite/translate '-Any-'. Just to test I added:
$form['submit']['#value'] = t('Search');
Which changes the text of the Submit button from 'Apply' to 'Search', and this works fine. In case you can't tell, I'm not much of a programmer, but I figure I must be missing something simple. Any help would be appreciated!
This is an old post but in case you are still looking or for anybody who comes to this searching for an answer. 'ALL' must be 'All' as in
$form['field_vendor_type_tid']['#options']['All'] = t('Everything');
Since the array has a member 'All' but not 'ALL' (case is important here) you are adding a member 'ALL' while you want to overwrite 'All'.
Use hook_form_views_exposed_form_alter instead hook_form_alter.
function hook_form_views_exposed_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$form['tid']['#options']['All'] = t('Search');
}
}
function any_exposed_form_alter(&$form, &$form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-vendors-page') {
$form['field_vendor_type_tid']['#options']['ALL'] = t('Everything'); } }
Works perfectly after changing ALL to All.
I recommend using Better Exposed Filters module, it allows you to do this simply via the Views UI interface.
Install & enable the module
Edit your view, then click on 'Exposed form > Exposed form style'
Select 'Better Exposed Filters'
Click 'More options'
Change the value of 'Override "Any" option label'