vue vf-form can't get ajax data - ajax

I have a problem with ajax from vue vf-form.
I have wizard in this project as registration process.
So every step I'have a different view.
Code:
<wizard-hidden-step
title="About your company"
v-ref:head2
:valid="valid.step2"
:locked="isLocked"
:loading="isSending"
>
#include('auth.steps.step2', ['action' => "/company/signup/step2"])
</wizard-hidden-step>
{{-- ====== STEP 3 - ADDITIONAL INFO ========= --}}
<wizard-hidden-step
title="Additional information"
description="Add company additional info"
v-ref:head3
:valid="valid.step3"
:locked="isLocked"
:loading="isSending"
>
#include('auth.steps.step3', ['action' => '/company/signup/step3'])
</wizard-hidden-step>
And on all view I have a similar data but I will insert the code where i can't get ajax data:
<vf-form ajax
action="{{ $action }}"
method="POST"
:validation="validation"
v-ref:step2
>
PHP Laravel Controller:
return json_encode([ 'shared' => [ 'status' => 'ok', 'postcodes' => $postcodes ] ]);
After that I have ajax data in console (see on picture) but I can't set that data on view in vf-select:
<vf-select
:items="shared.postcodes"
multiple
select2
err-msg="Postcodes"
name="postcodes"
:html="true"
:value=""
v-ref:postcodes
:options="{showDropdowns: true, width: '100%'}"
>
</vf-select>
I have just No results found in vf-select.
Please help me, I don't have idea what to do.

I've found solution.
I was send shared object from backend and when I edit that in share, and on frontend read that as a shared this was work. I don't know how but it work...

Related

Laravel 5: How to use redirect()->withInput() with forms that contain prepopulated values?

