Passing potential nulls to Macros in Spark view engine - asp.net-mvc

I have the following macro:
<macro name="InputField" id="string" value="string">
...
<input type="text" id="${id}" name="${id}" value="${value} />
...
</macro>
And the call to the macro:
${InputField( "model.address.address1", 75, "Address", model.Address.Address1 )}
The only problem is that model.Address will be null in some situations
(creating the item instead of editing it), because of this the macro
doesn't run or fails and just outputs the macro call to the view.
How can I pass either "" or the value of model.Address.Address1
depending if Address is null or not? the null operator ($!{}) doesnt
seem to work in this instance.

Solution 1. Write method
public static string HandleNull(Func<object> func)
{
try { return func().ToString(); }
catch (NullReferenceException) { return ""; }
}
and use it instead of Spark macro.
${InputField( "model.address.address1", 75, "Address", HandleNull(() => model.Address.Address1) )}
Solution 2. Use http://www.jmill.net/taxonomy/term/312
Solution 3.
<macro name="InputField" id="string" value="Func<string>">
...
<input type="text" id="${id}" name="${id}" value="$!{value()} />
...
</macro>
${InputField( "model.address.address1", 75, "Address", () => model.Address.Address1 )}
All the solutions depend on deferred execution.

Related

react-hook-form: How validate object works

