Accessing Different Controller Method from A View with Ajax->form() - ajax

In my conversations view, I'm trying to make it so I can add messages to a conversation.
Currently I have a Conversation hasMany Messages relationship.
Now when I try to invoke the following code:
<?=$ajax->form('message','post',array('update'=>'messages')); ?>
It produces a form with a form action to
action="facebook/conversations/messages/add"
So I get an error saying I don't have the a controller function labeled "messages" in my conversations controller.
I want the action to go to my messages controller instead.
I'm sure its some really silly code I have to implement, but I'd really appreciate your help.

You can pass a url explicitly when you create a form.
echo $ajax->form('message', 'post', array('url'=>$html->url(array('controller'=>'messages', 'action'=>'action_name'))));

From the CakePHP Book you can also use a slightly different variant of the Ajax form function and avoid using the Html Helper to build the url.
$this->Ajax->form( array(
'type' => 'post',
'options' => array(
'url' => array(
'controller' => 'messages',
'action' => 'action_name'
)
)
));

Related

How to implement email validation on admin form in Magento2

I want to use email validation in admin form of my custom module. I've seen core module but couldn't get exact idea.
You can also validate the 'email' text field by adding a class ['validate-email'] in your form
$fieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email'),
'title' => __('Email'),
'required' => true,
'class' => 'validate-email'
]
);
Validation is Model's issue. Only model knows how your data should look like. You describe your data fields in model, so you should describe validation rules for this fields in the same place.
It seems to be obvious for me, but I'd gladly listen to opponents.
put database validation in the Model (assuming it's a db model) and http data validation in the controller. Xss filtering, for example, does not pertain to the Model. it pertains to the Controller in input and to the View in output
I found an answer by myself, I used PHP email validation in the save controller.

How to display two dynamic names within one url using laravel

The following code:
{{url('/'.$subjecttype->name)}}
is the name 'garden' wrapped up in a url. This gives me localhost/garden with obviously garden as the dynamic name. With my routes setup like so:
Route::get('/{subject}/', array( 'as' => 'subject', 'uses' =>
'SubjectController#getsubject'));
The question is how would I setup two dynamic names within one route? For example
localhost/garden/12
so i would want my route to look something like this
Route::get('/{subject}/{id}/', array( 'as' => 'subjectid', 'uses' => 'SubjectController#getsubjectid'));
but more importantly what would it look like in my view? so that I have the of my garden header wrapped up in a url that looks like this:
'gardening tips for beginners' which is {{$subjecttype->title}}
below is my very poor attempt at what i want but i hope you get the picture.
{{url('/$subjecttype->name/$subjecttype->id/'.$subjecttype->title)}}
Thanks
For your route:
Route::get(
'/{subject}/{id}/',
array(
'as' => 'subjectid',
'uses' => 'SubjectController#getsubjectid'
)
);
you can generate the URL with the following code:
$url = URL::route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
or, if you prefer to use the helper functions:
$url = route(
'subjectid',
array(
'subject' => $subjecttype->name,
'id' => $subjecttype->id
)
);
That's going to give you a URL like http://example.com/subjectname/42. If you want to add another parameter like the title at the end of the URL, you'll need to add another parameter to your route definition. If you don't you're going to get 404 errors because no route will match the URL.
For the second part of my question using the 'gardening tips for beginners' example:
gardening tips for beginners

How to pass validation errors to view without redirect in Laravel

How can i pass validation errors directly to a View without a redirect in Laravel?
I don't want to do that:
return Redirect::back()->withErrors($validator)->withInput();
but rather:
Input::flash(); // for repopulating fields
Validator::flash(); // this doesn't exist
return View::make('fragments/login_ajax');
Or another more concise direct generation of a view without a redirect. Optimal would be something like that, but that doesn't work:
return View::make('fragments/login_ajax')->withErrors($validator)->withInput();
The reason is, that this is the answer to an ajax based login. Redirecting to another method only used to display the same view deems unneccessary.
Cheers
Though agreeing it is a bad idea to do this, you can use View::share() to share the errors to all the views.
\View::share('errors', $errors);
Use below solution.
Controller:
$validator = Validator::make($request->all(),
[
'class_id' => 'required|numeric|not_in:0',
'batch_id' => 'required|numeric|not_in:0',
'student_id' => 'required|numeric|not_in:0'
]);
$this->viewData['errors'] = $validator->errors();
View File:
{!! Form::label('class_id','Student Class') !!}
{!! Form::select('class_id', array(), 0, ['class' => 'form-control', 'id' =>
'class_id']) !!}{!! $errors->first('class_id', '<p class="help-block">:message</p>')!!}
Real answer:
this is not an issue that is worth spending time on. It is an architecture problem.
Possible Solution to your situation (not recommended):
Make auth a new route and have it return json of errors/messages. Then have JS deal with the errors / messages / logic.

CakePHP Observe field for validation check(already exists)

i have already user observe field in cakephp 1.3 Now i want to do the same thing in cakephp 2.0
I have one form.there is a one field named email. When user insert email. onblur there is a popup one message that displays "email already exists" or "right"
for that i have write below code in my projects. the below code is my add file code
<?php $options = array('url' => array( 'controller' => 'users', 'action' => 'is_exist'), 'update' => 'email_exist');
echo $this->ajax->observeField('UserEmailId',array('url' => array( 'controller' => 'users', 'action' => 'is_exist'), 'update' => 'email_exists')); ?>
In my controller i have created one function like
public function is_exist($id = null)
{
$result = "yes";
$this->set('existdata',$result);
}
i have also created is_exists.ctp fiel in view/uers.
i dont know why its not working.
i did the same thing in Cakephp 1.3 and its working file but not in cakephp 2.0
can anyone tell me how i implement this ?
thanks in advance
because ajax helper is not supported in cakephp 2.
you need to learn how to implement it using the js helper
check this link : js helper

CakePHP ajax form return display continuously current page w/ missing controller

I am running an ajax submit in cakephp. It appears to be running, but the end result for the population of the assigned div is the page itself as opposed to the results set. And it also says the controller is missing which obviously is not true.
Model: Plan
Action: search()
search form element (being pulled into the search.ctp):
...
echo $ajax->form(array('type' => 'post',
'options' => array(
'update' => 'plansQueryResults',
'url' => array('controller' => 'plan', 'action' => 'search'),
'loading' => "Element.show('plsLoaderID')",
'loaded' => "Element.hide('plsLoaderID')"
)
));
...
echo $form->end();
This div is pulling in the default layout, but
<div id="plansQueryResults"></div>
Standard error (wrapped around default layout) displaying within the div:
Missing Controller
Error: PlanController could not be found.
Error: Create the class PlanController below in file: dental/1/app/controllers/plan_controller.php
<?php
class PlanController extends AppController {
var $name = 'Plan';
}
?>
What is the code you have on the search action?
What is the returned data from the action? Check with Firebug.
By the way... when following the standards to create a DB, it is better and handy use cake bake: Code Generation with Bake

Resources