Zend Framework 2 form - zend-framework2

I have a dilemma.
I would like to in my form if the data that are bind (object) added to the form, depending on whether the image exists or not, the item is shown on the form or not.
I solved it like this, but I do not know if this is correct.
$id = (int) $this->params()->fromRoute('id', 0);
$coupon = $this->getEntityManager()->find('Application\Entity\Coupon', $id);
$forms = $this->getServiceLocator()->get('FormElementManager');
$form = $forms->get('CouponForm');
$form->bind($coupon);
$form->setBindOnValidate(false);
$form->get('dateStart')->setValue($coupon->getDateStart()->format('Y-m-d'));
$form->get('dateEnd')->setValue($coupon->getDateEnd()->format('Y-m-d'));
if($coupon->getImageUrl()) {
$form->get('image')->setAttribute('src', $coupon->getImageUrl());
}else {
$form->remove('image');
}
Can it be nicer to solve?

In the event you're looking to change the display of the form itself, the logic for rendering/not rendering the coupon should most likely live in a View Helper.
This keeps the rendering free from the controller and maintains a nice separation of concerns.

Related

Defining a kendo grid by it's model

For the functionality I want to add I'm using a couple of different grids on the same page. All these grids should be able to affect what code is executed by the buttons 'delete', 'edit' and 'create'.
In order to seperate the grids I could use their name, but this is rather problematic with the subgrids. The code I'm currently using is as follows:
function deleteButton()
switch(selectedGrid) {
case $("#GridA").data("kendoGrid"):
log("A"); break;
case $("#GridB").data("kendoGrid"):
log("B"); break;
}
selectedGrid has a grid stored that should determine which function to execute.
From this grid I would prefer to extract the model (name) and use that as the switchcase variable. The result would look something like this:
function deleteButton()
switch(selectedGrid.Model) {
case 'modelA':
log("A"); break;
case 'modelB':
log("B"); break;
}
Currently I use logs instead of functions, but the idea is the same. The reason I chose this approach is because multiple grids are using the same model and should call the same case.
Code in the kendo grid for calling the function to set the selectedGrid variable:
.Event(event => event.Change("function(){onRowSelectGrid('grid', 'model');}"))
Code in page js for setting the selectedGrid variable:
function onRowSelectGrid(datagrid, model) {
if (typeof (selectedGrid) !== 'undefined' && selectedGrid !== $("#" + datagrid).data("kendoGrid")) {
//Remove rowselection from the previous grid
selectedGrid.select().removeClass("k-state-selected");
}
var grid = $("#" + datagrid).data("kendoGrid");
selectedGrid = grid;
}
I understand that this is not your every day situation, so if the only solution would be to have seperate buttons for every grid, I would understand that. However, personally I would prefer to have dynamic code that can be used by all grids.
If there are any questions or more explanation is needed, please let me know. If there is a way to do this where the name (including the name of the subgrids) is somehow used, I would definitely want to take a look at such solution.
Oke so this may not be the best solution, but it works. What I did was add a bit to the code saving the selectedgrid.
selectedGrid = grid;
By adding a "model" to the variable to indicate the model of the grid:
selectedGrid.model= model;
This way, when calling the grid from the button, I can call the model variable to indicate what method should be used.
function deleteButton()
switch(selectedGrid.model) {
case 'modelA':
log("A"); break;
case 'modelB':
log("B"); break;
}
Hope this helps anybody running into the same issue.

What is the best way to disable elements in a form if it is signed?

I have a logic when users might be presented with a form values, but should be unable to change it if it is "signed". I am wondering what is the best way to do it from controller. Right now, all elements go through the loop and get "disabled" attribute if signature exist. I wonder if I can apply more elegant way on form creation.
This is what I have (and the "disabled" is not assigned to fieldsets this way):
EDIT 1 - added forms creation
$form = $sl->get('FormElementManager')->get($formName);
$hydrator = new DoctrineHydrator($objectManager);
$form->setHydrator($hydrator);
if($isSigned)
{
$formElements = $form->getIterator();
foreach($formElements as $element )
{
$element->setAttribute('disabled', 'disabled');
}
}

I am unable to access a variable used in my select tag from my ModalInstance controller