I see in the docs
<input
{...register("test1", {
validate: {
positive: v => parseInt(v) > 0,
lessThanTen: v => parseInt(v) < 10,
checkUrl: async () => await fetch(),
}
})}
/>
So here how can i show different messages for each validation.
like number is -5, then i show
"Number is not positive"
"Number less than 10"
HOw to access each error positive and lessThanTen
Per default RHF will only show one error per field, so if there are multiple you will you don't have to loop over them. You can just use the errors object provided by RHF and access the name of your field and then the message property.
If you need to have all errors to be shown simultaneously you can set the config criteriaMode, check the docs here for more info.
function App() {
const {
register,
handleSubmit,
formState: { errors }
} = useForm();
const onSubmit = (data) => {
console.log(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label htmlFor="test1">Test Field</label>
<input
{...register("test1", {
validate: {
positive: (v) => parseInt(v) > 0 || "Number is not positive",
lessThanTen: (v) => parseInt(v) < 10 || "Number less than 10",
checkUrl: async () => {
const result = await Promise.resolve(true);
return result || "result was false so show this message";
}
}
})}
/>
{errors.test1 && <p>{errors.test1.message}</p>}
<input type="submit" />
</form>
);
}
To apply multiple validations, you can build a custom hook as a resolver. A custom hook can easily integrate with yup/Joi/Superstruct as a validation method, and be used inside validation resolver.
You can find more doc and examples here:
https://react-hook-form.com/advanced-usage/
in the section:
Custom Hook with Resolver
I recommend you yup.

React.js - Rails: Set State

I have two components: App and Registration Form
The form has two inputs: Name and Last name
Looking at the App state in dev. tools I see length: undefined and name: "name entered". I'm not getting any errors but I'm missing the last name.
This is only happening in Rails. I tried the same code in a non rails environment and it works fine. I'm using this gem for React: gem 'react-rails', '~> 1.5.0' and running Rails 4.2.4
var App = React.createClass({
getInitialState : function(){
return {
registrations: {}
}
},
addRegistration : function(registration){
// create unique id
var timestamp = (new Date()).getTime();
// update state
this.state.registrations['registration-' + timestamp] = registration;
//set the state
this.setState({ registrations : this.state.registrations });
},
render : function(){
return (
<RegistrationForm addRegistration={this.addRegistration}/>
)
}
});
var RegistrationForm = React.createClass({
createRegistration : function(event){
// prevent default
event.preventDefault();
// take data from form and create object
var registration = {
name : this.refs.name.value,
lastname : this.refs.lastname.value
}
// Add registration to App Object
this.props.addRegistration(registration);
this.refs.registrationForm.reset();
//console.log(registration);
},
render : function(){
return (
<div className="col-sm-12">
<form action="" className="form" ref="registrationForm" onSubmit={this.createRegistration}>
<div className="form-group">
<label >Name</label>
<input className="form-control" ref="name"/>
</div>
<div className="form-group">
<label >Last Name</label>
<input className="form-control" ref="lastname"/>
</div>
<div>
<button className="btn btn-primary">Submit</button>
</div>
</form>
</div>
)
}
});
App = React.createFactory(App)
What I'm trying to do is to give each registration a unique id number based on the time stamp.
When I console log the following:
addRegistration : function(registration){
// create unique id
var timestamp = (new Date()).getTime();
// update state
this.state.registrations['registration-' + timestamp] = registration;
//set the state
this.setState({ registrations : this.state.registrations });
},
I can see a registration object the way I want it. I can add as many unique registrations to the App state but each registration has length: undefined, name: "name" , but it's missing the last name.
If I change the set state to this:
this.setState({ registrations : registration });
This gives me a single registration with name and last name but it doesn't add multiple registrations. It only creates one registration which gets update every time I submit the add registration form.
this.state.registrations['registration-' + timestamp] = registration;
You seem to be mutating the state directly, based on the React Docs https://facebook.github.io/react/docs/component-api.html
NEVER mutate this.state directly, as calling setState() afterwards may
replace the mutation you made. Treat this.state as if it were
immutable.
setState() does not immediately mutate this.state but
creates a pending state transition. Accessing this.state after calling
this method can potentially return the existing value.
There is no
guarantee of synchronous operation of calls to setState and calls may
be batched for performance gains.
setState() will always trigger a
re-render unless conditional rendering logic is implemented in
shouldComponentUpdate(). If mutable objects are being used and the
logic cannot be implemented in shouldComponentUpdate(), calling
setState() only when the new state differs from the previous state
will avoid unnecessary re-renders.
Try cloning the current state then use that as the argument.
// if array
var clonedRegistration = this.state.registrations.slice();
clonedRegistration['registration-' + timestamp] = registration;
this.setState({registrations: clonedRegistration})
or
this.setState({registrations: {['registration-'+ timestamp]: registration} });
I think the answer Road put was close.
First set your initial state to an array.
getInitialState: function(){
return { registrations: []}
}
your addRegistration function
addRegistration : function(registration){
I think this is what you're missing:
//getting current state
var oldRegistrations = this.state.registrations;
Otherwise I believe you're updating the same thing over and over, instead of adding a new registration object. Then push your registration. You should set the timestamp
// update state
oldRegistrations.push(registration);
var registrations = oldRegistrations;
//set the state
this.setState({ registrations : registrations });
},
I would advise creating the id somewhere in here since you're not using an actual ajax call to a rails db:
var registration = {
name : this.refs.name.value,
lastname : this.refs.lastname.value
id: (new Date()).getTime();
}
I'm not sure I understand your question regarding your form values or if you were having trouble with them. But if you were I think doing something like this may help:
<input type='text' className='form-control'
placeholder='Name' name='name'
value={this.state.name} onChange={this.handleChange} >
</input>
<input type='text' className='form-control'
placeholder='Last Name' name='last name'
value={this.state.last_name} onChange={this.handleChange} >
</input>
Then implement a handleChange function within the same component to constantly handle the form's values onChange. That should look like this:
handleChange: function(e) {
var name = e.target.name;
var obj = {};
obj[name] = e.target.value;
this.setState(obj);
}
Hope this helps,

Passing parameter value from react component

In my app, people can comment on pets' images. I am using the react example from here, although I changed quite a few stuff.
Right now, it is successful in displaying the existing comments. Now, when a user is creating a comment, I have to pass the comment body, user id, and pet id. I was trying to do the following:
var CommentForm = React.createClass({
handleSubmit:function()
{
var user=this.refs.user_id.getDOMNode().value.trim();
var comment=this.refs.body.getDOMNode().value.trim();
var pet_id=this.refs.pet_id.getDOMNode().value.trim();
this.props.onCommentSubmit({comment:comment, user:user, pet:pet_id});
if(!user||!comment||!pet_id)
return false;
var formData = $( this.refs.form.getDOMNode() ).serialize();
this.props.onCommentSubmit( formData, this.props.form.action );
// reset form
this.refs.body.getDOMNode().value = "";
},
render: function () {
return (
<form ref="form" className="comment-form" action={ this.props.form.action } accept-charset="UTF-8" method="post" onSubmit={ this.handleSubmit }>
<p><input type="hidden" name={ this.props.form.csrf_param } value={ this.props.form.csrf_token } /></p>
<p><input type="hidden" ref="user" value={ this.props.user_id } /></p>
<p><input type="hidden" ref="pet_id" value={ this.props.pet_id } /></p>
<p><textarea ref="body" name="comment[text]" placeholder="Say something..." /></p>
<p><button type="submit">Post comment</button></p>
</form>
)
}
});
And apparently, it doesn't look like it is passing the pet_id correctly, because I am getting the error message
ActiveRecord::RecordNotFound in CommentsController#create
Couldn't find Pet with 'id'=
My CommentsController looks like
def create
#pet = Pet.find(params[:pet_id])
#comment = #pet.comments.new(comment_params)
#comment.user = current_user
For further clarification, I have three models, Pets, Users and Comments, and when users make comments, the comment gets the user_id, and pet_id as its parameters.
edit:
My react component looks like
<%= react_component('CommentBox',
{:presenter => #presenter.to_json},
{:prerender => true}) %>
and my PetController looks like
def show
#comments = #pet.comments
#user = current_user
#presenter = {
:comments => #comments,
:user => current_user,
:pet_id => #pet,
:form => {
:action => comments_path,
:csrf_param => request_forgery_protection_token,
:csrf_token => form_authenticity_token
}
So there are a few issues I can see. Firstly your using ref where you should be name.
<input type="hidden" ref="pet_id" value={ this.props.pet_id } />
should be
<input type="hidden" name="pet_id" value={ this.props.pet_id } />
Your setting both an action and an onSubmit. Is there a reason to do this? Why not just read it from the props when you perform the ajax request? This is most likely causing your form to be submitted and the browser to load another page. The form submitting has nothing to do with what is on the server. The issue is in your client side code.
I would also consider putting your model values in to their own array. This is generally what rails expects back from the server. In your case it should be params[:pet][:id] not params[:pet_id]. Many of the rails active record methods such as update attributes can then be directly called giving you less verbose code.
#pet = Pet.find(params[:pet][:id])
#pet.update_attributes(prams[:pet])
#pet.save

Render Razor view to string not keeping the name attribute

I have used this solution of rendering a view into a string successfully until I had to do the validation.
This is on the view:
#Html.TextBoxFor(m => m.OrderedQuantity, new { id="someid", name="somename", data_mini = "true", type = "number", #class = "orderedQuantity removeGroupsRequired" })
and what the result is:
<input class="orderedQuantity removeGroupsRequired" data-mini="true" data-val="true" data-val-number="The field Quantity: must be a number." data-val-required="The Quantity: field is required." id="someid" name="OrderedQuantity" type="number" value="0" />
and I need the name in order do make some rules for an unobtrusive validation:
var form = $("#mydiv form");
form.validate(
{
rules: {
somename: {
required: true
}
},
messages: {
somename:
{
required: "you must provide a quantity!"
}
}
}
);
You cannot set the name attribute when using the TextBox helper. That's by design. So don't try it. This helper is designed to generate markup with input field names matching your view model properties so that when the form is submitted the default model binder will be able to bind the corresponding value to the property on the view model:
#Html.TextBoxFor(
m => m.OrderedQuantity,
new {
id = "someid",
data_mini = "true",
type = "number",
#class = "orderedQuantity removeGroupsRequired"
}
)
The name of the input field is OrderQuantity, so that's what you should use for your validation rules:
form.validate({
rules: {
OrderedQuantity: {
required: true
}
},
messages: {
OrderedQuantity: {
required: "you must provide a quantity!"
}
}
});
If you want to be able to use custom name, different than what the correct name for the model binding would have been (OrderedQuantity), then you will have to either write a custom helper or hardcode the markup (both are totally non-recommended solutions).

Problems with $this->getRoute()->getObject() losing it's context.

Please see my question in the template section below:
Form (showSuccess):
<?php echo form_tag('job/salarySubmit') ?>
<input type="hidden" name="job_id" value="<?php echo $job->getId(); ?>">
<input type="submit" value="View Salary">
</form>
Action:
public function executeSalarySubmit(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod('post'));
$param = array('job_id' => $request->getParameter('job_id'), );
$this->redirect('job/salary?'.http_build_query($param));
}
public function executeSalary(sfWebRequest $request)
{
$this->object_id = $request->getParameter('job_id');
$this->salary = $this->getRoute()->getObject();
}
Template (salarySuccess.php):
<?php echo $object_id; ?> // returns correct job_id: 6100, but when I try to access the object's other getters (example: echo $object_id->getName(), I get "Fatal error: Call to a member function getName() on a non-object"
<?php echo $salary->getName(); ?> //works, but it gets the wrong name. It's returning the first job in my DB with an ID of "1" which is not correct... it needs to be 6100
Routing:
job_salary:
url: /job/salary/:job_id
param: { module: job, action: salary }
class: sfDoctrineRoute
options: { model: Job, type: object }
requirements:
id: \d+
sf_method: [GET]
use var_dump() on your template variables.
You'll see that $object_id is not an object (it is a non-object, like the error message says). $object_id is most likely a string or an integer and its value must be "6100".
$salary is not the object you're looking for, because sfDoctrineRoute looks for a parameter called id, and you named it job_id in your routing.yml (which is strange, because you name it id in this route's requirements.

Resources