I have a model called User and its $validate has 39 field validations and working fine in UsersController. In another controller I need to use the same code and validate only a single field from controller. Can Someonce please help me with this?
Relevant docs.
Use the fieldList option in the model's validates() method.
$this->ModelName->set($this->data);
if ($this->ModelName->validates(array('fieldList' => array('myField')))) {
// If myField validated successfully...
}
Related
I want to add my save, update, find queries in model and call those functions in controller, rather than adding the queries in controller.
I'm not sure what's you precisely want to acheive, but I first of all hope you know that CakePHP has implemented save, update and find functions for your models. If not, look at the docs, e.g. here: http://book.cakephp.org/2.0/en/models/saving-your-data.html
If you want to add functions with custom queries to your model, you can simply add them as a normal function in your model, e.g.:
MODEL
public function myMethod(){
// Fetch data
return $this->query("SELECT * FROM pictures LIMIT 2;");
}
CONTROLLER
public function index(){
// Get data from model-method
$data = $this->ModelName->myMethod();
// Send data to view
$this->set(compact('data'));
}
In your model classes you don't have to refer to your model in your code:
In your controller: $this->ModelName->function()
In your model: $this->function()
Has the same meaning.
I have this scenario where a field is invalid due to another selection on the form. When that selection changes I want to revalidate. I tried calling $setViewValue on the field when the selection changes, but that doesn't refire the validation. Any ideas?
I have a hack working, but I would prefer a clean solution.
I ran into the same issue and found a workaround/feature that appears to be undocumented. If you need to trigger ngModelController to revalidate, you can either do:
ngModelCtrl.$setViewValue(value, 'your event name', true);
or if you don't need to update your model value
ngModelCtrl.$commitViewValue(true);
The true in both cases above is a flag for revalidation. Without this flag, the issue I was running into was that if the model value does not change, then angular simply skips the validation. I am using this way to manually mark a custom control as $dirty
Source:
https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L1843
Write a directive, say validation, and place it on your field in the definition of this directive in the:
link:function(scope,element,args){
element.bind('onfocus',function(){
// Your logic
})
// Similarly, bind other relevant events like key presses, etc.
}
Put a ng-change on your select and broadcast an event in it:
$rootScope.$broadcast("selectChangedEvent");
Then, in a directive you have placed on your field, simply put in the link function:
$rootScope.$on("selectChangedEvent", () => ngModelCtrl.$validate());
$validate runs each of the registered validators of your field.
I have a shopping cart site and on successful purchase I need to send a mail with all product details in the order as attachment. I have used fat model skinny controller approach and all my functions are in model. I have a controller action which will give the order details by passing order id along with view. Using dompdf I can convert this html to pdf and can create a file. So for creating attachment I can use the same function by passing some parameter. My mail sending code is in model. From here I need to call the controller action and need to get the pdf file name that just created. I know calling controller action from model is against MVC architecture. But how can I achieve this functionality ?
'fat' models is a good thing to do, however, try not to put things in a Model that should not be in a Model. In MVC, Models should handle all things related to data.
Fat models
The 'fat' Model concept is to reduce the amount of code in your Controller, by moving data related code to the Model. for example:
In stead of this; (in your Controller):
public function view($id)
{
$this->request->data = $this->SomeModel->find('first', array(
'fields' => array(
// list of fields to retrieve
),
'conditions' => array(
// conditions
),
// etc.
);
}
Move the find instructions to a method inside your model and use this:
public function view($id)
{
$this->request->data = $this->SomeModel->someMethod($id);
}
Other locations to put your code
Code that is not related to data, can also be moved outside your Controller (to make it 'skinny'). CakePHP offers other locations to move your code to, for example inside a Component
Then inside your Controller;
this:
public function view($id)
{
$this->request->data = $this->SomeModel->someMethod($id);
// use functionality of a component
$this->SomeComponent->doSomething();
}
Triggering functionality via Events
To keep code and logic outside your controller, CakePHP 2.x now offers an 'Event' system. This allows you to execute code if a certain event happens. You can pass additional information through the events (the event will become a 'communication channel' that passes through your application).
Sending e-mails for certain events is a good example. The CakePHP also uses sending mails to illustrate the Event system in CakePHP; this is probably what you are looking for:
Dispatching Events - send emails
Make your model method return the data so you have it in the controller and pass it to the other model function together with your pdf related data.
I am unable to find how to filter the drown down which i got by using refmodel in my model which extends model_table
thanks in advance
For any view in toolkit you can use setController and getController. refModel fields from Model will set a proper controller for a form field. It uses type reference.
Once form in initialized, you can interact with controller of a field by
$field->getField('myfield')->getController()->addCondition('type','admin');
Alternatively the following might work too:
$field->getField('myfield')->dictionary()->addCondition('type','admin');
HI I am new to cakephp so any help would be grateful.
I have created a form and with one of the fields when the user has filled in checks to see it it already exists and offers other suggestions. I have used the Ajax observerField method to do this. I want the user to be able to click on the suggested names(radioboxes) and then it update the field in the other form. What is the best way to achieve this in cakephp?
If it was me, I would create a form field
echo $form->input('otherfield');
Then use javascript to catch the click on the suggested radio, and copy the value into that field.
In jQuery,
$('#suggestions input[type=radio]').click(function(){
$('#otherfield').val() = $(this).val();
});
Then when the form is submitted you will have it in,
$this->data['Model']['otherfield']