Yii2 Call to a member function load() on a non-object - post

I have standard code in controller for User class
public function actionEdit($username)
{
...
$model = User::findByUsername($username);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
And if I edit user I get error
Call to a member function load() on a non-object
which is pointing at $model->load
Why is that ?
UPDATE
var_dump on $model shows NULL
which is strange
because I have the same function used in view action and it works perfectly
public function actionView($username){
$model = User::findByUsername($username);
if($model){
UPDATE2
I've made some changes now the code is like this, no errors
but the logic goes that there is no load and save, cause if goes to else section and edit is loaded again not view.
$model = User::findByUsername($username);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'username' => $model->username]);
} else {
return $this->render('edit', [
'model' => $model,
}

Use
var_dump($model);
To see what exacly $model is. Propably it's not a model, check what's result of function findByUsername()

make sure your function is check and find one, like
protected function findByUsername($username)
{
if (($model = User::find()->where(['username' => $username])->one()) !== null) {
return $model;
} else {
throw new NotFoundHttpException('not found.');
}
}

Related

Kendo Upload control remove event has no response

I have a kendo upload control like this:
#(Html.Kendo().Upload()
.Name("attachments")
.Async(a => a
.Save("UploadAsync", "Intel")
.Remove("RemoveAsync", "Intel")
.AutoUpload(true)
)
.Events(e => e
.Success("onSuccessfulUpload")
.Remove("onRemoveFile")
)
.Validation(v => v.AllowedExtensions(exts))
)
In the controller, its Save method is like this:
public ActionResult UploadAsync(IEnumerable<HttpPostedFileBase> attachments)
{
string filename;
// ... do things ...
return Json(new { ImageName = filename }, "text/plain");
}
where the variable filename is assigned a value.
Its Remove method in the controller looks very similar:
public ActionResult RemoveAsync(string[] fileNames)
{
string filename;
// ... do things ...
return Json(new { ImageName = filename }, "text/plain");
}
I verified that both controller methods are called correctly and the variable filename is assigned to in both cases.
The upload works as expected, and the Success event also works as expected. (The alert is simply for testing.)
function onSuccessfulUpload(e) {
alert(e.response.ImageName);
}
The issue comes on removal of a file.
When I get to the Remove event, e does not have a .response. It has e.files and e.sender, but no response.
function onRemoveFile(e) {
alert(e.response); // undefined!
alert(JSON.stringify(e.files)); // works, but does not have what I need
}
How do I access what the RemoveAsync method returns?
It looks like the remove event doesn't provide this kind of data, so I see only a workaround to solve this.
You could try to put the result name to the headers, and you should be able to read the result:
// Controller
Response.AddHeader("ImageName", imageName); // before Json(...)
// View/JS
alert(e.headers['ImageName']);
I haven't tested that and I see a risk that that the remove event doesn't really read the async response, that would explain why the response object is not available.
In that case, you could try to use the following workaround: Don't call any Url on remove (or use some Action without any body, just a plain result) and inside of the event callback, execute RemoveAsync yourself.
// View/JS
function onRemoveFile(e) {
$.post('#Html.Url("RemoveAsync", "Intel")', e.files, function(response) {
alert(response);
});
}
It's not pretty, but it should work and provide the results you need.
After some time poking around, I found the answer.
The key lay in the order of the events. My first assumption was that the Success event was called after successful upload, and the Remove event was called after successful(?) removal. This was wrong.
The actual order of the events is:
JS onUpload > Controller UploadAsync > JS onSuccess
JS onRemoveFile > Controller RemoveAsync > JS onSuccess
My Solution:
I created two parallel arrays in javascript to represent the files uploaded in the client-side e.files, which contains uid's for each file, and the filenames created by the server-side controller method (which renames the files).
var fileUids = [];
var fileSaveNames = [];
I changed the onSuccessfulUpload function to this, when I discovered that there is an e.operation that specifies which operation was the successful one:
function onSuccess(e) {
if (e.operation == "upload") {
var filename = e.response.ImageName;
var uid = e.files[0].uid;
// add to the arrays
fileUids.push(uid);
fileSaveNames.push(filename)
// ...
}
else if (e.operation == "remove") {
var uid = e.files[0].uid;
var saveIdx = fileUids.indexOf(uid);
// remove from the arrays
fileSaveNames.splice(saveIdx, 1);
fileUids.splice(saveIdx, 1);
// ...
}
}
Then I updated the removeFile function, which I now knew was called before the method in the controller.
function removeFile(e) {
var uid = e.files[0].uid;
var idx = fileUids.indexOf(uid);
e.data = { fileToRemove: fileSaveNames[idx] };
}
That last line, where I assign to e.data, was because of this thread on the Telerik forums, which has the following info:
Solution: All that's needed it to define a function for the upload
event and modify the "data" payload.
Add the upload JS function to add a parameter "codeID" in my case.
$("#files").kendoUpload({
[...]
upload: function (e) {
e.data = { codeID: $("#id").val() };
}
});
Now on the controller add the parameter and that's it.
[HttpPost]
public ActionResult Save(IEnumerable<HttpPostedFileBase> files, Guid codeID) { }
(Instead of being in the Upload event, mine is in the Remove event.)
I chose the parameter name fileToRemove, and now the new RemoveAsync method in the controller is as such:
public ActionResult RemoveAsync(string[] fileNames, string fileToRemove)
{
string returnName = "";
if (!string.IsNullOrWhiteSpace(fileToRemove))
{
// ... do things ...
}
return Json(new { ImageName = returnName }, "text/plain");
}

Get and set the current value from the relationship between "maquina" and "emisor"

Related to this topic and this other topic I'm experimenting a issue. This is the SdrivingMaquinForm.class.php code:
class SdrivingMaquinaForm extends BaseSdrivingMaquinaForm {
protected $current_user;
public function configure() {
$this->current_user = sfContext::getInstance()->getUser()->getGuardUser();
unset($this['updated_at'], $this['created_at']);
$this->widgetSchema['idempresa'] = new sfWidgetFormInputHidden();
$id_empresa = $this->current_user->getSfGuardUserProfile()->getIdempresa();
$this->setDefault('idempresa', $id_empresa);
$this->widgetSchema['no_emisor'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingEmisor', 'add_empty' => 'Seleccione un Emisor', 'table_method' => 'fillChoice'));
$this->validatorSchema['idempresa'] = new sfValidatorPass();
$this->validatorSchema['no_emisor'] = new sfValidatorPass();
}
protected function doUpdateObject($values) {
parent::doUpdateObject($values);
if (isset($this['no_emisor'])) {
if ($this->isNew()) {
$sdrivingMaquinaEmisor = new SdrivingMaquinaEmisor();
$this->getObject()->setSdrivingMaquinaEmisor($sdrivingMaquinaEmisor);
} else {
$sdrivingMaquinaEmisor = $this->getObject()->getSdrivingMaquinaEmisor();
}
$sdrivingMaquinaEmisor->setIdemisor($this->values['no_emisor']);
}
}
}
And it works perfectly, if I create a new maquina values are saved correctly, if I edit a existent record once again values are saved correctly and if I delete a record then the relation is deleted too. So the problem is not in actions or method. The problem I'm having is when user select to edit the existent record. Field idempresa and patente (see the schema.yml at first post metioned here) gets theirs values but no_emisor doesn't so every time I want to edit the record I got the select with values, yes, but the selected value isn't the right because I get the add_empty value. How I fix that? Meaning how I assign the default value for the select based on the one existent on the relation between maquina and emisor?
EDIT: working on a possible solution
I'm trying this code:
public function executeEdit(sfWebRequest $request) {
$this->forward404Unless($sdriving_maquina = Doctrine_Core::getTable('SdrivingMaquina')->find(array($request->getParameter('idmaquina'))), sprintf('Object sdriving_maquina does not exist (%s).', $request->getParameter('idmaquina')));
$this->forward404Unless($sdriving_maquina_emisor = Doctrine_Core::getTable('SdrivingMaquinaEmisor')->find(array($request->getParameter('idmaquina'))), sprintf('Object sdriving_maquina_emisor does not exist (%s).', $request->getParameter('idmaquina')));
$this->form = new SdrivingMaquinaForm($sdriving_maquina, $sdriving_maquina_emisor);
}
But then how in the form configure() method I can access to $sdriving_maquina_emisor in order to use form setDefault() method?
EDIT: doUpdateObject($values)
See this is how my doUpdateObject($values) function looks like:
protected function doUpdateObject($values) {
parent::doUpdateObject($values);
if (isset($this['no_emisor'])) {
if ($this->isNew()) {
$sdrivingMaquinaEmisor = new SdrivingMaquinaEmisor();
$this->getObject()->setSdrivingMaquinaEmisor($sdrivingMaquinaEmisor);
} else {
$sdrivingMaquinaEmisor = $this->getObject()->getSdrivingMaquinaEmisor();
}
$sdrivingMaquinaEmisor->setIdemisor($this->values['no_emisor']);
}
}
Where exactly feet the code you leave for doUpdateObject()?
In these situations you always have to do 2 things:
load the default value from the doctrine record object into the form widget
update the doctrine object with the posted value
And most of the times you should use updateDefaultsFromObject and doUpdateObject symmetrically.
To load back the saved values override updateDefaultsFromObject:
// maybe you have to declare it as public if the parent class requires that
protected function updateDefaultsFromObject()
{
parent::updateDefaultsFromObject();
if (isset($this['no_emisor'])
{
$this->setDefault('no_emisor', $this->getObject()->getSdrivingMaquinaEmisor()->getIdemisor());
}
}
// and you can simplify this a little bit as well
protected function doUpdateObject($values)
{
parent::doUpdateObject($values);
if (isset($this['no_emisor']))
{
$this->getObject()->getSdrivingMaquinaEmisor()->setIdemisor($this->values['no_emisor']);
}
}

Attribute.IsDefined returns false, Attribute.GetCustomAttribute returns null for defined ActionNameAttribute

I'm trying to set up some unit tests to ensure that URLs will be mapped to the appropriate controllers and actions according to a route-table, and that the target action-method and controller exists within the relevant assembly.
The only remaining problem I'm having is testing the existence of an action-method where an ActionNameAttribute has been applied to enable dash-separated action-name mappings, e.g., a "Contact Us" form url: /contact-us maps to the ContactUs method on a Forms controller because the ContactUs method signature is defined thusly:
[ActionName("contact-us")]
public ActionResult ContactUs()
I've set up the following method, which I am running inside each test, and works for all cases where the action-method name is not redefined with ActionNameAttribute:
private static bool ActionIsDefinedOnController(string expectedActionName, string controllerName, string assemblyName)
{
var thisControllerType = Type.GetType(AssemblyQualifiedName(controllerName, assemblyName), false, true);
if (thisControllerType == null)
return false;
var allThisControllersActions = thisControllerType.GetMethods().Select(m => m.Name.ToLower());
if( allThisControllersActions.Contains(expectedActionName.ToLower()))
return true;
var methods = thisControllerType.GetMethods();
//If we've so far failed to find the method, look for methods with ActionName attributes, and check in those values:
foreach (var method in methods)
{
if (Attribute.IsDefined(method, typeof(ActionNameAttribute))
{
var a = (ActionNameAttribute) Attribute.GetCustomAttribute(method, typeof (ActionNameAttribute));
if (a.Name == expectedActionName)
return true;
}
}
return false;
}
...but whenever a method's name is redifined with ActionNameAttribute, the check Attribute.IsDefined(method, typeof(ActionNameAttribute) is failing (returns false), even when I can see the attribute in the list of custom-attributes in my debugging session:
Why is this check failing, when it should be passing?
I've been able to construct a different check:
UPDATE I had pasted in the wrong code here initially, here's the revised:
List<string> customAttributes = method.GetCustomAttributes(false).Select(a => a.ToString()).ToList();
if (customAttributes.Contains("System.Web.Mvc.ActionNameAttribute"))
{
var a = (ActionNameAttribute) Attribute.GetCustomAttribute(method, typeof (ActionNameAttribute));
if (a.Name == expectedActionName)
return true;
}
...and now my condition is catching the cases where ActionNameAttribute is applied, but now Attribute.GetCustomAttribute() returns null. So I can't check the value of the action name to compare against the expected value... arrrrgh!
I would just have:
//If we've so far failed to find the method, look for methods with ActionName attributes, and check in those values:
foreach (var method in methods)
{
var attr = method.GetCustomAttribute<System.Web.Mvc.ActionNameAttribute>();
if (attr!=null && attr.Name == expectedActionName)
{
return true;
}
}
As I said in comment, I suspect that you're picking up the wrong ActionNameAttribute in your typeof calls, so I've been explicit

How to call a controller method from JavaScript when project is run under the IIS?

I want to call the JavaScript function for counting the selected checkbox and in this function I have to call one statement for call the actionresult method of controller and perform to some function as active user and deactive user and return view() at end
Here is my code:
if (state == "Dec") {
alert("Hello..Dear..you are DeActive");
$.post('#Url.Action("UserDeactive","Admin", new{})' + '?Id=' + strvalue);
}
This statement works well in the normal project when I run project by press the F5, also using VS localhost.
But when I host my project in IIS, for accessing in LAN, this is not work hold java script call but this statement are not call and not navigate to action result method ..for do some function..!
so please me ...All My Dear..! if you have any idea .!
this is my Controller Method:-
public ActionResult UserActive(string Id)
{
int[] numbers = Id.Split(',').Select(n => int.Parse(n)).ToArray();
if (numbers != null)
{
foreach (int id in numbers)
{
User_Master u_master = db.User_Masters.Find(id);
if (u_master.Is_active == "false")
{
u_master.Is_active = "true";
db.Configuration.ValidateOnSaveEnabled = false;
db.SaveChanges();
}
}
}
return RedirectToAction("Dashboard", "Home");
}
Use the second argument of the $.post() method which allows you to POST additional parameters to the server:
var url = '#Url.Action("UserDeactive", "Admin")';
$.post(url, { id: strvalue }, function(result) {
// handle the result of the AJAX call
});

Show only first error message

I am using ASP.NET MVC3 for a form that has both server and client validations. I'm showing error messages as balloons above the inputs. Due to the presentation of the errors, I need to only show one error at a time, otherwise the balloons tend to obscure other fields that may also be in error.
How can I customize the validation behavior to only render the first error message?
Edit: Please notice that the form has both server and client validations, and that I only want to show the first error message for the entire form (not per field).
In case anyone needs it, the solution I came up with is to add the following script towards the bottom of the page. This hooks into the existing javascript validation to dynamically hide all but the first error in the form.
<script>
$(function() {
var form = $('form')[0];
var settings = $.data(form, 'validator').settings;
var errorPlacementFunction = settings.errorPlacement;
var successFunction = settings.success;
settings.errorPlacement = function(error, inputElement) {
errorPlacementFunction(error, inputElement);
showOneError();
}
settings.success = function (error) {
successFunction(error);
showOneError();
}
function showOneError() {
var $errors = $(form).find(".field-validation-error");
$errors.slice(1).hide();
$errors.filter(":first:not(:visible)").show();
}
});
</script>
Could give this a shot on your controller action
var goodErrors = ModelState.GroupBy(MS => MS.Key).Select(ms => ms.First()).ToDictionary(ms => ms.Key, ms => ms.Value);
ModelState.Clear();
foreach (var item in goodErrors)
{
ModelState.Add(item.Key, item.Value);
}
I'm just selecting only one of each property error, clearing all errors then adding the individual ones back.
this is completely untested but should work.
You could create a custom validation summary which would display only the first error. This could be done either by creating an extension for the HtmlHelper class, or by writing your own HtmlHelper. The former is the more straightforward.
public static class HtmlHelperExtensions
{
static string SingleMessageValidationSummary(this HtmlHelper helper, string validationMessage="")
{
string retVal = "";
if (helper.ViewData.ModelState.IsValid)
return "";
retVal += #"<div class=""notification-warnings""><span>";
if (!String.IsNullOrEmpty(validationMessage))
retVal += validationMessage;
retVal += "</span>";
retVal += #"<div class=""text"">";
foreach (var key in helper.ViewData.ModelState.Keys)
{
foreach(var err in helper.ViewData.ModelState[key].Errors)
retVal += "<p>" + err.ErrorMessage + "</p>";
break;
}
retVal += "</div></div>";
return retVal.ToString();
}
}
This is for the ValidationSummary, but the same can be done for ValidationMessageFor.
See: Custom ValidationSummary template Asp.net MVC 3
Edit: Client Side...
Update jquery.validate.unobstrusive.js. In particular the onError function, where it says error.removeClass("input-validation-error").appendTo(container);
Untested, but change that line to: error.removeClass("input-validation-error").eq(0).appendTo(container);
Create a html helper extension that renders only one message.
public static MvcHtmlString ValidationError(this HtmlHelper helper)
{
var result = new StringBuilder();
var tag = new TagBuilder("div");
tag.AddCssClass("validation-summary-errors");
var firstError = helper.ViewData.ModelState.SelectMany(k => k.Value.Errors).FirstOrDefault();
if (firstError != null)
{
tag.InnerHtml = firstError.ErrorMessage;
}
result.Append(tag.ToString());
return MvcHtmlString.Create(result.ToString());
}
Update the jquery.validate.unobtrusive.js OnErrors function as below,
function onErrors(form, validator) { // 'this' is the form element
// newly added condition
if ($(form.currentTarget).hasClass("one-error")) {
var container = $(this).find(".validation-summary-errors");
var firstError = validator.errorList[0];
$(container).html(firstError.message);
}
else {
var container = $(this).find("[data-valmsg-summary=true]"),
list = container.find("ul");
if (list && list.length && validator.errorList.length) {
list.empty();
container.addClass("validation-summary-errors").removeClass("validation-summary-valid");
$.each(validator.errorList, function () {
$("<li />").html(this.message).appendTo(list);
});
}
}
}
Basically we have added a condition in the OnError to check whether the form contains a css-class named one-error and if yes then displays a single error else display all.

Resources