Yii2 remove form name from url parameters - yii

I use Yii2 forms and after submit my URL looks like:
http://example.com/recommendations/index?RecommendationSearch%5Bname%5D=&RecommendationSearch%5Bstatus_id%5D=&RecommendationSearch%5Btype%5D=0&RecommendationSearch%5Bcreated_at%5D=&sort=created_at
As you may seem each parameter contains form name RecommendationSearch. How to remove this RecommendationSearch from parameters in order to get URL url like the following:
http://example.com/recommendations/?name=&status_id=&type=0&created_at=&sort=created_at

You need to override formName() in your RecommendationSearch model to return empty string:
public function formName() {
return '';
}

Related

How can I get the value from #QueryParam

Basically what I'm trying to do is edit an entity that's stored in my db. For that I have a little method that is trying to access a paramater that's defined with #PathParam. My problem is that it comes back as null. Here is my method:
#PUT
#Path("/{id}")
#Produces(MediaType.APPLICATION_JSON)
public Response edit(#PathParam("id") Long id, #QueryParam("myParam") String name)
{
return Response.ok().build();
}
I`m using Postman to send the parameter to my server. My URL looks like this:
http://localhost:8080/myApplication/rest/users/1?myParam=test
How can I get the value from the parameter?
Try with
public Response edit(#PathParam("id") Long id, #QueryParam("myParam") String myParam) {
return Response.ok().build();
}
This will work. The query param and variable name will get auto bind if they are similar.

Get Query String Value in ASP.NET MVC Function

I have an ASP.NET MVC app. My views use Razor. At the top of my CSHTML file, I have the following:
#functions
{
public static HtmlString IsSelectedCss(string name)
{
string selected = ""; // Need to get value of "t" from query string
HtmlString attribute = new HtmlString("");
if (selectedTab.Equals(name, StringComparison.InvariantCultureIgnoreCase))
{
attribute = new HtmlString("class=\"active\"");
}
return attribute;
}
}
I need this function to examine the query string. Specifically, I need to get the value of the "t" query string parameter. My challenge is, I cannot seem to figure out how to get access to the QueryString in this function.
How do I get the value of a query string parameter in a Razor function?
Thanks!
The query string can be gotten from below.
HttpContext.Current.Request.QueryString["t"]
You need to make your function non-static, since the querystring is part of the request.
You can then write
HttpContext.Request.Query["t"]
You should really be doing this in the controller and pushing it through the model. But if you insist, you can simply use:
<%= Request["t"] %>
But why not read it in your controller?!

Parsing URL in Yii

How to parse the Url which in the format as shown
http://localhost/CorrectCodeToDeploy/healthkatta/index.php?r=site%2FArticle%2F2013%2F07%2F13%2FDisadvantages-of-smoking-51e12690a5a58
Want to get rid of the special characters %2Fand put / instead . I have shared a link of an article to facebook
but when i access this link from facebook it comes in the format as mentioned above and the content is not properly accessed in Yii.How can i write the pattern to access this kind of url in yii
Maybe you can directly decode your $_GET in your index.php file, something like:
$_GET = array_map('urldecode', $_GET);
Please be aware the above doesn't work with nested arrays, so you need to create a more in depth recursive function like (not tested though) :
function urldecodeArray($strArr){
if (!is_string($strArr) && !is_array($strArr)) {
return $strArr;
}
if(is_string($strArr)) {
return urldecode($strArr);
}
foreach ($strArr as $key=>$value) {
$strArr[$key] = urldecodeArray($value);
}
return $strArr;
}
$_GET = urldecodeArray($_GET);
You can just to use urldecode() function

Specifyng a default message for Html.ValidationMessageFor in ASP.NET MVC4

I want to display an asterisk (*) next to a text box in my form when initially displayed (GET)
Also I want to use the same view for GET/POST when errors are present) so For the GET request
I pass in an empty model such as
return View(new Person());
Later, when the form is submitted (POST), I use the data annotations, check the model state and
display the errors if any
Html.ValidationMessageFor(v => v.FirstName)
For GET request, the model state is valid and no messages, so no asterisk gets displayed.
I am trying to workaround this by checking the request type and just print asterisk.
#(HttpContext.Current.Request.HttpMethod == "GET"? "*" : Html.ValidationMessageFor(v=> v.FirstName).ToString())
The problem is that Html.ValidationMessageFor(v=> v.FirstName).ToString() is already encoded
and I want to get the raw html from Html.ValidationMessageFor(v=> v.FirstName)
Or may be there is a better way here.
1. How do you display default helpful messages (next to form fields) - such as "Please enter IP address in the nnn.nnn.nnn.nnn format) for GET requests and then display the errors if any for the post?
2. What is the best way from a razor perspective to check an if condition and write a string or the MvcHtmlString
Further to my last comment, here is how I would create that helper to be used:
public static class HtmlValidationExtensions
{
public static MvcHtmlString ValidationMessageForCustom<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string customString)
{
var returnedString = HttpContext.Current.Request.HttpMethod == "GET" ? customString : helper.ValidationMessageFor(expression).ToString();
return MvcHtmlString.Create(returnedString);
}
}
And it would be used like this #Html.ValidationMessageForCustom(v=> v.FirstName, "Please enter IP address in the nnn.nnn.nnn.nnn format")

send parameters to actionIndex in Yii controllers

I want to send a sql string for index action in Yii controller? something for example:
index.php?r=staff/index&id=1
I tried it and changed actionIndex() to actionIndex($id) but Yii gave me
error 400 : Your request is invalid.
Is it possible or I have to define another action ?
no you don't need to do that, receive the id as the normal request parameter inside your action method
public function actionIndex(){
$id = $_GET['id'];
}
your can get parameter as follows.
$key = Yii::app()->getRequest()->getParam('your_parameter_name');
and also you can get all the parameters from
$allParam = Yii::app()->getRequest()->restParams
OR
actionIndex($id = 0)
in your staff controller