I'm learning Laravel 5. I'm trying to build a simple form to edit an existing record from the database, and I'm confused about how to use withInput() and old() in this case.
For example, consider the following routes and controllers:
<?php
// routes.php
use App\Book;
Route::get( '/book', [ 'as' => 'book.edit', function() {
$book = Book::find(1);
return view( 'book', [
'title' => $book->title
] );
} ] );
Route::post( '/book', [ 'as' => 'book.update', function() {
$validator = Validator::make( Request::all(), [
// This validator will (almost) always fail. This is deliberate, to
// illustrate the problem I'm describing.
'title' => 'in:zzzzz|required'
] );
if ( $validator->fails() ) {
return redirect( route('book.edit') )
->withInput()
->withErrors( $validator );
}
// TODO: save book
} ] );
Here's the corresponding template:
<!-- book.blade.php -->
<form action="{{ route('book.update') }}" method="post">
<label>Book Title:</label>
<input type="text" name="title" value="{{ $title }}">
#if ($errors->has('title'))
<strong>{{ $errors->first('title') }}</strong>
#endif
<button type="submit">Save</button>
{{ csrf_field() }}
</form>
Now, consider the following user flow:
The user requests GET /book (we'll call this page load #1). The book.edit controller loads the book's title (which happens to be "50 Shades of Grey") from the database, and passes that string off to the view. The user sees the form with the value "50 Shades of Grey" prepopulated in the field. So far, so good.
The user modifies that title – say, to "A Tale of Two Cities" – and submits the form.
The book.update route processes this request, and finds it invalid. It redirects the user back to the book.edit route (we'll call this page load #2).
book.edit re-renders the same view, once again setting the value of title to "50 Shades of Grey" (which is incorrect).
This is the part I'm trying to solve: how can I display the title from the database on page load #1, but redisplay the user's input on page load #2?
Now, I understand why this code behaves this way. I just don't understand how I'm supposed to be doing it.
Possible solutions (and why I don't like them):
In my template, I could use value="{{ old('title') }}" instead of value="{{ $title }}". This will solve the problem on page load #2 – but it means that the title field will be blank on page load #1.
I could add some logic to the view – something like: {{ if $wasRedirected ? old('title') : $title }} This just feels hackish and wrong – it doesn't seem like the sort of thing that should be in the view.
I could add some logic to the book.edit request – something like 'title' => $wasRedirected ? Request::get('title') : $book->title. This also feels hackish and wrong.
I'm probably just misunderstanding something, or missing something obvious. How is this aspect of edit forms usually handled in Laravel 5? There's gotta be a standard pattern, right?
The signature for the old() helper is:
function old($key = null, $default = null)
You can pass the value from the database as the default.
old('title', $book->title)

cakephp ajax remote function select input parameter

I try to do two inputs in cakePHP (category, subcategory).
And if i change category input i want ajax to load values to subcategories.
What i can do it?
Im using remote function like this:
$ajax->remoteFunction(
array(
'url' => array( 'controller' => 'categories', 'action' => 'loadSubcategories', AND NOW I WANT PUT HERE CATEGORY ID FROM MY INPUT ),
'update' => 'subcategories'
)
);
<select name="categories" id="categories" onhange="MY REMOTE FUNCTION">
CATEGORIES
</select>
<select name="subcategories" id="categories" onhange="MY REMOTE FUNCTION">
LOAD SUBCATEGORIES BY CATEGORY ID WITH AJAX
</select>
I hope you can understand me :)
All you need is:
a Cake action that outputs a list of items based on the data it receives (shouldn't be hard)
a bit of JS that figures out which categories are selected (which you already have)
another bit of JS that packages that data and sends it to the Cake action
another bit of JS that updates the site with the list of items you received back
I'd take a look at jQuery's AJAX functions to accomplish this task. If you POST the data in a format like this, it's very easily accessible in $this->data in Cake:
{
'data[ModelName][categories]' : $("#category").val()
}

cakephp ajax not working properly

I am new in cakephp and I want to implement Ajax on my home page.
I have three modules in my page (client, developer and project). I want to add an ajax link. It's working perfectly only in the index page.
My code:
<h2>Projects</h2>
<div class="clear"></div>
<ul>
<li title="Project List">
<?php echo $ajax->link('Projects List', array("controller" => "projects", "action" => "index"), array( 'update' => 'main_page' ));?>
</li><br />
<li title="Add New Project">
<?php echo $ajax->link('Add New Project', array("controller" => "projects", "action" => "add"), array( 'update' => 'main_page' ));?>
</li>
</ul>
Now, my first issue is that in the add form, the validation with js is not working.
Second is: if I use cakephp inbuilt validation then it validates my form but redirects the page to "admin/projects/add" if no data is inserted.
Third problem is that when the above case happens and I want to redirect to listing page through my ajax link, at that time its also not working.
The best thing to do is stop using the ajax helper. Its been depreciated and will not be available in the 3.x branch.
This was done because it was not a good idea to start with, very limiting and buggy.
ajax with something like jQuery is not very difficult and you should rather look into using that or a similar tool.

cakephp ajax pagination works only once - scripts are not evaluatedd?

I followed tutorial from cakephp site but pagination with ajax works only once - the content is updated and its ok. But for the second time I click some page-link the whole page is reloaded - I think that click() event handlers are not binded again when content is refreshed by ajax - I don't know why... I am using this:
$this->Paginator->options(array(
'update' => '#content',
'evalScripts' => true
));
When I load page in the source code there is:
« Previous
$(document).ready(function (){
$("#link-925538478").bind("click", function (event) {
$.ajax({dataType:"html", success:function (data, textStatus){
$("#content").html(data);}, url:"\/final\/books\/index\/page:10\/sort:id\/direction:desc"});
return false;});
...
When I click for example next page (for the first time), everything is refreshed (the link hrefs also so it works) but the scripts are not reloaded so no click events are binded I think and clicking next page again just uses the link.
This is strange because I added this just after the pagination links:
<script>
$(document).ready(function (){
alert('yes');
});
</script>
And the alert is shown after first ajax refresh...
And I set up this thing ofc. <?php echo $this->Js->writeBuffer(); ?> at the end...
-------------------edit
I recognised that its not the paginator - for the following 2 links:
<?php echo $this->Js->link('link1', array('author' => 'abc'), array('update' => '#content')); ?>
<?php echo $this->Js->link('link2', array('author' => '123'), array('update' => '#content')); ?>
Its the same - when I click link1 its ajax, then when I click link2 there is normal reload - so it's somthing with script evaluation after ajax refresh... What that might be?
I am setting up JSHelper like this:
var $helpers = array('Js' => array('Jquery'));
I figured it out!!!
It's because ajax request sets the layout to app/View/Layouts/ajax.ctp and ajax.ctp is:
<?php echo $this->fetch('content'); ?>
I had to add this line
<?php echo $this->Js->writeBuffer(); ?>
To ajax.ctp to write java scripts (so the ajax links work after ajax request).
And now pagination works perfect!!!
Cake php Ajax Paginator not seems to be working fine. I had similar issues also.
I would recommend you to use the cakephp plugin Cakephp-DataTable
This plugin has implemented the pagination and it has most of the features by default. They have also provided documentation and if you find any difficulty in implementing please go throught the issues section
Also the developer is very responsive and can get clarifications for that plugin if you have any.

$ajax->submit Does Not Go To Controller

I am using cakephp and pippoacl plugin and I simply cannot add a new role. What I modify in the plugin is to make the submit using ajax, something like this in my view (add.ctp):
<?php echo $ajax->submit(
'submit',
array(
'url' => array('controller' => 'roles', 'action' => 'add'),
'before' => 'beforeSubmitAdd();',
'complete' => 'completeSubmitAdd(request);'
)
);
?>
When the add.ctp gets loaded for the first time, I can print_r something from the controller. But the ajax submit above only executes the javascript on 'before' and 'complete'. I check on the firebug, the response is blank.
On my controller:
function add() {
print_r("start");
if (!empty($this->data)) {
print_r("add new role");
// save new role
}
}
I use ajax submit for user and I don't have any problem adding new user. Is there any idea where I should check? I have been comparing the user and role code for a week and I have asked a friend to look at my code, too, but we still cannot find what causes this.
Thanks in advance! :D
I am not so familiar with the Ajax helper, but I haven't using it from so long that I can't remember what is it doing :).
Back to the problem.
Did you check if the requested URL in the Ajax address is correct? This should work straightforward, but it's possible that the url to be invalid.
Are you using Security component (even just adding it on the var $components variable)?. This could lead to blank screen especially if you modifying the fields in the form somehow. Try to remove it and see if it's working without.
Finally I would say how I would do it with jQuery.
Following code should do the job:
$(document).ready(function(){
$('form').live('submit', function(){ //handles also dynamically loaded forms
var form = $(this).addClass('loading'); //indicate somehow that the form has been submitted
$('#content').load($(this).attr('action'), $(this).serialize(), function(){
form.removeClass('loading');
});
})
});
This will handle all submits in the forms of the system, but you can modify of course.

Resources