Using #RenderSection("SectionName", false), why do I need to explicitly set the 2nd parameter to false when the Intellisense already states that the default is false?
Update:
The RTM signature of the RenderSection method is:
public HelperResult RenderSection(string name, bool required)
There also exists an override that looks like this:
public HelperResult RenderSection(string name) {
return RenderSection(name, required: true);
}
Note that this method no longer uses default parameters, instead opting for explicit overrides.
The signature of this method changed twice during the development of MVC 3 which explains why you might be seeing confusing examples.
Edit: It appears that the MVC 3 RTM documentation is incorrect and erroneously references a default value of the required parameter.
it needs to be true. You are saying that the section is optional.
#RenderSection("SectionName", true)
or
#RenderSection("SectionName", optional: true)
Related
I'm new to OOP and Yii2. I have a function in Model:
public function getDatRev() {
if ($this->rev) {
return $this->rev;
} else {
return $this->datum;
}
}
in the View until now I have used it like this:
$model->datRev;
and it would return the correct value. Now I don't know what has changed, maybe I was also updated the framework, but the old construct doesn't work anymore, and in order to make it work I have to change it to:
$model->getDatRev();
Can you please explain to me why that is?
When you try get property the Yii2 calls magic method __get (). Return value is depend from implementation of this method in parent class. Yii2 can check if this property exist in some container, or if exist getter of this property.
In your case seems like you don't call parent's method __get(). This may have happened because you override __get() method or initialized this property.
Your class needs to extend yii\base\Object (directly or not) in order to use short property syntax ($model->abc instead of $model->getAbc()). Magic method __get() #Timur mentioned is defined there and further extended in yii\base\Component class.
I recently met an issue with validator (illuminate/validation) under Lumen (5.4.6) (Laravel Components 5.4.*). It seems like the integer rule doesn't work.
I used Paw(or postman) to send companyName with type string and it can pass the validation and 'here i am' can printed out. Even a boolean type, let's say (boolean)companyName=TRUE can pass the validation. Have you met the same issue? this bug is quite obvious but I didn't find the similar discussion on google.
I also tested the string rule 'required|string|min:1' and it works accordingly, neither integer nor boolean parameters can pass the validation.
code screen-shot
Laravel/Lumen makes use of PHP filter_var() function for integer rule validation:
Class Illuminate\Validation\Concerns\ValidatesAttributes:
protected function validateInteger($attribute, $value)
{
return filter_var($value, FILTER_VALIDATE_INT) !== false;
}
As mentioned in the docs:
Note that scalar values are converted to string internally before they are filtered.
So boolean true is converted to string 1 internally and therefore passes validation.
It's not considered a bug, but an implementation detail.
According to http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx#optionals-and-defaults
You can have optional parameters by adding a question mark (?) when using attribute routing. However it does not work for me (ASP.NET Web API 5).
[Route("staff/{featureID?}")]
public List<string> GetStaff(int? featureID) {
List<string> staff = null;
return staff;
}
If I use staff/1 etc it works fine, if I use /staff I get the usual:
"No HTTP resource was found that matches the request URI..."
"No action was found on the controller that matches
the request."
Am I missing a reference or something? Or doing it wrong?
I also ran into the same issue and solved it a little differently. However, it still didn't work for me as laid out in that blog post. Instead of adding the default parameter value in the route definition I added it to the function definition.
I had to do this to for my example to work properly because I was using a string instead of an int and adding the default in the route definition of null caused my function parameter to have the string value "null".
[Route("staff/{featureID?}")]
public List<string> GetStaff(int? featureID = null) {
List<string> staff = null;
return staff;
}
This is because you always have to set a default value for an optional parameter, even if the default is null. That is why this works:
[Route("staff/{featureID=null}")]
If I do:
[Route("staff/{featureID=null}")]
instead of
[Route("staff/{featureID?}")]
It works.
Technically this doesn't answer my question but it gets me working!
From the FluentValidation documentation I learned that I can abort validation by setting the cascade mode.
RuleFor(x => x.Surname)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotNull()
.NotEqual("foo");
That way if the property Surname is null, the equality check won't be executed and a null pointer exception prevented. Further down in the documentation it is implied that this would also work not only within a rule but on a validator level as well.
public class PersonValidator : AbstractValidator<Person> {
public PersonValidator() {
// First set the cascade mode
CascadeMode = CascadeMode.StopOnFirstFailure;
// Rule definitions follow
RuleFor(...)
RuleFor(...)
}
}
I set the CascadeMode not inside the rule definition but for an instance of a validator. The expected behaviour would be that if the first RuleFor fails, the second RuleFor won't be evaluated but that's not the case. Regardless of previous validation errors, all rules are being evaluated.
Am I using it wrong or did I misinterpret the documentation?
According to the JeremyS' answer, I have misunderstood the purpose of the CascadeMode. It is in fact not intended to have effect on a validator level but only within a rule.
You can set CascadeMode at the global level by setting
ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;
or at property level by
RuleFor(x => x.PropertyName)
.Cascade(CascadeMode.StopOnFirstFailure)
If you are using .NET Core you can set the cascade mode at a global level like below
.AddFluentValidation(fv =>
{
fv.RunDefaultMvcValidationAfterFluentValidationExecutes = true;
fv.ValidatorOptions.CascadeMode = CascadeMode.Stop;
fv.RegisterValidatorsFromAssemblyContaining<Startup>();
});
I'm using 2.4.7 and I want to include some validation for two fields which take in prices (e.g. €1 or 2 for €3). Originally I thought that perhaps I would need to resort to validating user input but as the answer suggests it was a database issue.
The encoding within SilverStripe was defaulting to ASCII which converted the symbols such as the euro symbol. In the end I need to add
$this->response->addHeader("Content-Type", "application/json; charset='utf-8'");
to the init method in the controller. This corrected the encoding issue and prevented a hacky workaround taking place. Many thanks for the insight on this one.
Thanks
I'm assuming you want to do this in the CMS. If that's the case, the easiest way is probably to create a new class which extends the TextField class and adds a public function validate() method which performs your validation (see the CreditCardField class for an example).
class FancyCurrencyField extends TextField {
public function validate($validator) {
// Do your validation
if ($invalid) {
$validator->validationError(
$this->name,
"Please don't use currency symbols.",
"validation",
false
);
return false;
}
return true;
}
}
Once you've created your class, you can modify the form fields in your getCMSFields() function on the DataObject and use the new class in place of TextField.
Having said that, it feels like output encoding and not input validation is the root cause of your problem. I'd check to make sure that everything is setup to use UTF-8.
I encountered a problem with the PHP function substr. I just used mb_substr instead and it solved my issue.
Please view http://www.silverstripe.org/general-questions/show/11797#post369831