Datamapper validation errors with Sinatra - sinatra

I'm trying to validate my Datamapper models and show the validation errors in my HAML template, I'm using Sinatra. I'm at loss how to save those errors into an instance variable and access them from my template. I've looked around for some documentation or tutorials explaining how to do something like this but I haven't been able to find anything.

Here you go
haml view:
%label{:for => "title"} Title
%input#title{:name => "title", :type => "text", :size => "22", :maxlength => "256", :required => true, :placeholder => "Title...", :autofocus => true, :value => #post.title}/
= field_validation(#post, :title)
helper method:
def field_validation(target, field)
"<span class=\"field-validation-error\">#{target.errors[field][0]}</span>" unless target.errors[field].empty?
end

Related

Add Entity Reference to Drupal 8 Form Field?

How can users add products to a simple plugin config using an autocomplete field?
I tried to use Config Entity but it looks the same as Form API (and I can't use entity fields there).
I was able to do this in Drupal 8 using the form API and the entity_autocomplete type.
$form['stories'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#title' => $this->t('Stories'),
'#description' => $this->t('Select some stories.'),
'#default_value' => $default_entities,
'#tags' => TRUE,
'#selection_settings' => array(
'target_bundles' => array('page', 'article'),
),
'#weight' => '0',
];
Use webform - there is an entity reference field.
This won't be useable for your purpose .. but you can check the source for sake.

Haml Form Not Submitting in Sinatra App

I'm having problems submitting my form now that I've converted from erb to haml in a simple sinatra app.
new.haml
%form{ :action => "/new", :method => "post"}
%fieldset
%ol
%li
%label{:for => "username"} Name:
%input{:type => "text", :username => "name", :class => "text"}
%input{:type => "submit", :value => "Send", :class => "button"}
In my app.rb
get '/new' do
haml :new
end
post '/new' do
radcheck = Radcheck.new(:username => params[:username])
if radcheck.save
redirect '/'
else
"Hello World"
end
end
each time I get the Hello World statement appear. My logs show nothing interesting.
Any ideas? Worked just fine with erb??
This is what I've tested
get '/new' do
haml :new
end
post '/new' do
#radcheck = Radcheck.new(:username => params[:username])
username = params[:username]
if username
username
else
"Hello World"
end
end
and new.haml
%form{ :action => "/new", :method => "post"}
%fieldset
%ol
%li
%label{:for => "username"} Name:
%input{:type => "text", :name => "username", :class => "text"}
%input{:type => "submit", :value => "Send", :class => "button"}
And it works as expected. So for some reason radcheck.save is returning false, but that has nothing to do with haml. (But notice that I have corrected input with :name => "username")

Ruby on Rails Formtastic Multiple Options in Member_Label

I have a relational model where users have managers that are also users. The below code works great and does exactly what it's suppose to, but it's only displaying the first name of the manager. I'm trying to get this to show both the first name and the last name of the manager.
<%= sf.input :managers, :as => :check_boxes, :member_label => (:firstname) ,:input_html => { :size => 20, :multiple => true}%>
The other field i'm trying to add is the :lastname. I cannot figure out how to get :member_label to take both fields.
I figured it out. By using the Proc.new, I was able to add in both first name and last name.
<%= sf.input :managers, :as => :check_boxes, :member_label => Proc.new { |t| h(t.firstname + " " + t.lastname) } ,:input_html => { :size => 20, :multiple => true}%>

Load a Symfony sfWidgetFormDoctrineChoice select with a limited set of results

I am developing a form in a Symfony application where a user must indicate a country, a region and an optional island using HTML select elements.
I have three models: Country, Region and Island; and Symfony has auto-generated three widgets in the form using the sfWidgetFormDoctrineChoice widget:
...
'country_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Country'), 'add_empty' => false)),
'region_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Region'), 'add_empty' => false)),
'island_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Island'), 'add_empty' => true)),
...
Since the country list is large, and so the region list, I've been thinking on filtering the options available in Region and Island according to the value selected in Country.
Doing this after the HTML document is ready is easy with jQuery's change method and a simple AJAX request. But I wonder if there's a way of doing this directly from Symfony, perhaps in form configuration, to have a default combined selection.
Any suggestions?
Thanks!
After playing around with sfDependentSelectPlugin, I ended up assigning custom queries to initialize the HTML select elements:
$countryId = $this->getObject()->getCountry()->getTable()->getDefaultCountryId();
$regionId = $this->getObject()->getRegion()->getTable()->getDefaultRegionId($countryId);
$islandId = $this->getObject()->getIsland()->getTable()->getDefaultIslandId($regionId);
$this->widgetSchema->setDefault('country_id', $countryId);
$this->setWidget('region_id', new sfWidgetFormDoctrineChoice(array(
'model' => $this->getRelatedModelName('Region'),
'query' => $this->getObject()->getRegion()->getTable()->getRegionsQuery($countryId),
'default' => $regionId,
)));
$this->setWidget('island_id', new sfWidgetFormDoctrineChoice(array(
'model' => $this->getRelatedModelName('Island'),
'query' => $this->getObject()->getIsland()->getTable()->getIslandsQuery($regionId),
'add_empty' => '---',
'default' => $islandId,
)));
And then updating the options available with AJAX requests using jQuery. The good thing is that the actions that handle the AJAX requests use the same query methods above to return a new set of results.

Using Apache::Session::Memcached with MasonX::Request::WithApacheSession

I'm trying to use Apache::Session::Memcached in an HTML::Mason project where I'm using MasonX::Request::WithApacheSession to handle my sessions. Unfortunately Apache will not launch when I plug in the Memcached module instead of the MySQL one. My custom handler looks something like this (a few snips here and there):
my $ah = HTML::Mason::ApacheHandler->new (
comp_root => $ENV{HTDOCS},
data_dir => $data_dir,
request_class => 'MasonX::Request::WithApacheSession',
session_use_cookie => 0,
args_method => "mod_perl",
session_args_param => 'session_id',
session_class => 'Apache::Session::Memcached',
session_Servers => '127.0.0.1:20000',
session_Readonly => 0,
session_Debug => 1,
session_cookie_domain => $CONF->{global}->{site_name},
session_cookie_expires => "session",
session_allow_invalid_id => 0,
);
The problem I'm running into is that the session_* paramaters specific to Memcached are not being passed through to Apache::Session::Memcached like the docs say it should. This results in this error:
The following parameter was passed in the call to HTML::Mason::ApacheHandler->new()
but was not listed in the validation options: session_Servers
Now, I have gone through and swapped all of the 3 upper case arguments to lower case, to no avail. And the docs for Apache::Session::Memcached list them as upper case.
Thanks a ton for any help.
It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper, following the instructions at http://search.cpan.org/perldoc/Apache::Session::Wrapper#REGISTERING_CLASSES like so (code courtesy Jack M.):
Apache::Session::Wrapper::->RegisterClass(
'name' => 'Apache::Session::Memcached',
'required' => [ [ 'Servers' ], ],
'optional' => [ 'NoRehash', 'Readonly', 'Debug', 'CompressThreshold', ],
);