I have taken the codes shared from the Modal example page and instead of an LI I have decided to use a select element. My select element has ng-model="selectedColor" in it, and I can use {{selectedColor}} all over the partial I created, however, I can not use "$scope.selectedColor" from the "Model Instance Controller" or any controller for that matter. I assume this is because something is off with $scope but I cant seem to figure it out. Any help is appreciated.
http://plnkr.co/edit/MsNBglLJN0hWxvGZ1pj1?p=preview
The problem in your code is that $scope.selectedColor and the selectedColor in the modal markup are two different references. For details on this, please read Understanding Scopes, you will probably benefit from it as it is a common task.
Instead of writing $scope.selectedColor, you should make an object in your controller, then store the result in it.
var ModalInstanceCtrl = function ($scope, $modalInstance, colors) {
$scope.colors = colors;
$scope.o = {}
$scope.ok = function () {
console.log($scope.o.selectedColor, "$scope.o.selectedColor");
$modalInstance.close($scope.o.selectedColor);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
and in the markup, refer to o.selectedColor.
Here is a working version of your Plunker

Knockout js - Dirty Flag issue

I am using Knockout Js for my view page. I have a requirement where if any editable field changes, I have to enable Save button else not. This is working nicely.
My issue is I have checkboxes too for each row of item. These are observable items in my viewModel. What happens now is when I check or uncheck any checkbox, Knockout considers that as Dirty item and enables the Save button which I don't want.
How can I tackle this?
I am not sure of the exact code that you are using for a dirty flag, but if it involves using ko.toJS in a dependentObservable like this, then there is a trick that you can use to have it skip some observables.
If you create an observable that is a property of a function, then ko.toJS will not find it.
Here are two examples (someFlag and anotherFlag):
function Item(id, name) {
this.id = ko.observable(id);
//create a sub-observable that the dirty flag won't find
this.id.someFlag = ko.observable(false);
this.name = ko.observable(name);
this.dirtyFlag = new ko.dirtyFlag(this);
//or similarly, place an observable on a plain ol' function
this.forgetAboutMe = function() { };
this.forgetAboutMe.anotherFlag = ko.observable(false);
}
Sample here: http://jsfiddle.net/rniemeyer/vGU88/

DevExpress DateEdit using MVC

I have just started using the
<% Html.DevExpress().DateEdit()
control and i got it to work fine in my ASP.Net MVC application. The code is as shown below:
aspx page:
<% Html.DevExpress().DateEdit(settings =>
{
settings.Name = "EndDate";
settings.Properties.NullText = "dd/MM/yyyy";
settings.Properties.EditFormat = EditFormat.Custom;
settings.Properties.EditFormatString = "dd/MM/yyyy";
settings.Properties.DisplayFormatString = "dd/MM/yyyy";
settings.Date = Model.EndDate;
settings.Width = 100;
}
).Render();
%>
Above this code i have a reference to my javascript file (DateChanges.js) in this file i want to be able to do something like:
$(document).ready(function(){
$("#EndDate").change(function(){
//do whatever i want
});
})
I cant do this now cause using firefox i can see that the actual textbox that this datepicker assigns a value to has be named "EndDate_I". So my question is how can i easily do this since i want to be able to catch the change event of this control and play around with it in jQuery??
The DevExpress MVC Extensions offer their own infrastructure for the client-side processing needs (see the http://help.devexpress.com/#AspNet/CustomDocument6908 help topic to getting started).
It is necessary to handle the client-side ASPxClientDateEdit.DateChanged event, and retrieve the newly selected Date via the client-side ASPxClientDateEdit.GetDate() method. Use the retrieved js Date object for your additional needs:
<script type="text/javascript">
function OnDateChanged(s, e) {
var newDate = s.GetDate();
alert(newDate);
}
</script>
settings.Properties.ClientSideEvents.DateChanged = "OnDateChanged";
There is a rather long Blog post at http://kennytordeur.blogspot.com/2011/05/aspnet-mvc-where-is-clientid_10.html discussing your problem
( I think it is to long to have it pasted here, and the author deserves the credits )
following on from your comment on Mikhails's answer, there will be a property in the global namespace with the name of your control, so it's just like this:
CalculateDayDifference(s.GetDate(), EndDate.GetDate());
All the mvc controls do this, for some you might have to set the EnableClientSideApi property to start using them.

Resources